Skip to content

Instantly share code, notes, and snippets.

@bahmutov
Last active October 4, 2023 08:35
Show Gist options
  • Save bahmutov/448f73b49914d1981643 to your computer and use it in GitHub Desktop.
Save bahmutov/448f73b49914d1981643 to your computer and use it in GitHub Desktop.
Single command to run Node with local file access

Goal: run a simple NodeJS application inside a Docker container

Subgoal: give it access to local files.

docker run -it --rm --name example -v "$PWD":/usr/src/app -w /usr/src/app node:5-slim node index.js

output:

current working directory /usr/src/app
Hello, World!

Unwrapped command line

run - execute the image right away
-i  - inherit STDIO and STDOUT from the current shell process
-t  - execute the command (the command "node index.js" is at the end)
--rm - remove existing running image
--name example - give the running container user friendly name "example"
-v "$PWD":/usr/src/app - mount current directory inside the docker container as a volume, 
                         set its path as "/usr/src/app"
-w /usr/src/app - set the working directory before running the command
node:5-slim - use the "node:5-slim" as the docker base image for our container (small!)
node index.js - the command to run inside the new container
Hello, World!
console.log('current working directory', process.cwd())
var read = require('fs').readFileSync
console.log(read('./hello.txt', 'utf8'))
@cpalsa
Copy link

cpalsa commented Jul 13, 2018

Thank you, as someone learning docker right now the command line breakdown really helped.

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