Skip to content

Instantly share code, notes, and snippets.

@bahmutov

bahmutov/README.md

Last active Jul 20, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@cpalsa 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
You can’t perform that action at this time.