1. Overview
1.概述
In this tutorial, we’ll learn how to restart a single Docker container with Docker Compose.
在本教程中,我们将学习如何使用Docker Compose重启单个Docker容器。
2. Docker Compose restart Command
2.Docker Compose restartCommand
Docker Compose is a tool to manage multiple containers as a single service. However, the Docker Compose CLI includes commands that can apply to a single container. For example, the restart command lets us provide the name of the service we want to restart without affecting the other services that are running:
Docker Compose是一个将多个容器作为单一服务来管理的工具。然而,Docker Compose CLI包括可以适用于单个容器的命令。例如,restart命令让我们提供我们想要重启的服务的名称,而不影响正在运行的其他服务:
docker-compose restart service-name
Before diving into the restart command execution, let’s set up a working environment.
在深入研究重启命令的执行之前,让我们建立一个工作环境。
3. Setup
3.设置
We must have a Docker container to run the Docker Compose commands. We’ll use a previous Baeldung project, spring-cloud-docker, which is a dockerized Spring Boot application. This project has two Docker containers that will help us prove that we can restart a single service without affecting the other.
我们必须有一个Docker容器来运行Docker Compose命令。我们将使用之前的一个Baeldung项目,spring-cloud-docker,这是一个docker化的Spring Boot应用。这个项目有两个Docker容器,这将帮助我们证明我们可以重启一个服务而不影响另一个。
First, we must confirm that we can run both containers by running the following command from the root of the project:
首先,我们必须确认我们可以通过从项目的根部运行以下命令来运行这两个容器。
docker-compose up --detach --build
Now, we should be able to see both services running by executing docker-compose ps:
现在,我们应该能够通过执行docker-compose ps看到两个服务都在运行。
$ docker ps
Name Command State Ports
--------------------------------------------------------------------------------
message-server java -jar /message-server.jar Up 0.0.0.0:18888->8888/tcp
product-server java -jar /product-server.jar Up 0.0.0.0:19999->9999/tcp
Additionally, we could go to localhost:18888 or localhost:19999 in our browser and verify that we see the messages displayed by the application services.
此外,我们可以在浏览器中转到localhost:18888或localhost:19999,并验证我们是否看到应用服务所显示的信息。
4. Restarting a Single Container
4.重启单个容器
Up to this point, we have two containers running and managed as a single service by Docker Compose. Now, let’s see how we can use the restart command to stop and start only one of the two containers.
到此为止,我们有两个容器在运行,并被Docker Compose作为一个服务来管理。现在,让我们看看如何使用restart命令来停止和启动这两个容器中的一个。
First, we’ll look at how we can achieve this without rebuilding the container. However, this solution will not update the service with the latest code. Then, we’ll see another approach where we build the container with the latest code before running.
首先,我们来看看如何在不重建容器的情况下实现这一目标。然而,这种解决方案将不会用最新的代码来更新服务。然后,我们将看到另一种方法,即在运行前用最新代码构建容器。
4.1. Restarting Without Rebuilding
4.1.在不重建的情况下重新启动
With both containers running, we pick one of the services to restart. In this case, we’ll use the message-server container:
在两个容器都运行的情况下,我们选择其中一个服务进行重启。在这种情况下,我们将使用message-server容器。
docker-compose restart message-server
After we run the command in the terminal, we should be able to see the following message:
我们在终端运行该命令后,应该可以看到以下信息。
Restarting message-server ... done
Once the terminal prompts for another command, we can confirm that message-server has been restarted successfully by running the Docker command ps to check the states of all the running processes:
一旦终端提示另一条命令,我们可以通过运行Docker命令ps来检查所有运行进程的状态,确认message-server已经成功重启。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6541d1c4ddf product-server:latest "java -jar /product-…" 10 minutes ago Up 42 seconds 0.0.0.0:19999->9999/tcp product-server
1d07d2a7ed7d message-server:latest "java -jar /message-…" 10 minutes ago Up 15 seconds 0.0.0.0:18888->8888/tcp message-server
Finally, we can determine that the command successfully restarted the message-server container by looking at the STATUS column. We can see how the message-server service has been up and running for a smaller period than the product-server service, which has been up since we ran the docker-compose up command in the previous section.
最后,我们可以通过查看STATUS列来确定该命令成功重启了message-server容器。我们可以看到message-server服务启动和运行的时间小于product-server服务,后者在我们运行上一节中的docker-compose up命令后一直在运行。
4.2. Rebuilding and Restarting
4.2.重建和重新启动
If a container needs to be updated with the latest code, running the restart command is not sufficient since the service needs to build first to pick up the code changes.
如果一个容器需要用最新的代码进行更新,运行restart命令是不够的,因为服务需要首先构建以接收代码的变化。
With both containers running, let’s change the code first to confirm we’ll update the service with the latest code before restarting. In the DockerProductController class, let’s change the return statement to look like this:
在两个容器都运行的情况下,让我们先修改代码,以确认我们将在重启前用最新的代码更新服务。在DockerProductController类中,让我们改变return语句,看起来像这样。
public String getMessage() {
return "This is a brand new product";
}
Now, let’s build the Maven package:
现在,我们来构建Maven包。
mvn clean package
Now, we are ready to restart the product-server container. We can achieve this similarly to how we start the services, but this time by providing the name of the container we want to restart.
现在,我们准备重启product-server容器。我们可以像启动服务那样实现这一目标,但这次是通过提供我们想要重启的容器的名称。
Let’s run the command to rebuild and restart the container:
让我们运行命令来重建和重启容器。
docker-compose up --detach --build product-server
Now, we can verify that the product-server container restarted with the latest code by running docker ps and looking at the output:
现在,我们可以通过运行docker ps和查看输出来验证product-server container是否以最新的代码重新启动。
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
78a4364e75e6 product-server:latest "java -jar /product-…" 6 seconds ago Up 5 seconds 0.0.0.0:19999->9999/tcp product-server
b559f742973b message-server:latest "java -jar /message-…" 22 minutes ago Up 22 minutes 0.0.0.0:18888->8888/tcp message-server
As we can see, the product-server changed both the CREATED value and STATUS value, indicating that the service was rebuilt first and then restarted without any impact on the message-server.
我们可以看到,product-server改变了CREATED值和STATUS值,表明该服务首先被重建,然后被重新启动,对message-server没有任何影响。
Additionally, we can further confirm the code was updated by going to localhost:19999 in our browser and checking that the output is the latest.
此外,我们可以通过在浏览器中访问localhost:19999并检查输出是否是最新的,来进一步确认代码的更新。
5. Conclusion
5.总结
In this article, we’ve learned how to restart a single container using Docker Compose. We covered two ways to achieve this.
在这篇文章中,我们已经学会了如何使用Docker Compose重启单个容器。我们介绍了实现这一目的的两种方法。
First, we used the restart command with the name of the service to restart. Then, we made code changes to demonstrate we could rebuild the latest code and restart a single container without affecting the other.
首先,我们使用restart命令,并加上要重启的服务的名称。然后,我们对代码进行了修改,以证明我们可以重建最新的代码并重启一个容器而不影响其他容器。