JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Enforce clean build of images in Docker

Author:JIYIK Last Updated:2025/03/26 Views:

This article discusses and demonstrates how to enforce clean builds of images in Docker.

Building images in Docker

We will use a simple Flask application to demonstrate this concept. my-appCreate a app.pysimple application named in the home folder using the command below.

touch app.py

If you have not used this command, please install Python 3.

sudo apt update && sudo apt install python3-venv

We can also install Flask and Gunicorn using the command.

pip install flask gunicorn

Continue to create a docker file using the command touch Dockerfile. To make it easier for us to create docker files, we created a requirements.txtfile.

We can pip freeze > requirements.txtcreate a requirements.txtfile by running the command. Once completed, your file structure should be similar to the following.

File structure of containerized Docker applications

We can go ahead and go to app.pythe ./file and create our application. The application contains a simple function that prints a console message as shown below.

from flask import Flask

app = Flask(__name__)

def hello():

    print("Hello, this is a simple Flask application")

hello()

This is what we expect when we execute the code above.

The result of executing our Flask application

Now we will populate the Dockerfile with the commands that Docker will use to build the image. These commands will be executed when we run the docker container.

In this case, we will use Python as our base image. The Dockerfile should look like this.

# base image
FROM python

# Set your working directory
WORKDIR /var/www/

# Copy the necessary files
COPY ./app.py /var/www/app.py
COPY ./requirements.txt /var/www/requirements.txt

# Install the necessary packages
RUN pip install -r /var/www/requirements.txt

# Run the app
CMD python3 app.py

Once we have our Dockerfile ready, we can save it and build our image locally for testing. We can -tuse docker buildthe command next to the .

Building images in Docker

We can then docker runtest the image using the command as shown below.

(myapp) isaac@DESKTOP-HV44HT6:~/my-app$ docker run -it myapp
Hello, this is a simple Flask application

docker psWe will confirm that we have successfully built a new container using the command.

$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS     NAMES
dd10c9a6a8a1   myapp     "/bin/sh -c 'python3…"   11 minutes ago   Exited (0) 10 minutes ago             vigilant_brattain

Use the option in Docker --no-cacheto force a clean build of the image

After building a container using this image, suppose we want to build an image again using the same image. In this case, the docker daemon will use the existing cache of image layers to build the image.

However, we can --no-cacheforce a clean image build in Docker using the -build option. This overrides the default behavior of the docker daemon.

Here’s how we achieve it.

myapp) isaac@DESKTOP-HV44HT6:~/my-app$ docker build --no-cache -t myapp .
[+] Building 119.0s (10/10) FINISHED
 => [internal] load .dockerignore                                                                                                1.6s
 => => transferring context: 2B                                                                                                  0.4s
 => [internal] load build definition from Dockerfile                                                                             2.1s
 => => transferring dockerfile: 38B                                                                                              0.4s
 => [internal] load metadata for docker.io/library/python:latest                                                                37.9s
 => [1/5] FROM docker.io/library/python@sha256:ee0b617025e112b6ad7a4c76758e4a02f1c429e1b6c44410d95b024304698ff2                  0.1s
 => [internal] load build context                                                                                                0.4s
 => => transferring context: 63B                                                                                                 0.0s
 => CACHED [2/5] WORKDIR /var/www/                                                                                               0.1s
 => [3/5] COPY ./app.py /var/www/app.py                                                                                          1.3s
 => [4/5] COPY ./requirements.txt /var/www/requirements.txt                                                                      2.2s
 => [5/5] RUN pip install -r /var/www/requirements.txt                                                                          68.4s
 => exporting to image                                                                                                           4.8s 
 => => exporting layers                                                                                                          3.8s 
 => => writing image sha256:ee771b73a9ec468308375d139a35580f6c7f62988db9c0bb0b85794716406e92                                     0.1s 
 => => naming to docker.io/library/myapp

We can docker runcreate a new container using the command.

(myapp) isaac@DESKTOP-HV44HT6:~/my-app$ docker run -it myapp
Hello, this is a simple Flask application

As shown below, we have successfully built a new container and forced Docker --no-cacheto cleanly build the image using the ./build option.

$ docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
826b8de2c92f   myapp          "/bin/sh -c 'python3…"   47 seconds ago   Exited (0) 39 seconds ago             kind_pike
dd10c9a6a8a1   ba84c5d3b157   "/bin/sh -c 'python3…"   28 minutes ago   Exited (0) 28 minutes ago             vigilant_brattain

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:129 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:94 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:123 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:200 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:139 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Copy files from host to Docker container

Publish Date:2025/03/25 Views:127 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial