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 docker.io/library/python:latest                                                                                                                                   0.0s
 => CACHED [1/2] FROM docker.io/library/python                                                                                                                                                     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 docker.io/library/appbuild               

9 comments

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.