Containerizing node.js applications
Build a simple nodejs application
Build the node application docker image
cd building-a-nodejs-app
# build
docker build -t my-node-app .
# docker image size
docker inspect my-node-app
# run
docker run --init --rm --publish 3000:3000 my-node-app
Preparing a multi stage build process
cd multi-stage-build
# build
docker build -t multi-node -f multi.node.Dockerfile .
# docker image size
docker inspect multi-node
# run
docker run --init --rm -p 3000:3000 multi-node
Create application using create-react-app and serve static files using nginx
cd serve-static-files-nginx
# build
docker build -t static-app .
# run
# Port exposed to host is 8080. Internal port used is 80. And nginx uses 80 as the default port
docker run --init --rm -p 8080:80 static-app
# Using docker bind mount. Below command mounts the /build directory inside nginx html directory
docker run --mount type=bind,source="$(pwd)"/build,target=/usr/share/nginx/html -p 8080:80 nginx:1.17-alpine
Bind mount
- Bind mount are works great when you need to share data between host and container.
- Build image
docker build --tag=incrementor-bind .
- New image create with the tag name
incrementor-bind
- New image create with the tag name
- Run container
docker run --rm --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind
Volume mount
- Using bind volumes containers can maintain states between runs.
- Build image
docker build --tag=incrementor-volume .
- Run container
docker run --rm --env DATA_PATH=/data/num.txt --mount type=volume,src=incrementor-data,target=/data incrementor-volume
Try all example commands within this directory
--env
Passes process environment variable. For exampledocker run --rm --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind
. HereDATA_PATH
is the process environment variable passed. Node application can use this data byprocess.env.DATA_PATH
--rm
Deletes the container after exiting the process. Try running any container with and without this flag to see the difference. For exampledocker run --rm --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind
deletes the container after exiting the run process, butdocker run --env DATA_PATH=data/num.txt --mount type=bind,source="$(pwd)"/incrementor-data,target=/src/data incrementor-bind
doesn't deletes the container after exiting the processsrc
orsource
mount source directory i.edocker run --rm --env DATA_PATH=/data/num.txt --mount type=volume,src=incrementor-data,target=/data incrementor-volume
ordocker run --rm --env DATA_PATH=/data/num.txt --mount type=volume,source=incrementor-data,target=/data incrementor-volume
--init
Gracefully exits node process where listen is used
Inspired & credits to @btholt for creating a course in FEM & sharing complete into to containers
- Docker netowkring Crash Course By Hussein Nasser