Docker Container like a system service

1. create and edit a systemd service file – I advise to set a naming convention:

vi /etc/systemd/system/docker.dockerContainerName.service

2. with the following content:

[Unit]
Description=DockerContainerName Container
After=docker.service
Requires=docker.service

[Service]
TimeoutSec=0
Restart=always
ExecStart=/usr/bin/docker start -a dockerContainerName
ExecStop=/usr/bin/docker stop -t 2 dockerContainerName
                      
[Install]
WantedBy=local.target

3. start and enable the service

systemctl start docker.containerName.service
systemctl enable docker.containerName.service

Leave a Reply

Your email address will not be published. Required fields are marked *