Start docker automatically as a systemd service

Docker Compose Example

copy and paste the folowing text into a new file called ~/script.sh

#!/bin/bash
SERVICENAME=$(basename $(pwd))

# Create systemd service file
cat | sudo tee /etc/systemd/system/$SERVICENAME.service << EOF
[Unit]
Description=$SERVICENAME
Requires=docker.service
After=docker.service

[Service]
Restart=always
User=root
Group=docker
WorkingDirectory=$(pwd)
ExecStartPre=$(which docker) compose -f docker-compose.yml down
ExecStart=$(which docker) compose -f docker-compose.yml up
ExecStop=$(which docker) compose -f docker-compose.yml down

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl enable $SERVICENAME.service
sudo systemctl start $SERVICENAME.service
chmod +x ~/script.sh

go to the folder where your docker-compose.yml file is,

sudo bash ~/script.sh

External Resources

edited from here