Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Run Node/NPM in a Docker container
# For example, run "npm install"
docker run -v "$PWD":/usr/src/app -w /usr/src/app node:4 npm install
# This command creates a container (downloading one first if you don't have it locally), runs the command in a current directory and quits the container
# Great Success!
@feload

This comment has been minimized.

Copy link

feload commented Apr 21, 2017

Nice one!

@willsilvano

This comment has been minimized.

Copy link

willsilvano commented Jul 27, 2017

Thanks!!

@paulwellnerbou

This comment has been minimized.

Copy link

paulwellnerbou commented Aug 22, 2017

Thanks very much. Based on this I created a build command, using current node container (alpine, because it is smaller) and chaining two npm commands together:

docker run -v "$PWD":/usr/src/app -w /usr/src/app node:alpine sh -c 'npm install && npm run dist'
@luisincrespo

This comment has been minimized.

Copy link

luisincrespo commented Nov 21, 2017

👍

@bertalanimre

This comment has been minimized.

Copy link

bertalanimre commented Jan 10, 2018

With the latest LTS version:
sudo docker run -v $(pwd):/usr/src/app -w /usr/src/app node:8.9.4 npm install

👍 👍

@t3ran13

This comment has been minimized.

Copy link

t3ran13 commented Apr 22, 2018

for docker in windows

docker run -v c:\path_to_app:/usr/src/app -w /usr/src/app node:8.9.4 bash -c "npm install"
@giansalex

This comment has been minimized.

Copy link

giansalex commented May 26, 2018

For Windows (CMD)

docker run --rm -v "%cd%":/app -w /app node:alpine npm i
@patoi

This comment has been minimized.

Copy link

patoi commented Oct 31, 2018

On MacOS/Linux with NodeJS 10 LTS:

docker run --user $(id -u):$(id -g) -v $PWD:/app -v $PWD/.npm:/.npm -v $PWD/.config:/.config -w /app node:10.12.0-alpine npm install
@Mithrandir0407

This comment has been minimized.

Copy link

Mithrandir0407 commented Apr 16, 2019

Nice work

@guifcoelho

This comment has been minimized.

Copy link

guifcoelho commented Jun 6, 2019

Why not create a file named 'npm' in '/usr/local/bin' with:

#!/bin/bash
docker run --rm -i --user=$UID:1000 --name=npm-executable -w /usr/app -v $PWD:/usr/app node:10 npm "$@"

then
sudo chmod +x /usr/local/bin/npm

now you can use npm install, npm run anything, etc.

@clrmahieu

This comment has been minimized.

Copy link

clrmahieu commented Jul 30, 2019

Hello, I'm new to Docker and lost in your instructions...
What are we supposed to put instead of the c:\path_to_app and the /usr/src/app expressions ?

Thanks for the help !

@jtrancas

This comment has been minimized.

Copy link

jtrancas commented Jul 31, 2019

@clrmahieu you only have to replace the c:\path_to_app with the full path for your app root folder, usually it's your current path where you have your package.json file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.