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

How to configure MySQL on Ubuntu 16.04 server and enable remote connection

background I am learning nodejs recently, and I r...

How to add fields to a large data table in MySQL

Preface I believe everyone is familiar with addin...

Four methods of using JS to determine data types

Table of contents Preface 1. typeof 2. instanceof...

How to install PostgreSQL11 on CentOS7

Install PostgreSQL 11 on CentOS 7 PostgreSQL: The...

Detailed steps to configure MySQL remote connection under Alibaba Cloud

Preface As we all know, by default, the MySQL ins...

Vue implements file upload and download

This article example shares the specific code of ...

How to add sudo permissions to a user in Linux environment

sudo configuration file The default configuration...

A brief discussion of four commonly used storage engines in MySQL

Introduction to four commonly used MySQL engines ...

The simplest form implementation of Flexbox layout

Flexible layout (Flexbox) is becoming increasingl...

Is it necessary to create a separate index for the MySQL partition field column?

Preface Everyone knows that the partition field m...

Tutorial on installing Microsoft TrueType fonts on Ubuntu-based distributions

If you open some Microsoft documents with LibreOf...

vue.config.js packaging optimization configuration

The information on Baidu is so diverse that it...