NodeJS app dockerization

NodeJS app dockerization

NodeJS apps can be containerized using the docker build command. This article is based on this guide.

To create a container image, a Dockerfile is needed. Here is an example specifically made for the containerization of NodeJS apps:

FROM node:14

# Create and move into app directory
WORKDIR /usr/src/app

# Copy files of host current workdir
# into container workdir
COPY . .

# Install dependecies described in packages.json
RUN npm install

# Open port 8080
EXPOSE 8080

# Run the node app
CMD [ "node", "server.js" ]

RUN is for commands to be executed during the container building process

CMD is for commands to be executed when the container is run

With this Dockerfile, a container image can be built by running:

docker build -t docker_test .

where -t means tag

To reduce the size of the container image, node_modules can be prevented from being copied by creating a .dockeringore file with the following content:

node_modules