Skip to content

Instantly share code, notes, and snippets.

@artemgordinskiy
Created December 11, 2015 14:00
Show Gist options
  • Save artemgordinskiy/b79ea473e8bc6f67943b to your computer and use it in GitHub Desktop.
Save artemgordinskiy/b79ea473e8bc6f67943b to your computer and use it in GitHub Desktop.
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!
@giansalex
Copy link

giansalex commented May 26, 2018

For Windows (CMD)

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

@patoi
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
Copy link

Nice work

@guifcoelho
Copy link

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
Copy link

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
Copy link

@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.

@Dzhuneyt
Copy link

Dzhuneyt commented Mar 11, 2021

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

This creates 2 folders .npm and .config within the current project's directory. I would avoid this since you can easily include them in the next commits by mistake.

A better alternative is to mount these folders from your user's home directory:

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

@MkLHX
Copy link

MkLHX commented Mar 22, 2021

Hi there!
There a way to store in VSCode the command line syntaxe to run npm run build from docker container?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment