Home » Docker » What is Dockefile? Create docker image for nodejs

What is Dockefile? Create docker image for nodejs

In this tutorial you will learn what is Dockerfile and how to create docker image for nodejs application
A Dockerfile is a text file that has a sequence of instructions commands on how to build your application image. It provisions a simple set of commands that you need to use in your Dockerfile. There are several commands supported such as FROM, CMD, ENTRYPOINT, ARGUMENTS, VOLUME, ENV and many more. Let’s have a look at some of them.
Let us first of all we need to understand the flow, which is something like below:

What is a docker https://www.linuxpcfix.com/docker-installation-configuration/

1. You need to create a Dockerfile with required instructions.
2. Then need to execute docker build command to build docker image.

Create the directory.

[root@client01 ~]# mkdir /docker-project
[root@client01 ~]# cd /docker-projec
[root@client01 ~]# vi Dockerfile

Append the following the lines in your Dockerfile.

FROM node:10-alpine
MAINTAINER Gajendra Singh (singh677@gmail.com)
RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app
WORKDIR /home/node/app
COPY package*.json ./
USER node
RUN npm install
COPY –chown=node:node . .
CMD [ “node”, “app.js” ]

Each instruction or command creates one layer:
FROM generates a layer from the node:10-alpine Docker image.
MAINTAINER command indicates who is the author of the images
WORKDIR Application directory path in docker container
COPY adds files from your current local directory to Docker container.
USER Application user in docker container
RUN Install all nodejs application dependencies.
EXPOSE The port where nodejs application will listen
CMD specifies what command will execute while container start.
When you will execute docker build command generate an image and container, and will append a new writable layer (the “container layer”) on top of the primary layers. All changes or modifications made to the running container, like as writing new files, amending existing files, and deleting files, are written to this writable container layer.
A Docker image is built up from a sequence of layers. Each layer signifies an instruction in the image’s Dockerfile. Each layer excepting the very last one is read-only.
Execute the docker build command to build a image.

[root@client01 ~]#docker build -t nodejsapp:latest .

-t is the Docker image tag. You can assign a different name of your docker image
. is represent the current working directory where your Dockerfile existed.
List the created images.

Now you can launch a container with newly created image.

[root@client01 ~]#docker run -d -p 8080:8080 –name mynodeapp nodejsapp

Upload the docker image in your docker hub repository the follow the below instructions. Login to you docker hub repository.

[root@client01 ~]#docker login –username=your-hub-username –email=email@company.com
[root@client01 ~]#docker image tag nodejsapp:latest hub-user-name/repo/nodejsapp:latest
[root@client01 ~]#docker image push hub-user-name/repo/nodejsapp:latest

Now your docker image is now available for everyone to use and deploy.


I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry since more than 14 years.

Leave a Reply

Your email address will not be published. Required fields are marked *


Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud