Skip to content

Instantly share code, notes, and snippets.

@isweluiz
Last active August 2, 2021 18:10
Show Gist options
  • Select an option

  • Save isweluiz/3d126e55e7259597eb747edbf1779a68 to your computer and use it in GitHub Desktop.

Select an option

Save isweluiz/3d126e55e7259597eb747edbf1779a68 to your computer and use it in GitHub Desktop.
RUNNING CONTAINERS AS SYSTEMD SERVICES WITH PODMAN

Enviroment:

  • Wordpress;
  • Mariadb;
  • Systemd.

wp-pod

For more detail you can check the official documentation on Red Hat.

Creating our pod, we want to publish port "80" in the pod to "8080" on the host. We want to name the pod "wp-pod".

podman pod create --name wp-pod -p 8080:80

Running our MariaDB pod. Set the following variables:

  • MYSQL_ROOT_PASSWORD="wordress321"
  • MYSQL_DATABASE="wordpress"
  • MYSQL_USER="wordpress"
  • MYSQL_PASSWORD="wordpress"
podman run -d --name mariadb --restart=always --pod wp-pod \
 -e MYSQL_USER=wordpress \
-e MYSQL_PASSWORD=wordpress123 \
-e MYSQL_ROOT_PASSWORD=wordress321 \
-e MYSQL_DATABASE=wordpress \
mariadb 

Next, we'll start the WordPress container. Set the following variables:

  • WORDPRESS_DB_NAME="wordpress"
  • WORDPRESS_DB_USER="wordpress"
  • WORDPRESS_DB_PASSWORD="wppass"
  • WORDPRESS_DB_HOST="127.0.0.1"
podman run -d --name wordpress --restart always --pod wp-pod 
-e WORDPRESS_DB_HOST="127.0.0.1" \
-e WORDPRESS_DB_USER=wordpress \
-e WORDPRESS_DB_PASSWORD=wordpress123 \
-e WORDPRESS_DB_NAME=wordpress \
wordpress 

Creating the systemd user directory

mkdir /home/luiz/.config/systemd/user -p

Generating our systemd service files

podman generate systemd --files --new --name wp-pod 

Reload, enabling and checking the status of systemd service

systemctl --user daemon-reload
systemctl --user enable --now pod-wp-pod
systemctl --user status pod-wp-pod.service 

Let's test our persistent setup for our wp-pod pod!

Perform the following:

  • Reboot to test our pod for persistence.
  • Check for containers and pods again.
  • Check connectivity with a curl command on localhost:8080. You won't get anything back. Check the exit code immediately. It should be 0. If you'd like to try connecting with a web browser on the lab server's public IP, on port 8080, you can.
@ip-10-0-1-172: user $ systemctl --user status pod-wp-pod.service 
● pod-wp-pod.service - Podman pod-wp-pod.service
   Loaded: loaded (/home/luiz/.config/systemd/user/pod-wp-pod.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2021-08-02 17:40:13 UTC; 15min ago
     Docs: man:podman-generate-systemd(1)
 Main PID: 13486 (conmon)
   CGroup: /user.slice/user-1001.slice/user@1001.service/pod-wp-pod.service
           ├─13462 /usr/bin/fuse-overlayfs -o ,lowerdir=/home/luiz/.local/share/containers/storage/overlay/l/ML>
           ├─13466 /usr/bin/slirp4netns --disable-host-loopback --mtu=65520 --enable-sandbox --enable-seccomp -c -e 3>
           ├─13468 containers-rootlessport
           ├─13475 containers-rootlessport-child
           ├─13486 /usr/bin/conmon --api-version 1 -c 892bc726cdb7894eca9cb21b0f4462c7ee5c9b371a45069d48a5867892b6a19>
           └─892bc726cdb7894eca9cb21b0f4462c7ee5c9b371a45069d48a5867892b6a19b
             └─13496 /pause

Aug 02 17:40:11 ip-10-0-1-172.ec2.internal systemd[9566]: Starting Podman pod-wp-pod.service...
Aug 02 17:40:12 ip-10-0-1-172.ec2.internal podman[13419]: 6f5152ad9dfef2c80234279afd045970b4594ab3e26da97db1e9dc900c2>
Aug 02 17:40:13 ip-10-0-1-172.ec2.internal podman[13444]: 6f5152ad9dfef2c80234279afd045970b4594ab3e26da97db1e9dc900c2>
Aug 02 17:40:13 ip-10-0-1-172.ec2.internal systemd[9566]: Started Podman pod-wp-pod.service.


@ip-10-0-1-172: user $ podman pod ps
POD ID        NAME    STATUS   CREATED         INFRA ID      # OF CONTAINERS
6f5152ad9dfe  wp-pod  Running  26 minutes ago  892bc726cdb7  3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment