Automating Your Docker Setup with Systemd

· 3 min read
Automating Your Docker Setup with Systemd
Photo by Ian Taylor / Unsplash

This guide walks you through creating a systemd service to automate the startup and shutdown of your Docker Compose setup. This is a common and recommended approach for ensuring your containers consistently run and for easy management.

Prerequisites:

  • Basic familiarity with Docker, Docker Compose, and Systemd.
  • Docker installed and running on your system.

Create the Systemd Service File

We’ll create a service file named compose.service within the /etc/systemd/system/ directory. You can use a text editor like nanoto create and modify this file.

sudo nano /etc/systemd/system/compose.service

Paste the following content into the file:

[Unit]
Description=Starts the provisioned docker compose setup

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/slothiesmooth/compose
ExecStart=docker compose up -d
ExecStop=docker compose down
TimeoutStartSec=0

[Install]
WantedBy=multi-user.target

Important Note: The WorkingDirectory is set to /home/slothiesmooth/compose. Make sure this directory exists and contains your docker-compose.yml file.

The ExecStart command in the service file uses docker compose up -d to start your containers in detached mode. However, if the binary isn't in the default location, you’ll need to update this command.

Verify the path of the binary using the whereis command:

whereis docker

If the output shows a different path (e.g., /usr/local/bin/docker), update the ExecStart line in your compose.service file accordingly:

ExecStart=/usr/local/bin/docker compose up -d

Enable and Start the Service:

Now, let’s configure systemd to manage the service:

  • Reload Systemd:
sudo systemctl daemon-reload
  • Enable the Service:
sudo systemctl enable compose.service
  • Start the Service:
sudo systemctl start compose.service
  • Verify Service Status:
sudo systemctl status compose.service

Docker Restart Policies

To ensure your containers restart automatically, configure restart policies within your docker-compose.yml file. Add one of the following to each service definition:

  • restart: unless-stopped : This tells Docker to restart the container unless it was explicitly stopped.
  • restart: always : This tells Docker to restart the container whenever it stops, regardless of the last state. e.g., the container will start right after you stop it or it stops due to an error.

Disclaimer: It should go without saying that this setup is not production-ready and that this is merely a basic guide to get you started. You can always expand on it and make it more efficient. e.g., better error handling, cron jobs to update Docker images, etc.


Troubleshooting Common Issues

This section provides solutions to common problems you might encounter when using the Systemd service to manage your Docker Compose setup.

1. Service Doesn’t Start at All

  • Issue: The service simply fails to start, with no output or error messages.
  • Possible Causes & Solutions:
    • Syntax Errors in compose.service: Systemd is very strict about the syntax of service files.
      • Carefully review your compose.service file for typos, missing colons, incorrect indentation, or other errors.
      • Use a text editor with syntax highlighting to help identify errors. Consider using systemd-analyze verify compose.service to check the service file for errors.
    • Systemd Daemon Not Reloaded: The service file may not be recognized by Systemd.
      • Verify that systemd-analyze verify compose.service doesn’t report errors.
      • Run sudo systemctl daemon-reload to reload systemd’s configuration.

2. Containers Don't Start After Service Starts

  • Issue: The service starts successfully, but your Docker Compose containers don’t launch.
  • Possible Causes & Solutions:
    • Errors Within docker-compose.yml: The root cause is almost always an issue within your docker-compose.yml file.
      • Examine the logs from your containers. These logs will often reveal the specific error causing the container to fail to start.
      • Carefully review your docker-compose.yml file for syntax errors, incorrect environment variables, network configuration problems, or missing dependencies.
    • Network Issues: Containers might not be able to communicate with each other or the outside world.
      • Check the container logs for network-related errors. Use docker exec -it <container_id> bash to enter the container and run ping <other_container_ip> or ping google.com.
      • Verify that your network configuration (DNS, subnet masks, gateway) is correct. Ensure that the container networks are properly defined in your docker-compose.yml file.

General Troubleshooting Tips:

  • Check Systemd Logs: The systemd logs are your best friend. Use journalctl -u compose to view logs related to your service.
  • Use docker logs <container_id>: This command displays the logs for a specific Docker container.
  • Start Simple: Create a minimal docker-compose.yml file with just one container to isolate the issue.

Resources: