Systemd and Docker Compose: A Dynamic Duo for Efficient Deployments
Docker Compose is like a handy tool for putting together simple apps. It lets you start a bunch of connected parts (we call them containers) all at once, creating a single app. It's pretty easy to use and set up.
But for really big and complicated apps, there are other tools like Docker Swarm or Kubernetes that might work better. They're like supercharged versions, with extra features and power, perfect for the big leagues of software.
Introduction
Docker Compose makes it super easy to set up simple applications. But there's a little catch: when the server restarts, the Docker containers don't start on their own. No worries, though! We can fix this by creating a custom systemd service. Let's start from the beginning to understand how.
What is systemd service?
A systemd service
is a script or executable that is managed by the systemd
process, which controls the behavior and status of these underlying services.
- Automatic Startup: Ensure that your Docker containers start automatically when the system boots.
- Unified Management: Use familiar systemctl commands to manage your Docker Compose applications.
- Enhanced Reliability: Leverage systemd's process management capabilities to monitor and restart services as needed.
- Handy & Simple to operate: Handling services is simple and easy as they can be used as system component instead of decoupled program.
Steps to Create systemd Service
To create systemd service we need to write a service file which will execute command or script in case of start, stop and restart actions.
So in case of docker-compose application we need to have docker-compose.yml
file which has definition of all the services and then using docker-compose up -d
command we can give start
action for the service, similarly for stop
we can use docker-compose stop
command.
Let's see step by step:
- Write docker-compose.yml file with all services/components, below is the django app example taken from docker documentation.
- For testing purpose you can bring the application up by using
docker-compose up
command, additionally we can have-d
flag for running application as daemon
# will bring up all services in docker-compose.yml
docker-compose up -d
# pass yml file with -f flag
docker-compose up -f application.yml
- Once, application is working correctly we can have service for this application by writing service file and placing it in
/etc/systemd/system
directory - Create file
/etc/systemd/system/myapp.service
with below content - noteExecStart
andExecStop
directives - each will be used as starting and stopping command for the service respectively
- You can set environment variables using
Environment
directive or pass variables from a file - Once file is created you need to reload all system services using
systemctl daemon-reload
command - Now you have created
myapp
service, start the service usingsystemctl start myapp
which will execute command/script defined inExecStart
- If you want to start your service whenever your server reboots then you need to enable it using
systemctl enable myapp
command which will ensure whenever your server boots upmyapp
service will be started - Adjust the
WorkingDirectory
to the location of yourdocker-compose.yml
. - Ensure the
ExecStart
andExecStop
paths point to your Docker Compose binary.
# will enable service to start at server bootup
systemctl enable myapp
# start service ( Executes ExecStart command )
systemctl start myapp
This way you can make sure your docker-compose service stays up and running after every server restart.
Best Practices
- Environment Variables: If your application requires environment variables, define them within the
docker-compose.yml
or specify them in the systemd service file using theEnvironment
directive. - User Permissions: If running Docker as a non-root user, specify the
User
directive in the[Service]
section of the systemd service file. - Timeouts: Adjust
TimeoutStartSec
and other timeout settings as needed to ensure that services have adequate time to start and stop.
Conclusion
To turn a correctly working application into a service, you can write a service file and place it in the /etc/systemd/system
directory. The service file, named myapp.service
, should contain the starting and stopping commands for the service, specified in the ExecStart
and ExecStop
directives, respectively.
With a custom systemd
service, the application can be automatically started when the server is restarted, overcoming the limitation of Docker Compose's lack of automatic start on reboot.
Member discussion