NodeJS app building and publishing to the registry with docker

Martin
DevOps Dudes
Published in
3 min readMay 22, 2020
Part two

This is the follow-up article to the previous one, where we prepared the environment for development. Here we will prepare the same application for the next stages that lead towards production. Make sure that you have read the previous article before proceeding with this one. Mostly because of crusial project files.

Because we want to have a pre-built docker image of our application we will create a Dockerfile in the root of the project:

FROM: node:10-alpine

We used node:10-alpine image, because is the smallest image (about 40MB) that can run Node applications. You can find other available images here: https://hub.docker.com/_/node

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

In the third line we created the folders and changed the ownership to the node user.

USER: node

9th line changes the user to “node” . This allows access to the project files.

Project files are copied with COPY and set the ownership to node user and group.

To prevent copying the development environment’s folders to the image we include a .dockerignore file as well:

We need to include the Dockerfile to docker-compose, so we include the build and change the container_name tags. The new docker-compose.yaml looks like this:

The folder structure of the project looks like this:

project   
| README.md
│ docker-compose.yaml
| Dockerfile
| .dockerignore
| server.js
| package.json
|
└───views
│ │ index.ejs

Building the image with using docker-compose.

docker-compose build

When the build finishes we can test run the image with:

docker run -p 3001:3001 nodeapp:1.0

Open the browser to the address: http://localhost:3001/ and you should be able to see the welcome message. Should you want to detach from the standard output of the container, just detach from the image with “-d”.

If you are facing problems with running the image, make sure the previous attempts are taken care of. That means you should stop other containers that use the same port as this application (3001 in our case) and delete the containers after stopping.

Now we can upload it to the registry from where it can be distributed to the production. Your company may have a private container registry but for this tutorial, we will be uploading to the public registry: https://hub.docker.com/.

docker login --username=yourhubusername --email=youremail@company.comdocker tag nodeapp https://hub.docker.com/

Acquire image ID with:

docker images

Use the ID to tag the image correctly in the following command. Also, make sure that the tags comply to the company policy. The “1.0” tag should be used with care. Using “latest” as a tag is often done with rigorous testing. Use the numbering tags and increment them accordingly.

docker tag edb1bb625ff0 yourhubusername/nodeapp:1.0docker push yourhubusername/nodeapp

The image is pushed to the registry and is now ready for further stages of its lifecycle.

Also, clap if you think this content is useful.

--

--