How to use Docker for container failure recovery and automatic restart

WBOY
Release: 2023-11-07 16:28:47
Original
1119 people have browsed it

How to use Docker for container failure recovery and automatic restart

Docker, as a lightweight virtualization platform based on container technology, has been widely used in various scenarios. In a production environment, high availability and automatic failure recovery of containers are crucial. This article will introduce how to use Docker for container failure recovery and automatic restart, including specific code examples.

1. Configuration of automatic container restart

In Docker, the automatic restart function of the container can be enabled by using the --restart option when running the container. Common options are:

  • no: Do ​​not automatically restart. Default option;
  • always: always automatically restart;
  • on-failure: automatically restart only when the container exits due to non-0 status;
  • unless-stopped: unless Stop manually, otherwise it always restarts automatically.

The following is an example of enabling automatic container restart by using the --restart option:

docker run -d --restart always nginx

In this example , we started a Docker container named nginx and configured the container to always restart automatically through the --restart option.

It should be noted that the --restart option will only take effect when the container exits due to failure. If a container is stopped manually, it will not be restarted automatically. If you still want to enable automatic restart after the container is manually stopped, you can use the unless-stopped option.

2. Configuration of container failure recovery

In Docker, container failure recovery usually refers to using cluster management tools such as Docker Swarm to automatically reschedule containers to ensure service availability. Here is an example that demonstrates how to configure automatic failover in Docker Swarm:

  1. Create a Docker Swarm cluster:

docker swarm init

  1. Create a service in the cluster:

docker service create --name nginx --replicas 3 nginx

In this example, we create a service named nginx , and set its number of copies to 3.

  1. Enable failure recovery in the service:

docker service update --update-delay 10s --update-parallelism 2 --update-failure-action restart nginx

The --update-delay option here specifies the delay time between update operations; the --update-parallelism option specifies the number of concurrent instances for each update; the --update-failure-action option specifies The action to take when the update fails, here we set it to restart the container.

It should be noted that the fault recovery function can only take effect when using cluster management tools such as Docker Swarm. If you use the docker run command directly to start the container, then we can only use the --restart option to automatically restart the container.

3. Code example of container failure recovery and automatic restart

The following is a complete code example that demonstrates how to implement container failure by using the --restart option and cluster management tools such as Docker Swarm. Recovery and automatic restart functions:

  1. Create a Docker Swarm cluster named docker-demo:

docker swarm init --advertise-addr 127.0.0.1

  1. Create a service named nginx in the cluster and set its number of replicas to 3:

docker service create --name nginx --replicas 3 nginx

  1. Enable failure recovery in the service:

docker service update --update-delay 10s --update-parallelism 2 --update-failure-action restart nginx

  1. After waiting for a period of time, manually stop a container:

docker container stop

  1. After waiting for a period of time, view the container Whether it is automatically restarted:

docker container ls

If the container is automatically restarted, its status should be running.

It should be noted that the specific implementation methods of container failure recovery and automatic restart are different, and different scenarios require different methods to be implemented. The above examples are for reference only, and the specific implementation needs to be adjusted according to the actual situation.

Summary

Container failure recovery and automatic restart are important means to ensure the high availability of Docker containers. By correctly configuring Docker's automatic restart and failure recovery functions, you can effectively reduce the service interruption time caused by container failure. This article describes how to use the --restart option and cluster management tools such as Docker Swarm to implement container failure recovery and automatic restart functions, and provides specific code examples. I hope this article can be helpful to everyone when using Docker.

The above is the detailed content of How to use Docker for container failure recovery and automatic restart. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!