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?

3
  • 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. Commented Aug 27, 2018 at 21:28
  • @EddyHernandez I don’t really understand what you’re suggesting. Commented Aug 27, 2018 at 23:22
  • Does your .bashrc or similar login script inside the container setup the path for npm? Commented Aug 28, 2018 at 0:53

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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").
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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.