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.
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?
systemd service is a script or executable that is managed by the
systemd process, which controls the behavior and status of these underlying services.
- These services can start up all by themselves when your computer boots up.
- If something goes wrong, they can get a fresh start.
- You can decide whether you want these services to work with 'systemd' or not.
- By using
systemdservices, system administrators can manage and organize system services in a consistent and centralized manner.
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 upcommand, additionally we can have
-dflag 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
- Create file
/etc/systemd/system/myapp.servicewith below content - note
ExecStopdirectives - each will be used as starting and stopping command for the service respectively
- You can set environment variables using
Environmentdirective or pass variables from a file
- Once file is created you need to reload all system services using
- Now you have created
myappservice, start the service using
systemctl start myappwhich will execute command/script defined in
- If you want to start your service whenever your server reboots then you need to enable it using
systemctl enable myappcommand which will ensure whenever your server boots up
myappservice will be started
# 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.
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
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.