Docker
In this tutorial we will learn how to build Docker image and publish it to DockerHub.
A Docker Image is a file that acts as an immutable template to create Docker Container.
We build our own Docker image by defining it in a Dockerfile in our project and then running the docker build
command.
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.
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.
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.
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.
node_modules
npm-debug.log
test
coverage
docs
.idea
.env
.gitignore
.vscode
Now, we will create the Dockerfile and add the following lines.
FROM node:alpine
WORKDIR /app
COPY package.json ./
RUN npm install
COPY ./ ./
EXPOSE 3000
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. https://hub.docker.com/_/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"]
.
Now we will run the following command to create the docker image in our local machine.
docker build -t YOUR_NAME/YOUR_DOCKER_IMAGE_NAME .
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.
We run the following command to push the docker image we build to the DockerHub.
docker push YOUR_DOCKER_IMAGE_NAME
For this project I am running the following command.
docker push yusufshakeel/simple-restapi-nodejs
Here is the published docker image https://hub.docker.com/r/yusufshakeel/simple-restapi-nodejs
We run the following command to pull the docker image from DockerHub.
docker pull YOUR_DOCKER_IMAGE_NAME
For this project I am running the following command.
docker pull yusufshakeel/simple-restapi-nodejs
We run the following command to run the docker container using the docker image in detach mode and at a specified host port.
docker run -d -p HOST_PORT:CONTAINER_PORT DOCKER_IMAGE_NAME
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.
ADVERTISEMENT