1

In my deployment process, I need to run npm run production after the files are available on the server.

Since I deploy to a Docker container, I figured I'd need to run something like:

cd /apps/laradock/ && docker-compose exec -T workspace bash -c "cd /var/www/myapp/ && npm run production"

Unfortunately, this produces the error:

bash: npm: command not found

But npm is available both outside the container and in!

  • Outside the container, which npm produces /home/serviceUser/.nvm/versions/node/v10.9.0/bin/npm
  • And if I enter the container via cd /apps/laradock/ && docker-compose exec workspace bash and then run which npm, it's installed there too (and shows /home/laradock/.nvm/versions/node/v10.9.0/bin/npm).

What am I doing wrong?

Ryan
  • 22,332
  • 31
  • 176
  • 357
  • I suppose that `npm run production` adds the dependencies in you already running server. I think this task can be done using a script or entrypoint to execute your command after you copy your source files. – Eddy Hernandez Aug 27 '18 at 21:28
  • @EddyHernandez I don’t really understand what you’re suggesting. – Ryan Aug 27 '18 at 23:22
  • Does your `.bashrc` or similar login script inside the container setup the path for `npm`? – BMitch Aug 28 '18 at 00:53

2 Answers2

1

2 options here.

1) use the full path of the npm binary; instead of using npm run production use /home/laradock/.nvm/versions/node/v10.9.0/bin/npm run production

2) add nvm to your .bashrc (if you use bash) in order to source it so you can type nvm directly:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Not sure how did you install nvm since if you were following the install instruction from their github page, that should have been added already to your .bashrc file

For more information about how to install nvm, source it and use it you can check the link bellow: https://github.com/creationix/nvm#install-script

Bogdan Stoica
  • 4,349
  • 2
  • 23
  • 38
  • Your first option results in `/usr/bin/env: ‘node’: No such file or directory`. The 2nd option is already what I have. My `~/.bashrc` already has those lines (except the `NVM_DIR` inside the container's `.bashrc` says `export NVM_DIR="/home/laradock/.nvm"`). – Ryan Aug 27 '18 at 20:56
  • And I meant to say: Thank you for your quick and clear answer! I appreciate your attempt to help. I'll keep poking around and will see what I can figure out. – Ryan Aug 27 '18 at 21:57
1

I wonder if upgrading npm and nodejs is what got it working. Here is what I did (as root rather than entering the workspace container with --user=laradock):

cd /apps/laradock/ && docker-compose exec workspace bash
apt-get update
apt-get install -y npm
curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt-get install -y nodejs

(from https://stackoverflow.com/a/34558299/470749 and https://askubuntu.com/a/1044694/48214)

Now this works: docker exec -it laradock_workspace_1 /bin/sh -c "cd /var/www/myapp/ && npm run production"

It would be great if I could get this similar question working too: How to use Deployer with Docker (Laradock)

P.S. Now outside the container, npm -v shows "6.2.0" and nodejs -v shows "v4.2.6" while inside container: npm -v shows "6.4.0" and nodejs -v shows "v10.9.0" and nvm --version shows "0.33.8".

I'm still not sure how to get this to install properly in the Laradock Dockerfile. Its default way of installing nvm and npm doesn't seem to expose them to commands from outside the container such as docker exec -it laradock_workspace_1 /bin/bash -c "npm -v".

Update 12 months later

I added this near the bottom of my Dockerfile (adding it sooner caused errors), and it seems to work:

RUN apt update && \
    apt install -y npm nodejs

#https://stackoverflow.com/a/26320915/470749
RUN ln -s /usr/bin/nodejs /usr/bin/node 

RUN npm --version && node --version

RUN npm cache clean -f && \
    npm install npm@latest -g n && \
    #npm install -g n && \
    n stable && \    
    npm --version && node --version
Ryan
  • 22,332
  • 31
  • 176
  • 357