Skip to content

Instantly share code, notes, and snippets.

Last active February 4, 2022 05:31
What would you like to do?

Example Dockerfile

FROM node:12-alpine
# Adding build tools to make yarn install work on Apple silicon / arm64 machines
RUN apk --no-cache --virtual build-dependencies add python2 make g++
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

Build docker image

docker build -t <image_name> .

Run detached with port mapped

docker run -dp <host port>:<container port> <image_name>

Run ubuntu container with running process

docker run -d ubuntu bash -c "shuf -i 1-10000 -n 1 -o /data.txt"

Execute command for running ubuntu container

docker exec <container_id> cat /data.txt

Create named volume

docker volume create <volume-name>

Run detached with port mapped and volume

docker run -dp <host port>:<container port>  -v <volume-name>:<container mount point> <image_name>

e.g. (all files saved to /etc/todos will be persisted in volume)

docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started

For development, run the container bound to the working source code directory and run something like nodemon

docker run -dp <host port>:<container port> -w <working_dir_in_container> -v "<host_folder>:<folder_in_container>" <image_name> <command to run the dev env>


docker run -dp 3000:3000 \
    -w /app -v "$(pwd):/app" \
    node:12-alpine \
    sh -c "apk --no-cache --virtual build-dependencies add python2 make g++ && yarn install && yarn run dev"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment