Docker starts automatically without booting

PHPz
Release: 2023-05-13 13:05:07
Original
739 people have browsed it

Docker is one of the most popular containerization software currently, which can help developers create and run applications more conveniently. But sometimes, you may encounter a problem: Docker will not start automatically after you start your computer. If you want Docker to start automatically, let's take a look at how to set it up.

  1. Prerequisites

Before setting up, you first need to confirm whether your Docker has systemd and daemon.json files installed. Both files are required as they will help Docker start automatically at boot.

If they are not installed on your system, you can use the following command to install them:

sudo apt-get update
sudo apt-get install systemd
Copy after login
  1. Edit the daemon.json file

Enable Docker On systems, you need to edit the /etc/docker/daemon.json file. If the file does not exist, you can create it using the following command:

sudo nano /etc/docker/daemon.json
Copy after login

Then, add the following code to the file:

{
    "default-address-pools":
    [
        {"base":"172.0.0.0/8","size":16},
        {"base":"192.168.0.0/16","size":24}
    ],
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "10m",
        "max-file": "3"
    }
}
Copy after login

This code snippet contains some Docker configuration information. It also contains a default address pool that will be used to assign IP addresses to Docker containers. You can modify it as needed.

  1. Add systemd file

Next, you need to create a systemd file named docker.service and add it to the /etc/systemd/system/ directory middle.

Create a new file using the following command:

sudo nano /etc/systemd/system/docker.service
Copy after login

Add the following code to the file:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target sshd.service
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
EnvironmentFile=-/run/flannel/docker
Restart=always
RestartSec=3
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity

[Install]
WantedBy=multi-user.target
Copy after login

This file will tell systemd how to start Docker and configure some options, as follows:

  • Description: The name of the service
  • Documentation: The link to Docker’s official documentation
  • After: A list of other services that need to be started when the service starts
  • Wants: After some listed services are loaded, the service will be started after a period of delay
  • Type: Service type
  • ExecStart: Service startup command
  • ExecReload: Service reload command
  • EnvironmentFile: Environment variable file loading path
  • Restart: How to restart the service
  • RestartSec: The interval between restarting the service
  • LimitNOFILE: The upper limit of the number of file descriptors that a process can open
  • LimitNPROC: The upper limit of the number of threads that a process can create
  • LimitCORE: The maximum memory limit that a process can use
  1. Reload systemd

After adding the systemd file to the /etc/systemd/system/ directory, you need to reload the systemd process for the configuration to take effect. You can use the following command to reload systemd.

sudo systemctl daemon-reload
Copy after login
  1. Enable systemd

The next step is to enable the Docker service to start automatically. You can enable systemd using the following command.

sudo systemctl enable docker.service
Copy after login

This command will automatically start Docker when booting.

Finally

Now, you have learned how to automatically start the Docker service when the system starts. If you encounter other Docker problems, you can refer to the official Docker documentation or ask for help in the community.

The above is the detailed content of Docker starts automatically without booting. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template