使用 Docker-Compose 从新镜像重新创建容器
在开发应用程序时,我们通常会进行更改或添加更多功能,以使应用程序对与应用程序交互的不同用户更有效。
使用 Docker 时,我们需要通过重建映像并从此映像运行新容器来确保所做的更改或功能更新到主应用程序。 本文将教我们如何使用 Docker Compose 从新图像重新创建容器。
创建一个新项目
在 WebStorm IDEA 中,选择 File > New > Project 创建一个新项目。 然后选择 Empty Project 并将项目名称从 untitled 更改为 docker-compose-no-cache 或使用任何首选名称。
按标有“创建”的按钮生成项目。 在当前文件夹下创建一个名为 index.html 的文件,复制粘贴以下代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi"
crossorigin="anonymous">
</head>
<body>
<div class="card">
<div class="card-header">
Featured
</div>
<div class="card-body">
<h5 class="card-title">Special title treatment</h5>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<a href="#" class="btn btn-primary">Go somewhere</a>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3"
crossorigin="anonymous"></script>
</body>
</html>
当我们从应用程序的图像运行容器时,该文件使用 Bootstrap 创建一个显示卡片的网页。 我们将使用此页面来验证我们的容器是否正在运行。
定义镜像
在当前文件夹下创建一个名为 Dockerfile 的文件,并将以下说明复制并粘贴到该文件中。
FROM nginx:1.22.0-alpine
COPY . /usr/share/nginx/html
- FROM - 设置基础图像,使用后续指令在其上创建我们的自定义图像。 在本例中,我们将基础镜像设置为 Nginx,并使用 alpine 拉取 Nginx 的轻量级版本。
- COPY - 将当前目录中的文件和文件夹复制到我们映像中的文件系统位置。 在这种情况下,我们已经将当前目录中的所有文件复制到/src/share/nginx/html。
使用 Compose 文件定义容器
在当前文件夹下创建一个名为 compose.yaml 的文件,并将以下说明复制并粘贴到该文件中。
services:
web-app:
restart: on-failure
build: ./
hostname: web-app-service
ports:
- '80:80'
该文件定义了一个名为 web-app 的服务,该服务在主机中公开 80 端口以监听容器中的 80 端口。 该文件提供了一种比终端上的容器更容易管理容器的方法。
构建镜像并运行容器
由于这是我们第一次构建图像,我们的基础图像将被拉取并用于使用 Dockerfile 中定义的指令创建我们的自定义图像。 compose.yaml 文件中定义的容器也将被创建并添加到网络中。
在您的计算机上使用键盘快捷键 ALT+F12 打开一个新的终端窗口,并执行下面的命令来构建一个图像并从中运行一个容器。
~/WebstormProjects/docker-compose-no-cache$ docker compose up -d
此命令执行 compose.yaml 文件以构建名为 docker-compose-no-cache_web-app 的图像,并运行名为 docker-compose-no-cache-web-app-1 的容器,如下所示。
=> CACHED [1/2] FROM docker.io/library/nginx:1.22.0-alpine@sha256:addd3bf05ec3c69ef3e8f0021ce1ca98e0eb21117b97ab8b64127e 0.0s
=> [2/2] COPY . /usr/share/nginx/html 0.6s
=> exporting to image 0.8s
=> => exporting layers 0.6s
=> => writing image sha256:d72675b7a3e3a52dd27fe46f298dc30757382d837a5fbf36d8e36d646b5902d6 0.1s
=> => naming to docker.io/library/docker-compose-no-cache_web-app 0.1s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/2
⠿ Network docker-compose-no-cache_default Created 0.3s
⠿ Container docker-compose-no-cache-web-app-1 Started
要验证我们的容器是否正在运行,请打开浏览器并向 localhost:80 (http://localhost/#) 发出请求。 我们在 index.html 页面中定义的卡片将显示在浏览器上。
使用相同的终端窗口,执行以下命令以验证我们的自定义图像是否已创建。
~/WebstormProjects/docker-compose-no-cache$ docker image ls
输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-compose-no-cache_web-app latest d72675b7a3e3 8 minutes ago 23.5MB
使用缓存重建镜像
要使用缓存和运行容器重建现有图像,我们首先需要使用命令 docker compose down 停止并删除现有容器。
使用缓存重建 docker 镜像意味着当前容器将被重用以创建新容器。 使用相同的终端窗口,执行以下命令以使用缓存重建现有图像。
~/WebstormProjects/docker-compose-no-cache$ docker compose build
此命令使用现有映像重建新映像,我们可以使用 docker compose up -d
从中运行容器。
要验证现有镜像是否被重新用于创建新容器,请执行以下命令并注意除了上一节中创建的镜像之外没有其他镜像。
~/WebstormProjects/docker-compose-no-cache$ docker image ls
输出:
REPOSITORY TAG IMAGE ID CREATED SIZE
docker-compose-no-cache_web-app latest d72675b7a3e3 8 minutes ago 23.5MB
重建没有缓存的镜像
重建没有缓存的图像意味着创建一个新图像而不重用以前的图像。 我们可以通过在使用 docker build
命令构建镜像时添加 --no-cache 命令来实现这一点。
使用相同的终端窗口,执行以下命令重建没有缓存的图像。 确保使用 docker compose down
命令删除正在运行的容器。
~/WebstormProjects/docker-compose-no-cache$ docker compose build --no-cache
此命令重建一个没有缓存的新图像,这意味着不会重复使用以前的图像。 要验证之前的图像没有被重新用于创建当前图像,请执行以下命令并注意之前的图像没有名称,如 <none>
所示。
新图像使用先前图像的名称。
~/WebstormProjects/docker-compose-no-cache$ docker image ls
输出:
docker-compose-no-cache_web-app latest f111dde0e704 3 seconds ago 23.5MB
<none> <none> 1c05a46fe049 13 minutes ago 23.5MB
由于上一张图片和新图片不能重名,所以当前图片使用上一张图片的名字,上一张图片得到none表示没有名字。
总结
我们已经学习了如何使用 Docker Compose 从新图像重新创建容器。 为了实现这一点,我们学习了如何使用缓存重建图像以及如何在没有缓存的情况下重建镜像。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
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