1. Overview
1.概述
In this tutorial, we’ll learn how to use the restart policy with Docker Compose.
在本教程中,我们将学习如何在Docker Compose中使用重新启动策略。
First, we’ll cover how to restart Docker containers with restart policies. Then we’ll cover how Docker Compose defines restart policies in normal mode and swarm mode as a configuration for multi-container Docker applications.
首先,我们将介绍如何用重启策略重启Docker容器。然后,我们将介绍Docker Compose如何在正常模式和Swarm模式下定义重启策略,作为多容器Docker应用的配置。
2. Docker Restart Policy
Docker重启政策
Restart policies are strategies we can use to restart Docker containers automatically and manage their lifecycles.
重启策略是我们可以用来自动重启Docker容器并管理其生命周期的策略。
Given that containers can fail unexpectedly, Docker has safeguards to prevent services from running into a restart loop. In case of a failure, restart policies don’t take effect unless the container runs successfully for at least 10 seconds.
鉴于容器可能会出现意外故障,Docker有保障措施来防止服务进入重启循环。在发生故障的情况下,除非容器成功运行至少10秒,否则重启策略不会生效。
We can also assume that manually stopping a container will make Docker restart the service automatically when a restart policy is provided. However, these policies are dismissed in this case to prevent containers from restarting after being stopped arbitrarily.
我们也可以认为,当提供重启策略时,手动停止容器会使Docker自动重启服务。然而,在这种情况下,这些策略被驳回,以防止容器在被任意停止后重新启动。
To use restart policies, Docker provides the following options:
要使用重启策略,Docker提供了以下选项。
- no: Containers won’t restart automatically.
- on-failure[:max-retries]: Restart the container if it exits with a non-zero exit code, and provide a maximum number of attempts for the Docker daemon to restart the container.
- always: Always restart the container if it stops.
- unless-stopped: Always restart the container unless it was stopped arbitrarily, or by the Docker daemon.
Now let’s look at an example of how to set a restart policy using the Docker CLI for a single container:
现在我们来看一个例子,如何使用Docker CLI为单个容器设置重启策略。
docker run --restart always my-service
From the example above, my-service will always restart if the container stops running. However, if we explicitly stop the container, the restart policy will only take effect when the Docker daemon restarts, or when we use the restart command.
从上面的例子来看,如果容器停止运行,my-service将始终重新启动。但是,如果我们显式地停止容器,那么重启策略将只在 Docker 守护程序重新启动时生效,或者在我们使用restart命令时生效。
The previous example demonstrates how the restart flag configures the strategy to restart a single container automatically. However, Docker Compose allows us to configure restart policies to manage multiple containers by using the restart or restart_policy properties in normal mode or swarm mode.
前面的例子演示了restart标志如何配置策略以自动重启单个容器。然而,Docker Compose允许我们通过使用restart 或restart_policy 属性在正常模式或swarm模式下配置重启策略来管理多个容器。
3. Setup
3.设置
Before diving into the restart policies implementation with Docker Compose, let’s set up a working environment.
在深入了解Docker Compose的重启策略实现之前,让我们先建立一个工作环境。
We must have a running Docker container to test the restart policies we’ll specify. We’ll use a project from a previous article, spring-cloud-docker, which is a dockerized Spring Boot application. This project has two Docker services that we’ll use to implement different restart strategies with Docker Compose.
我们必须有一个正在运行的Docker容器来测试我们将要指定的重启策略。我们将使用之前文章中的一个项目,spring-cloud-docker,它是一个docker化的Spring Boot应用。这个项目有两个Docker服务,我们将用Docker Compose来实现不同的重启策略。
First, we must confirm that we can run both containers by running the following command from the project root:
首先,我们必须确认我们可以通过从项目根部运行以下命令来运行两个容器。
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
Alternatively, 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. Restart Policy in Docker Compose
4.Docker Compose中的重启策略
Like the restart Docker command, Docker Compose includes the restart property to restart containers automatically.
与restart Docker命令一样,Docker Compose包括restart属性以自动重新启动容器。
We can also define restart policies in Docker Compose by providing the restart property to the service in the docker-compose.yml file. Docker Compose uses the same values provided by the Docker CLI restart command to define a restart strategy.
我们还可以通过在docker-compose.yml文件中为服务提供restart属性来在Docker Compose中定义重启策略。Docker Compose使用Docker CLI restart命令所提供的相同值来定义重启策略。
Now let’s create a restart policy for our containers. In the spring-cloud-docker project, we must change the docker-compose.yml configuration file by adding the restart policy property:
现在让我们为我们的容器创建一个重启策略。在spring-cloud-docker项目中,我们必须改变docker-compose.yml配置文件,加入重启策略属性。
message-server:
container_name: message-server
build:
context: docker-message-server
dockerfile: Dockerfile
image: message-server:latest
ports:
- 18888:8888
networks:
- spring-cloud-network
restart: no
product-server:
container_name: product-server
build:
context: docker-product-server
dockerfile: Dockerfile
image: product-server:latest
ports:
- 19999:9999
networks:
- spring-cloud-network
restart: on-failure
Notice how we added the restart property to both services. In this case, the message-server will never restart automatically. The product-server will only restart if it exits with a non-zero code as specified by the on-failure value.
注意我们是如何为这两个服务添加restart属性的。在这种情况下,消息-服务器将永远不会自动重启。产品服务器只有在以失败时值所指定的非零代码退出时才会重新启动。
Next, let’s see how the same policies are declared using Docker Compose in swarm mode.
接下来,让我们看看如何使用Docker Compose在swarm模式下声明同样的策略。
5. Restart Policy in Docker Compose Swarm Mode
5.在Docker Compose Swarm模式下重启策略
Docker Compose in swarm mode provides a larger set of options when specifying how containers will restart automatically. However, the following implementation only works in Docker Compose v3, which introduces the deploy key-value pair in the configuration.
在指定容器如何自动重启时,Swarm模式下的Docker Compose提供了一组更大的选项。然而,下面的实现只适用于Docker Compose v3,它在配置中引入了deploy键值对。
Below we can find the different properties to further expand the configuration for restart policies in swarm mode:
下面我们可以找到不同的属性,以进一步扩展蜂群模式下重启策略的配置。
- condition: none, on-failure, or any (default)
- delay: Duration between restart attempts
- max_attempts: Maximum number of attempts outside of the restart window
- window: Duration for determining if a restart is successful
Now let’s define our restart policies. First, we must make sure we’re using Docker Compose v3 by changing the version property:
现在让我们来定义我们的重启策略。首先,我们必须通过改变version 属性来确保我们正在使用Docker Compose v3。
version: '3'
Once we change the version, we can add the restart_policy property to our services. Similar to the previous section, our message-server container will always restart automatically by providing the any value in the condition:
一旦我们改变了版本,我们就可以为我们的服务添加restart_policy属性。与上一节类似,我们的message-server容器将通过在condition中提供any值而始终自动重启。
deploy:
restart_policy:
condition: any
delay: 5s
max_attempts: 3
window: 120s
Similarly, we’ll add an on-failure restart policy to the product-server:
同样地,我们将为产品服务器添加一个故障时重启策略。
deploy:
restart_policy:
condition: on-failure
delay: 3s
max_attempts: 5
window: 60s
Notice how the restart_policy property is within the deploy key, which indicates that restart policies are a deployment configuration we provide to manage a cluster of containers in swarm mode.
注意到restart_policy 属性如何在deploy 键内,这表明重启策略是我们为管理swarm模式下的容器集群提供的部署配置。
Also, the restart policies in both services include the additional configuration metadata that makes the policies a more robust restart strategy for the containers.
另外,这两个服务中的重启策略包括额外的配置元数据,使策略成为容器的更强大的重启策略。
6. Conclusion
6.结语
In this article, we learned how to define restart policies with Docker Compose. After introducing Docker restart policies, we used a previous Baeldung project to demonstrate two ways to configure a restart strategy for containers.
在这篇文章中,我们学习了如何用Docker Compose定义重启策略。在介绍完Docker重启策略后,我们用之前的Baeldung项目演示了两种为容器配置重启策略的方法。
First, we used the Docker Compose restart property configuration, which includes the same options as the native restart command in the Docker CLI. Then we used the restart_policy property, only available in swarm mode and Docker Compose version 3, along with other configuration values that define restart policies.
首先,我们使用了Docker Compose的restart属性配置,其中包括与Docker CLI中的本地restart命令相同的选项。然后,我们使用了restart_policy 属性(仅在swarm模式和Docker Compose第3版中可用),以及其他定义重启策略的配置值。
As always, all the sample code used in this article is available over on GitHub.
一如既往,本文中所使用的所有示例代码都可以在GitHub上找到。