CentOS 7: Configure Docker to start on boot and configure containers to auto-start
To set Docker to automatically start on boot on CentOS 7, you need to use the systemctl command. Below are the steps and example code:
- Enable the Docker service
sudo systemctl enable docker
- Start the Docker service
sudo systemctl start docker
To configure a container to automatically start, you need to use the --restart option when running the container. Docker supports the following restart policies:
no: Do not automatically restart the container.on-failure: Only restart the container when it exits abnormally (with a non-zero exit status).always: Always restart the container regardless of its exit status.unless-stopped: Always restart the container unless the user has explicitly stopped it, even if the host machine is rebooted.
For example, to configure a container to automatically start when the Docker daemon starts and always restart it regardless of exit status, you can do the following:
docker run -d --restart always --name mycontainer myimage
In this example, mycontainer is the name you assign to the container, myimage is the name of the image you are using. The -d flag indicates that the container should be run in daemon mode.
This is a discussion topic separated from the original topic at https://juejin.cn/post/7369120920147820579