In this blog post, we will explore how to run Docker and Podman container as Systemd service, enabling smoother integration of containerized applications into your environment.
In the world of containerization, Docker and Podman have become indispensable tools for developers and system administrators. They provide an efficient way to package, distribute, and run applications. One of the advanced use cases is running containers as Systemd services, which ensures they start automatically with the system and can be managed just like any other Systemd unit.
Why Run Container as Systemd Service?
Running containers as Systemd services offers several benefits:
- Autostart on Boot: Containers configured as Systemd services will start automatically when the system boots up. This is vital for ensuring your application is always available.
- Dependency Management: You can define dependencies between containers and other Systemd services, allowing for ordered and reliable start-up sequences.
- Logging and Monitoring: Systemd provides built-in logging and monitoring, which can be harnessed for better container management.
- Granular Control: You can control container lifecycle just like any other Systemd service, making it easier to stop, restart, and manage them.
- Pre Installed Docker / Podman on your Linux system
- Stable Internet Connection
- Sudo User with root privileges
Now, let’s deep dive into the specifics of how to run Docker and Podman containers as Systemd services.
Running Docker Container as Systemd Service
1) Create a Systemd Unit File
To run a Docker container as a Systemd service, you’ll need to create a Systemd unit file. Here’s an example of a unit file, in this example we have used nginx image, you can adjust the ExecStart line to specify your Docker image and configuration you need.
$ sudo vi nginx-container.service [Unit] Description=NGINX Dockerized Service After=docker.service Requires=docker.service [Service] Restart=always ExecStart=/usr/bin/docker run --rm --name %n -p 8080:80 --stop-timeout 60 nginx ExecStop=/usr/bin/docker stop -t 2 %n [Install] WantedBy=default.target
save and exit the file.
Next, copy the created unit file to the appropriate Systemd directory (usually /etc/systemd/system/).
$ sudo cp nginx-container.service /etc/systemd/system
Reload the Systemd manager to recognize the newly added unit file, run beneath command.
$ sudo systemctl daemon-reload
2) Start and Enable Nginx container Systemd service
Run the following commands to start and enable nginx container systemd service,
$ sudo systemctl start nginx-container.service $ sudo systemctl enable nginx-container.service
Check the service status,
$ sudo docker ps
Finally, try to access your nginx application, using following url:
Running Podman Container as Systemd Service
Podman is an alternative to Docker and offers similar capabilities for running containers as Systemd services. Here’s how to do it:
For the demonstration purpose, I am using httpd image.
1) Run httpd container
Execute the following podman command to run httpd container, we will be using this container for generating systemd file .
$ podman run --name=httpd --hostname=httpd -p 8081:80 -d docker.io/library/httpd $ podman ps
2) Generate Systemd service file for httpd container
Execute the following podman command to generate systemd file using above created httpd container,
$ podman generate systemd --new --files --name httpd /home/linuxtechi/container-httpd.service $
Now copy this systemd service file to “/etc/systemd/system” folder.
$ sudo cp container-httpd.service /etc/systemd/system
As we have generated the systemd service, remove the httpd container,run
$ podman stop httpd && podman rm -a
3) Start and Enable HTTPD Container Systemd Service
Run following systemctl commands to start and enable httpd container systemd service.
$ sudo systemctl daemon-reload $ sudo systemctl start container-httpd.service $ sudo systemctl enable container-httpd.service
Check the container and systemd service status,
$ sudo podman ps $ sudo systemctl status container-httpd.service
Perfect, output above confirms that httpd container is started using its systemd service.
Now, try to access web page of your httpd application, run
$ curl http://<Linux-Server-IPAddress>:8081
$ curl http://192.168.1.29:8081 <html><body><h1>It works!</h1></body></html> $
That’s all from this post, I hope you have found it informative. Kindly do post your queries and feedback in below comments section