We can use the Docker build command to build Docker images from Dockerfiles. Docker allows us to create images in two different ways.
- The first one is by directly pulling a base image from Docker registries such as Dockerhub using Docker pull command. You can then create containers associated with these images using the Docker run command.
- The second and most widely used method is by using Dockerfiles. You can mention instructions such as FROM (to pull base images), COPY (to copy files), CMD (to specify entrypoint instructions), etc. to build Docker images. Once done, you can use the Docker build command on these Dockerfiles to build and use them.
In this article, we will look at how to build Docker images using the Docker build command on Dockerfiles. You can check out our complete and free Docker tutorial to learn Docker the best possible way.
To install Docker on your machine, check out these guides.
What is a Dockerfile?
A Dockerfile is a simple text file that contains instructions to build Docker images. We can think of them as the blueprint of your application and Docker container environment. The first instruction that needs to mentioned inside a Dockerfile is the FROM instruction. This instruction helps Docker to identify the base image that needs to be used to create the custom image.
Each instruction in a Dockerfile contributes to a new image layer that is built on top of the previous layers. An image layer only consists of the changes that have been made to the previous layer. All the instructions subsequent to the FROM instruction take place on top of the corresponding base image.
Some common instructions used in a Dockerfile are –
- FROM: To specify the base image to be pulled from the Docker registry.
- WORKDIR: To specify the default working directory for all the subsequent instructions.
- COPY: To copy the build context to the container.
- RUN: To specify the commands to be executed inside the container. It is generally used to install packages and other dependencies.
- CMD: To specify the command to be executed once the container is run.
- ENV: To specify the environment variables of the container. It persists throughout the container session.
- ARG: To specify arguments for temporary use. The variables created using ARG are not accessible inside the container.
- ENTRYPOINT: To specify multiple entrypoint commands such as commands to run applications, etc.
Docker Build Command
The Docker build command helps us to build Docker images using a Dockerfile with instructions mentioned and a build context. The build context includes all your application files and other files that will help you to build Docker images. This includes Dockerfiles, .Dockerignore files, source code of your application, etc.
The syntax of the Docker build command is –
$ docker build [OPTIONS] PATH | URL | -
Another form of this command is –
$ docker image build [OPTIONS] PATH | URL | -
The PATH or URL parameter specifies the path of the build context. We can specify plain text files, pre-packaged tarball files, or even git repositories through the URL parameter.
We can use the following important options to build our Docker images.
|–tag, -t||To provide a tag in the format <image-name>:<tag>.|
|–rm||To remove all the intermediate containers after a successful image build.|
|–quiet, -q||It is used to suppress the output and only print Image ID.|
|–pull||Used to always pull newer versions of the images.|
|–output, -o||To print the output destination.|
|–label||To provide metadata to images.|
|–force-rm||To forcefully remove intermediate containers.|
|–build-arg||It is used to specify build-time variables.|
How to build your first Docker Image?
Let’s check out an example that will help you to understand how to build Docker images using a Dockerfile. In this example, we’ll try to dockerize a Flask application. We will try to build our own Flask image as Docker does not have any official repository for Flask. We will create a flask Python application and try to host in a docker container and access it inside the local machine. Finally, we will publish ports so that the container and local machine can communicate with each other.
Creating the Flask Application
First, let’s create our simple Flask application.
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Welcome to TechTutorialSite" if __name__ == "__main__": app.run(host ='0.0.0.0', port = 5001, debug = True)
You can create a simple flask application in a file with a .py extension. Refer to the code snippet above stored in a file name called app.py inside the same directory that will contain our Dockerfile. Here, we have created a simple function called hello that will display a message once the application is hosted. We can try to access it on port 5001.
Creating the Dockerfile
Next, let’s create the Dockerfile. The Dockerfile must not have any extension and should be named simple Dockerfile. Both the Dockerfile and app.py file should be in the same directory and they together constitute the Docker build context.
FROM python:latest COPY . /app WORKDIR /app RUN pip install -r requirements.txt EXPOSE 5001 ENTRYPOINT [ "python" ] CMD [ "app.py" ]
Now, let’s create a requirements.txt file inside the same directory. We will use it to install all the packages needed to run the application. In this example, we only need the flask library. Hence, the content of the requirements file is –
Let’s try to understand each instruction mentioned in the Docker file above.
- We have used the FROM instruction to pull a Python image from the Dockerhub registry. We have specified a tag along with the image name. Tags are used to specify a name, metadata, version, etc. to an image. We want to pull the latest version of the Python image.
- Next, we have used the COPY instruction to copy the build context from the current directory (containing both the files) to the /app directory in the Docker container.
- We have used the WORKDIR instruction to set the default working directory as /app.
- Next, we have used the RUN instruction to specify commands to install packages from a requirements.txt file.
- We have used the EXPOSE instruction to expose port 5001 of the container on which the application will run. To access the flask application inside a browser in our local machine, we will need to publish this port to another port in our host machine. We will do it using the publish option later on.
- We have used the ENTRYPOINT instruction to specify the entrypoint command which needs to be run once we run the container.
- Finally, we have used the CMD command to specify the arguments of the ENTRYPOINT command. Together, they mean that we need to execute the “python app.py” command automatically once the container runs.
Building the Docker Image
Now that we have created all the files, let’s try to build the Docker Image using the Docker build command. For this, open a terminal inside the directory that contains the build context and run the below command.
$ docker build -t flaskapp .
We can use the -t option to specify a name and tag to the image. You can see that all the instructions mentioned inside the Dockerfile have been executed step-by-step. Also, each instruction creates a new image layer on top of the previous ones. On executing the image build command, the Docker first sends the build context to the daemon which checks whether the base image is already present in the host or not. If yes, it does not pull a new copy of the image from Dockerhub, If no, it first pulls the image from Dockerhub and executes the rest of the instructions on top of it.
Listing Docker Images
If we want to verify whether or not the image has been successfully built, we can try to list all the images in our host machine. There are two commands to do so.
$ docker images
$ docker image ls
You can use any one of them. Let’s try to use the Docker images command to list all the images.
You can see that we have successfully built our image.
Running Docker Images to create Containers
Now that we have our Docker Image, we can use the Docker run command to create and run a container associated with this image. The syntax of the Docker run command is –
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
To run a container of our flaskapp image, we can use the following command.
$ docker run --name=flaskappcont -d -p 5001:5001 flaskapp
We can use the –name option to specify a name to the container. We have used the -d option to run the container in the background or detached mode. Finally, we have used the -p option to publish a port of the host machine to the exposed ports of the container. Let’s list all the containers that are actively running.
$ docker ps
You can see that the ports have been published and we can now access the flask application at localhost:5001 in the browser.
In this article, we have discussed how to build Docker Images using the Docker build command. We looked at how Dockerfiles work and some common instructions used to create them. Then, we saw how to build a Docker image using a Dockefile by creating a simple Flask application. We hosted the Flask application inside a container and accessed it on our browser on the local machine. Next, we saw how to build images, how to run containers, list images and containers, and publish ports.
We hope that this article will get you up and running with Docker images. If you have any queries or suggestions, please mention them in the comment box and we will have our experts get back to you.
Check out these articles too.