Build a Docker Image With Dockerfile

Building custom images with Docker is probably the most common task any DevOps engineer or a developer will do when developing containerized solutions.

Docker Build

To build container images with Docker CLI, Docker has two important tools to help us facilitate the process. The tools are the docker build command and configuration file called dockerfile.

In this post, we will build a Docker image that runs Python and flask using the following steps.

Create Build Directory

The first step in our process is to create a build directory that will hold all the image configuration files.

mkdir appbuild

Inside the appbuild directory, we create the Dockerfile configuration file.

Dockerfile Configuration

Now it is time to add our image configuration to the dockerfile. Let’s open the file and add the following lines and save the file.

FROM python
RUN pip install flask

The configuration is fundamental, and as you can see, the base image is Python latest, and we install flask using PIP. Below you could see the build output.

Build Image

To build the image, I will run the following command.

docker build --tag appbuild .

Below you could see the build output.

The image name in our case is appbuild, but you can change it to any name you line.

=> [internal] load build definition from Dockerfile                                                                                                                                               0.0s
 => => transferring dockerfile: 77B                                                                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                                    0.0s
 => [internal] load metadata for                                                                                                                                   0.0s
 => CACHED [1/2] FROM                                                                                                                                                     0.0s
 => [2/2] RUN pip install flask                                                                                                                                                                    5.2s
 => exporting to image                                                                                                                                                                             0.2s
 => => exporting layers                                                                                                                                                                            0.2s
 => => writing image sha256:6331835b4cd6fa8c9bbc50d6a5886a3b1da7461c3beb80f2d84e5c033bcb906b                                                                                                       0.0s 
 => => naming to               






9 responses to “Build a Docker Image With Dockerfile”

  1. […] To visit the previous about building a Docker image with a Dockerfile, click here. […]

  2. […] To build the image, please visit the following post. […]

  3. […] To learn how to build a Docker image, visit the following post. […]

  4. […] building Docker images with a Dockerfile, we can use the expose statement to open a network […]

  5. […] Building Docker images with a Dockerfile is the most common method to build and reuse images. When building images, sometimes there is a need to download files from the external website (URL) directory into the container. […]

  6. […] To learn how to build a Dockerfile, visit the following post. […]

  7. […] To learn more about building a Dockerfile visit this post. […]

  8. […] is a simple deployment that takes a Docker image I have built With a Dockerfile. You could also use a public image like […]

  9. […] you are building docker images you are probably consuming a lot space that can be freed. The following command will […]