Docker container self start implementation method

  • 2020-12-09 01:12:28
  • OfStack

Container self-start

Docker provides the restart policy mechanism that controls the ability of the container to start itself when it exits or when Docker restarts. This Restart policy ensures that the relevant containers start in the correct order. While this can also be done by process monitoring, such as systemd, Docker recommends that you avoid using process monitoring to "self-start" containers.

The Restart policy of Docker differs from the -- ES19en-ES20en startup flag of the dockerd command: -- The -- ES21en-ES22en flag keeps the container running during the Docker upgrade, but the network and user terminal inputs are interrupted.

So what exactly is restart policy? Let's look at the actual situation.

Using restart policy

restart policy is specified by the --restart flag when starting the container using docker run. This flag is optional for multiple value, and different value have different behaviors, as shown in the table below:

Flag Description
no 不自动重启容器. (默认value)
on-failure  容器发生error而退出(容器退出状态不为0)重启容器
unless-stopped  在容器已经stop掉或Docker stoped/restarted的时候才重启容器
always  在容器已经stop掉或Docker stoped/restarted的时候才重启容器

For example: The following command starts an Redis container, and when the Redis container stops or Docker is restarted, the Redis container restarts.


$ docker run -dit --restart unless-stopped redis

Restart policy details

The following details should be noted when using restart policies:

(1) restart policy will take effect only after the container is successfully started. Successful startup here means that the container is at up for at least 10 seconds and is already under docker supervision. This is to prevent containers that do not start successfully from getting stuck in the restart loop.

(2) If the manual (manually) stop(how is it different from the previous explicitly stopped) is 1 container, the container setting restart policy will be ignored unless Docker daemon is restarted or the container is restarted manually. This avoids another endless cycle.

(3)restart policies only for containers, swarm services restart policies has an rejected configuration.

Process monitoring

If restart policies mentioned above does not meet the requirements, you can also adopt a process monitoring management scheme, such as upstart, systemd or supervisor, etc.

In this scenario, the process monitoring service runs in the container. It can monitor whether a process is running or not, and can start a process when it is not running. None of the Docker that occurred was noticed.

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 release.

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

Docker container starts automatically after startup

When starting the container with docker run, use the --restart parameter to set:


# docker run -m 512m --memory-swap 1G -it -p 58080:8080 --restart=alway

 --name bvrfis --volumes-from logdata mytomcat:4.0 /root/run.sh   

--restart Specific parameter values

no - Do not restart the container when it exits; on-failure - Restart the container only when the non-zero state exits; 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 ES146en-ES147en policy. By default, Docker will attempt to restart the container forever.


# sudo docker run --restart=on-failure:10 redis

Related articles: