How to run Docker / Podman container as a system service

The
You can download this article in PDF format to support us through the following link.

Download the guide as a PDF

turn off
The

The
The

Container engines such as Podman or Docker do not have the ability / function to manage system services, such as stopping services, starting sequence, dependency checking, and recovery of faulty services. This is most likely due to the special development of other initialization applications (such as init and systemd) to achieve this purpose, because they have the beauty of injection.

The good news is that Podman / Docker containers can now be managed in a similar way, and thus can manage httpd, nginx or any other service you are used to. In other words, you can make the host start, stop, enable, check the status, and usually manage the container as a system service, we will learn how to do this in this guide.

Before we start, another beautiful feature is that when the container starts, you can also use systemd to start a given service installed in the container, such as Nginx.

Use systemd to start the container

If you do not have podman installed, please run the following command to speed it up

######## CentOS 8/RHEL 8 ##########
sudo dnf -y install podman

`

Install Podman on Ubuntu

Follow the guide below to install podman on your Ubuntu:

How to install Podman on Ubuntu

Now that we are sailing on the same yacht, let’s start using systemd to run, start, stop and check the status of the container.

Step 1: Pull the image (if there is no image yet)

As an example, let’s extract an Nginx container from the Docker registry

$ podman pull docker.io/nginx

Check if the image is pulled out successfully

$ podman images

REPOSITORY                TAG      IMAGE ID       CREATED        SIZE   
localhost/firstapache     latest   a0c546bc3927   23 hours ago   1.68 GB
docker.io/library/nginx   latest   602e111c06b6   32 hours ago   131 MB 

Step 2: Use Podman to run the container

In this step, we will initialize the image to run as the image with the name we chose. If you are using CentOS / RHEL and stick to SELinux, you must open container_manage_cgroup Boolean uses systemd to run the container as follows

sudo setsebool -P container_manage_cgroup on

After that, continue to run the container

$ sudo podman run -d --name nginx_server -p 7070:80 nginx
384a42964b1b133d82320ebaa2f54407c7ca23d06154a5f413c8119026bda231

To confirm that the container is running, run the podman ps command

$ sudo podman ps
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS                 NAMES
384a42964b1b  docker.io/library/nginx:latest  nginx -g daemon o...  48 seconds ago  Up 46 seconds ago  0.0.0.0:7070->80/tcp  nginx_server

Step 3: Configure the container as a systemd service

We will / etc / systemd / system / table of Contents. As an example, we will create a file in a directory named /etc/systemd/system/nginx-container.service. Inside the file, fill in the following similar details and make sure that nginx_server is the same as the name given to the container when using podman runtime in step 2. If you are not sure, just run “sudo podman” ps “and check the” NAMES “column.

$ sudo vim /etc/systemd/system/nginx-container.service

[Unit]
Description=Cool Nginx container
Wants=syslog.service

[Service]
Restart=always
ExecStart=/usr/bin/podman start -a nginx_server
ExecStop=/usr/bin/podman stop -t 2 nginx_server

[Install]
WantedBy=multi-user.target

As simple as that, we are ready to manage the container like any other service using systemd.

Step 4: Test whether the setting is valid

Start the service Run the regular systemd start command to start our service

sudo systemctl start nginx-container

Check its status

$ systemctl status nginx-container

● nginx-container.service - Cool Nginx container
   Loaded: loaded (/etc/systemd/system/nginx-container.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-04-24 21:58:15 UTC; 9s ago
 Main PID: 3910 (podman)
    Tasks: 11 (limit: 11121)
   Memory: 32.2M
   CGroup: /system.slice/nginx-container.service
           └─3910 /usr/bin/podman start -a nginx_server

If you want to run the container at system startup, just enable it as shown below

$ sudo systemctl enable nginx-container

Created symlink /etc/systemd/system/local.target.wants/nginx-container.service → /etc/systemd/system/nginx-container.service.

ul off

So far, the sailing situation on our yacht is really good, I hope you like it as much as we do. As a result, we can now run the container as a system service, and the experience is very good. Thank you for your company during the voyage. When we stop, you can enjoy another journey by clicking the link below.

Use Podman to set up Docker container registry and encrypt SSL

How to install Podman on Debian 10/9

Install and use Podman on CentOS 8 / RHEL 8

How to use Podman and Lippod to run Docker containers

How to use Buildah to build OCI and Docker container images

The
You can download this article in PDF format to support us through the following link.

Download the guide as a PDF

turn off
The

The
The

Sidebar