Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to self-start the Docker container

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how to self-start the Docker container, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

Container self-start

Docker provides a restart policy mechanism to control the container to start itself when the container exits or Docker restarts. This Restart policy ensures that the relevant containers start in the correct order. Although this can also be done through process monitoring (such as systemd), Docker recommends that you avoid using process monitoring to "self-start" the container.

The Restart policy of Docker differs from the live-restore startup flag of the dockerd command: the live-restore flag ensures that the container continues to run when Docker is upgraded, but the network and user terminal input will be interrupted.

So what on earth is restart policy? Let's take a look at the actual situation.

Use restart policy

Restart policy is specified by the-- restart flag when launching the container using docker run. This flag has multiple value options, and different value have different behaviors, as listed in the following table:

FlagDescriptionno does not restart the container automatically. (default value) on-failure container exits when error occurs (container exit status is not 0) restart container unless-stopped only restarts container when container has stop or Docker stoped/restarted, always restarts container when container has stop or Docker stoped/restarted

For example: the following command starts a Redis container, and when the Redis container is stopped or Docker is restarted, the Redis container will restart.

$docker run-dit-restart unless-stopped redis

Restart policy details

You need to pay attention to the following details when using restart policies:

(1) the restart policy will not take effect until the container is started successfully. In this case, "successful startup" means that the container has been in up for at least 10 seconds and has been under docker supervision. This is to prevent containers that do not start successfully from falling into the dead loop of restart.

(2) if the manual (manually) stop (how is it different from the previous explicitly stopped) is a container, the restart policy set by the container will be ignored unless the Docker daemon is restarted or the container is manually restarted. This avoids another kind of endless cycle.

(3) restart policies can only be used in containers. There is a failed configuration for swarm services's restart policies.

Process monitoring

If the restart policies mentioned above does not meet the requirements, you can also use a process monitoring management solution, such as upstart,systemd or supervisor, and so on.

In this scenario, the process monitoring service runs in the container. It can monitor whether a process is running or not, and can start it when it is not running. And Docker was unconscious of what was happening.

Docker does not recommend this method for process monitoring for the simple reason that it is related to the system platform or even the linux distribution.

Original: https://docs.docker.com/engine/admin/start-containers-automatically/#use-a-process-manager

Docker Container Boot starts automatically

When using docker run to start the container, use the-- restart parameter to set:

# docker run-m 512m-- memory-swap 1G-it-p 58080 it 8080-- restart=alway-- name bvrfis-- volumes-from logdata mytomcat:4.0 / root/run.sh

-- details of specific parameter values of restart:

No-do not restart the container when it exits

On-failure-restart the container only if you exit in a non-zero state

Always-restart the container regardless of the exit status

You can also specify the maximum number of times Docker will attempt to restart the container when using the on-failure policy. By default, Docker attempts to restart the container forever.

# sudo docker run-restart=on-failure:10 redis has shared here on how to start the Docker container. I hope the above content can be of some help and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report