1. Overview
1.概述
In this tutorial, we’ll see how to rebuild a container independently from the others with docker-compose.
在本教程中,我们将看到如何使用docker-compose独立于其他容器重建一个容器。
2. Presentation of the Problem
2.问题的提出
Let’s define a docker-compose.yml configuration file with two containers: One will refer to the latest ubuntu image and the other one to the latest alpine image. We’ll add pseudo-terminals for each with “tty: true” to prevent the containers from exiting directly on launch:
让我们定义一个docker-compose.yml配置文件,里面有两个容器。一个指的是最新的ubuntu镜像,另一个是最新的alpine镜像。我们将为每个容器添加带有”tty: true“的伪终端,以防止这些容器在启动时直接退出。
version: "3.9"
services:
ubuntu:
image: "ubuntu:latest"
tty: true
alpine:
image: "alpine:latest"
tty: true
Let’s now build the containers and start them. We’ll use the docker-compose up command with the -d option to let them run in the background:
现在让我们构建容器并启动它们。我们将使用docker-compose up命令和-d选项来让它们在后台运行。
$ docker-compose up -d
Container {folder-name}-alpine-1 Creating
Container {folder-name}-ubuntu-1 Creating
Container {folder-name}-ubuntu-1 Created
Container {folder-name}-alpine-1 Created
Container {folder-name}-ubuntu-1 Starting
Container {folder-name}-alpine-1 Starting
Container {folder-name}-alpine-1 Started
Container {folder-name}-ubuntu-1 Started
We can quickly check that our containers are running as expected:
我们可以快速检查我们的容器是否按预期运行。
$ docker-compose ps
NAME COMMAND SERVICE STATUS PORTS
{folder-name}-alpine-1 "/bin/sh" alpine running
{folder-name}-ubuntu-1 "bash" ubuntu running
We’ll now see how we can rebuild and restart the ubuntu container without impacting the alpine container.
我们现在将看到我们如何在不影响ubuntu容器的情况下重建和重启alpine容器。
3. Rebuild and Restart a Container Independently
3.独立地重建和重启一个容器
Adding the name of the container to the docker-compose up command will do the trick. We’ll add the build option to build the image before starting the container. We’ll also add the force-recreate flag because we haven’t changed the image:
在docker-compose up命令中添加容器的名字就可以了。我们将添加build选项,在启动容器之前构建镜像。我们还将添加force-recreate标志,因为我们还没有改变镜像。
$ docker-compose up -d --force-recreate --build ubuntu
Container {folder-name}-ubuntu-1 Recreate
Container {folder-name}-ubuntu-1 Recreated
Container {folder-name}-ubuntu-1 Starting
Container {folder-name}-ubuntu-1 Started
As we can see, the ubuntu container was rebuilt and restarted without any impact on the alpine container.
正如我们所看到的,ubuntu容器被重建并重新启动,对alpine容器没有任何影响。
4. If the Container Depends on Another Container
4.如果该容器依赖于另一个容器
Let’s now slightly update our docker-compose.yml file to make the ubuntu container depend on the alpine one:
现在让我们稍微更新一下我们的docker-compose.yml文件,使ubuntu容器依赖于alpine的容器。
version: "3.9"
services:
ubuntu:
image: "ubuntu:latest"
tty: true
depends_on:
- "alpine"
alpine:
image: "alpine:latest"
tty: true
We’ll stop the previous containers and rebuild them from scratch with the new configuration:
我们将停止以前的容器,然后用新的配置从头开始重建它们。
$ docker-compose stop
Container {folder-name}-alpine-1 Stopping
Container {folder-name}-ubuntu-1 Stopping
Container {folder-name}-ubuntu-1 Stopped
Container {folder-name}-alpine-1 Stopped
$ docker-compose up -d
Container {folder-name}-alpine-1 Created
Container {folder-name}-ubuntu-1 Recreate
Container {folder-name}-ubuntu-1 Recreated
Container {folder-name}-alpine-1 Starting
Container {folder-name}-alpine-1 Started
Container {folder-name}-ubuntu-1 Starting
Container {folder-name}-ubuntu-1 Started
In this case, we need to add the no-deps option to explicitly tell docker-compose not to restart linked containers:
在这种情况下,我们需要添加no-deps选项,明确告诉docker-compose不要重新启动链接的容器。
$ docker-compose up -d --force-recreate --build --no-deps ubuntu
Container {folder-name}-ubuntu-1 Recreate
Container {folder-name}-ubuntu-1 Recreated
Container {folder-name}-ubuntu-1 Starting
Container {folder-name}-ubuntu-1 Started
5. Conclusion
5.总结
In this tutorial, we’ve seen how to rebuild a container with docker-compose.
在本教程中,我们已经看到了如何用docker-compose重建一个容器。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。