迹忆客 EN >

当前位置:主页 > 学无止境 > 操作系统 > Docker >

在 Docker 中强制干净构建映像

作者:迹忆客 最近更新:2023/03/22 浏览次数:

本文将讨论并演示如何在 Docker 中强制干净构建映像。

在 Docker 中构建镜像

我们将使用一个简单的 Flask 应用程序来演示这个概念。使用下面的命令,在主文件夹 my-app 中创建一个名为 app.py 的简单应用程序。

touch app.py

如果你没有使用过该命令,请安装 Python3。

sudo apt update && sudo apt install python3-venv

我们也可以使用命令安装 Flask 和 Gunicorn。

pip install flask gunicorn

继续使用命令 touch Dockerfile 创建一个 docker 文件。为了让我们更轻松地创建 docker 文件,我们创建了一个 requirements.txt 文件。

我们可以通过运行 pip freeze > requirements.txt 命令创建一个 requirements.txt 文件。完成后,你的文件结构应与以下类似。

容器化 Docker 应用程序的文件结构

我们可以继续到 app.py 文件并创建我们的应用程序。该应用程序包含一个打印控制台消息的简单函数,如下所示。

from flask import Flask

app = Flask(__name__)

def hello():

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

hello()

这是我们执行上面的代码时所期望的结果。

执行我们的 Flask 应用程序时的代码结果

现在我们将使用 Docker 用于构建映像的命令填充 Dockerfile。这些命令将在我们运行 docker 容器时执行。

在这种情况下,我们将使用 Python 作为我们的基础镜像。Dockerfile 应该如下所示。

# 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

一旦我们准备好 Dockerfile,我们就可以保存它并在本地构建我们的镜像以进行测试。我们可以在 -t 标签旁边使用 docker build 命令。

在 Docker 中构建镜像

然后,我们可以使用 docker run 命令测试映像,如下所示。

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

我们将确认我们已经使用 docker ps 命令成功构建了一个新容器。

$ 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

在 Docker 中使用 --no-cache 选项强制干净构建映像

使用这个镜像构建了一个容器后,假设我们想使用同一个镜像再次构建一个镜像。在这种情况下,docker 守护进程将使用镜像层的现有缓存来构建镜像。

但是,我们可以使用 --no-cache 选项强制在 Docker 中构建干净的映像。这会覆盖 docker 守护程序的默认行为。

以下是我们如何实现它。

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

我们可以使用 docker run 命令创建一个新容器。

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

如下所示,我们已经成功构建了一个新容器并强制 Docker 使用 --no-cache 选项来干净构建映像。

$ 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Solution to incorrect access log time when deploying Nginx in Docker

发布时间:2025/03/26 浏览次数:165 分类:Docker

In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t

Docker deploys nginx php application

发布时间:2025/03/26 浏览次数:131 分类: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

发布时间:2025/03/26 浏览次数:107 分类: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

发布时间:2025/03/26 浏览次数:97 分类: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

发布时间:2025/03/26 浏览次数:124 分类: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

发布时间:2025/03/26 浏览次数:202 分类: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

Enforce clean build of images in Docker

发布时间:2025/03/26 浏览次数:87 分类:Docker

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-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

发布时间:2025/03/26 浏览次数:140 分类: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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便