In this post we are going to dive deep into how Docker images are built and the best practices that can be followed to build a production-ready Docker image. So, let’s break down what we are going to learn from this post.
Build a Docker image using Docker file
Tag Docker images
Run Docker images
As a first step, we need a Dockerfile to build an image. So, let’s try to understand what that Dockerfile does. A Dockerfile is a text file with a series of commands in it.
1 2 3 4 5 6 7 8
FROM node # base image LABEL maintainer example @gmail.com # who is maintaining RUN git clone - q https: //github.com/docker-in-practice/todo.git # clone the source code WORKDIR todo # Move to the new directory RUN npm install > /dev/null # run node package manager EXPOSE 8000 # application should listen on this port CMD["npm", "start"] # command which runs on startup
You begin by defining the base image for your Dockerfile. In this example, Nodejs base image was used. The image name is “node”. We define the base image using the “FROM” keyword.
Using the “Label” command you can define the maintainer of the project.
“Run” command is very useful. You can execute shell commands using it. In this example we clone the git repo. After you clone the repo, “WORKDIR” changes the working directory to “todo” directory where the source code resides.
After changing the working directory we need to install the application dependencies. For Nodejs applications we use the npm package manager. We can use the “Run” command to execute the npm install. Since we are not interested in the out of the npm install command, we can direct the command output to /dev/null.
“EXPOSE” keyword specifies that containers from the built image should listen on this port.
CMD keyword lets you define the command you want to run during the container startup.
You can read more about the Dockerfile in the official documentation. It is nearly impossible to cover everything about Dockerfile in a single blog post.
https://docs.docker.com/engine/reference/builder
Once you build the image you will see the image ID. However, it is really difficult to keep referring to your image by the ID, so we need to tag the image with a friendly and meaningful name.
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
For our example we could tag our image as
docker tag 66c76cea05bb todoapp
Docker tagging is used for multiple reasons. Using Docker tags we can build reliable CI/CD pipelines. Each application change can be captured as a Docker image and stored in the registry. If required, it will be easy to roll back to the previous version of the application.
Now you have built your image and the image has been tagged. So finally, we need to run the Docker image. Running the Docker image is fairly easy to do.
docker run -i -t -p 8000:8000 --name example-todo todoapp
That’s all we have to do!.
You can list the running containers by executing docker ps command. It will list the running Docker containers, and other useful information.
In upcoming articles I will write about how we can push Docker images to your Docker registries and as well as set up Docker registries in your local machine.
CHECK OUT TOPCODER DEVOPS FREELANCE GIGS