Docker - How to build docker image and publish to DockerHub


← Prev

In this tutorial we will learn how to build Docker image and publish it to DockerHub.

Table of Content

What is a Docker Image?

A Docker Image is a file that acts as an immutable template to create Docker Container.

How do we build our own Docker Image?

We build our own Docker image by defining it in a Dockerfile in our project and then running the docker build command.

What is a Dockerfile?

Dockerfile is a text based document that contains instruction like what base image to use for our application, environment variables to set, ports to expose, files to copy and many other things as per requirement to create a Docker Image.

Steps to create a Docker image

Let's assume we have completed our application, fully tested it and it works.

For this tutorial I will be using my open source project simple-restapi-nodejs to explain how to create a Docker Image.

simple-restapi-nodejs is a NodeJS RESTful API application. To keep things simple the project exposes few api endpoints. You can check the details in the GitHub repository simple-restapi-nodejs.

The first thing we need to do in order to create a Docker image is to create the following files.

  • .dockerignore
  • Dockerfile

The .dockerignore file is used to ignore files from getting added into the Docker image. This is similar to a .gitignore file that we create when we are using Git as our version control system.

And we already covered what Dockerfile file is used for in the above section.

Let's begin creating the files.

Create .dockerignore file

In the project folder we add the .dockerignore file.

Since this is a Node project so we would want to ignore the following files from getting added to the Docker image.


Create Dockerfile

Now, we will create the Dockerfile and add the following lines.

FROM node:alpine
COPY package.json ./
RUN npm install
COPY ./ ./
CMD ["npm", "start"]

We always start with a base image by defining the FROM followed by the image name. In this case our base image is Node and of alpine tag node:alpine.

Head over to the official DockerHub page of Node to check different tags available for Node.

Next, we set the working directory. So inside our container we want to move to /app folder.

Next, we copy package.json file from our project directory to the working directory in the container.

Then, we install all the packages using the npm install command.

Next, we copy all the files to the working directory.

This project runs on port 3000 so we EXPOSE port 3000 of the container.

Finally, we define the command to run when the container starts which in this case is npm start so we have set it as ["npm", "start"].

Build docker image

Now we will run the following command to create the docker image in our local machine.


For this project I am running the following command.

docker build -t yusufshakeel/simple-restapi-nodejs .

This builds an image and saves in our machine.

Push docker image to DockerHub

We run the following command to push the docker image we build to the DockerHub.


For this project I am running the following command.

docker push yusufshakeel/simple-restapi-nodejs

Here is the published docker image

Pull docker image

We run the following command to pull the docker image from DockerHub.


For this project I am running the following command.

docker pull yusufshakeel/simple-restapi-nodejs

Run docker container

We run the following command to run the docker container using the docker image in detach mode and at a specified host port.


For this project I am running the following command.

docker run -d -p 3000:3000 yusufshakeel/simple-restapi-nodejs

This brings us to the end of this tutorial. Have fun learning.

← Prev