How to implement Docker container self-start

How to implement Docker container self-start

Container auto-start

Docker provides a restart policy mechanism that can control the container to automatically start when the container exits or Docker is restarted. This Restart policy ensures that related containers are started in the correct order. Although this action can also be accomplished through process monitoring (such as systemd), Docker still recommends avoiding the use of process monitoring to "self-start" containers.

There is a difference between Docker's Restart policy and 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 exactly is a restart policy? Let's take a look at the actual situation.

Using restart policy

The restart policy is specified by the --restart flag when starting the container using docker run. This flag has multiple values ​​​​to choose from, and different values ​​​​have different behaviors, as listed in the following table:

Flag Description
no Do not automatically restart the container. (Default value)
on-failure The container exits with an error (the container exit status is not 0) and restarts the container
unless-stopped Restart the container only when the container has been stopped or Docker is stopped/restarted
always Restart the container only when the container has been stopped or Docker is stopped/restarted

For example: The following command starts a Redis container. When the Redis container is stopped or Docker is restarted, the Redis container will be restarted.

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

Restart policy details

When using restart policies, you need to pay attention to the following details:

(1) The restart policy takes effect only after the container is successfully started. "Successful start" here means that the container has been up for at least 10 seconds and has been supervised by Docker. This is to prevent containers that have not been successfully started from falling into an endless loop of restarting.

(2) If you manually stop a container (what is the difference from explicitly stopped above), the restart policy set for the container will be ignored unless the Docker daemon is restarted or the container is manually restarted. This avoids another vicious cycle.

(3) Restart policies can only be used for containers. For swarm services, restart policies have invalid configurations.

Process Monitoring

If the restart policies mentioned above cannot meet your needs, you can also use process monitoring management solutions, such as upstart, systemd or supervisor, etc.

In this solution, the process monitoring service runs in the container. It can monitor whether a process is running and can start the process if it is not running. Docker is completely unaware of all this happening.

Docker does not recommend this method for process monitoring. The reason is simple. This method is related to the system platform and even the Linux distribution.

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

Docker container automatically starts at boot

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 value details:

  • no - when the container exits, do not restart the container;
  • on-failure - restart the container only if it exits with a non-zero status;
  • 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 strategy. By default, Docker will try to restart containers forever.

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • 6 solutions for network failure in Docker container
  • Example of how to create and run multiple MySQL containers in Docker
  • How to isolate users in docker containers
  • Detailed explanation of importing/exporting MySQL data in Docker container
  • Docker container uses Jenkins to deploy web projects (summary)
  • Method for accessing independent IP in Docker container
  • Detailed explanation of migrating local Docker containers to the server
  • How to use Mysql in Tomcat container under Docker
  • How to install and uninstall docker application container engine under Centos7
  • Add port mapping after docker container starts
  • Detailed explanation of Docker port mapping and container interconnection
  • Docker Tutorial: Using Containers (Simple Example)

<<:  Problems and solutions for MYSQL5.7.17 connection failure under MAC

>>:  JS implements click drop effect

Recommend

Disable input text box input implementation properties

Today I want to summarize several very useful HTML...

Notes on the MySQL database backup process

Today I looked at some things related to data bac...

How to deploy Tencent Cloud Server from scratch

Since this is my first post, if there are any mis...

Detailed explanation of computed properties in Vue

Table of contents Interpolation Expressions metho...

Several ways to use v-bind binding with Class and Style in Vue

Adding/removing classes to elements is a very com...

How to use yum to configure lnmp environment in CentOS7.6 system

1. Installation version details Server: MariaDB S...

jQuery implements navigation bar effect with expansion animation

I designed and customized a navigation bar with a...

How to deploy Vue project using Docker image + nginx

1. Packaging Vue project Enter the following name...

HTML+CSS makes div tag add delete icon in the upper right corner sample code

1. Requirements description Display the delete ic...

Summary of MySQL lock knowledge points

The concept of lock ①. Lock, in real life, is a t...

In-depth understanding of HTML form input monitoring

Today I saw a blog post about input events, and o...

A brief analysis of the four import methods and priorities in CSS

First: 4 ways to introduce CSS There are four way...

Simple usage example of MySQL 8.0 recursive query

Preface This article uses the new features of MyS...