Running containers as systemd services
Start a rootless container automatically at boot with a Quadlet unit.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 8 minutes to read.
In short
Containers should start at boot like any other service. Quadlet lets you describe a container in a small .container file, and systemd generates a matching service unit from it. For rootless containers, lingering keeps the user's systemd instance running without a login session.
Explained simply
A container you start with podman run stops when the server reboots, and nobody restarts it if it crashes. For real services you want systemd, the same manager that starts sshd and httpd, to look after your container too.
Quadlet makes this easy. You write a short file ending in .container that says which image to run, which ports to publish and which folders to mount. systemd reads it and creates a normal service from it. You then start, stop and check it with systemctl.
For rootless containers there is one extra step. A normal user's services usually only run while that user is logged in. Turning on lingering with loginctl enable-linger tells systemd to start the user's services at boot and keep them running after logout.
The final test is always the same: reboot the machine and check that the container came back on its own.
Press Play on a diagram to walk through it one step at a time.
- 1Write web.container
In ~/.config/containers/systemd/.
- 2daemon-reload
systemctl --user daemon-reload generates web.service.
- 3Start it
systemctl --user start web.
- 4Enable lingering
loginctl enable-linger so it runs without a login.
- 5Reboot and verify
podman ps and systemctl --user status web.
- systemd (PID 1) to user manager: Linger is on: start user@1001.service1Linger is on: start user@1001.service
- 2Generator turns web.container into web.serviceuser manager to user manager: Generator turns web.container into web.service
- user manager to web.service: default.target wants web.service3default.target wants web.service
- web.service to podman: podman run with the Quadlet options4podman run with the Quadlet options
- podman to web.service: Container running; output goes to the journal5Container running; output goes to the journal
In depth
Where Quadlet files go and what they become
For a rootless user, put files in ~/.config/containers/systemd/. For system-wide containers run by root, use /etc/containers/systemd/. When you run systemctl --user daemon-reload (or systemctl daemon-reload for root), a systemd generator reads each file and writes a service unit: web.container becomes web.service. The generated unit exists only in a runtime folder, not in your config, which is why you edit the .container file and never the service.
By default the container is named systemd-web; set ContainerName=web if you want a short name in podman ps. If the service does not appear, ask Quadlet what is wrong with /usr/libexec/podman/quadlet -dryrun -user, which prints the generated unit or the error.
- Other Quadlet types: .volume, .network, .pod, .kube, .image
- systemctl --user cat web.service shows the generated unit
- Rootful containers use WantedBy=multi-user.target
- 1web.container
Written in ~/.config/containers/systemd
- 2daemon-reload
Quadlet generator runs
- 3web.service
Generated in a runtime folder
- 4systemctl --user start web
Runs podman run for you
- 5systemd watches it
Restart= rules, journal logs
The keys you will use most
In the [Container] section: Image= the full image name, ContainerName=, PublishPort=8080:8080, Volume=%h/web:/var/www/html:Z (where %h is your home folder), Environment=KEY=value or EnvironmentFile= for many variables, and Exec= to override the command. AutoUpdate=registry lets podman auto-update pull newer images on a timer.
The [Service] section takes normal systemd settings. Restart=always restarts the container if it exits. TimeoutStartSec=900 gives slow image pulls time on first start. The [Install] section, with WantedBy=default.target for user units, is what makes it start at boot. You cannot run systemctl enable on a generated unit; the generator handles [Install] for you.
Lingering and the user manager
Every logged-in user gets their own systemd instance, the user manager, which runs their --user units. Normally it starts at login and stops after the last session ends, taking the containers with it, and it does not start at boot.
loginctl enable-linger USER changes that: the user manager starts at boot and keeps running. It creates a marker file in /var/lib/systemd/linger/USER, and loginctl show-user USER --property=Linger shows Linger=yes. Without lingering, your container works perfectly until you log out, which is a classic exam trap.
Using systemctl --user correctly
systemctl --user needs to talk to the user's own manager, through the variables XDG_RUNTIME_DIR and DBUS_SESSION_BUS_ADDRESS that a real login sets. If you switch user with su or sudo -u, those are missing and you get Failed to connect to bus. Log in properly with ssh user@localhost, or use machinectl shell user@ from root.
Logs for a user container: journalctl --user -u web, or podman logs systemd-web. Check status with systemctl --user status web. After editing the .container file, run daemon-reload again and restart the service. The older way, podman generate systemd, still exists but is deprecated in favour of Quadlet, so learn both names but write Quadlet files.
- Failed to connect to bus: you used su; log in with ssh instead
- systemctl --user list-units 'web*'
- Edit, daemon-reload, restart: three steps after every change
Learn it in 4 short steps
About 30m each. Track them with a timer in your plan.
- 1Write a Quadlet .container file~30m
- 2Enable lingering with loginctl~30m
- 3Manage it with systemctl --user~30m
- 4Verify it survives a reboot~30m
Key points
- The [Install] section with WantedBy=default.target makes a Quadlet unit start at boot.
- Generated units cannot be enabled with systemctl enable; the [Install] section does that.
- Use systemctl --user for rootless units and log in as that user, not via su.
- podman generate systemd is the older, deprecated approach you may still see.
Key terms
Tap a card to flip it. 0/4 known
Examples
# ~/.config/containers/systemd/web.container
[Container]
Image=registry.access.redhat.com/ubi9/httpd-24
PublishPort=8080:8080
Volume=%h/web:/var/www/html:Z
[Install]
WantedBy=default.target
# systemctl --user daemon-reload && systemctl --user start web
# loginctl enable-linger $USERA Quadlet unit; %h expands to the user's home directory.
A rootless web container that survives reboot
User ana needs the UBI httpd container serving ~/web on port 8080 at boot. Step 1: log in as ana with SSH, not su. Step 2: create the Quadlet folder and file. Step 3: daemon-reload and start. Step 4: as root, enable lingering for ana. Step 5: reboot, log in again and check the service and the page.
ssh ana@localhost
mkdir -p ~/.config/containers/systemd ~/web && echo hi > ~/web/index.html
cat > ~/.config/containers/systemd/web.container <<'EOF'
[Container]
Image=registry.access.redhat.com/ubi9/httpd-24
ContainerName=web
PublishPort=8080:8080
Volume=%h/web:/var/www/html:Z
[Service]
Restart=always
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload && systemctl --user start web
sudo loginctl enable-linger ana
sudo systemctl reboot # then: systemctl --user status web; curl localhost:8080The service does not show up
After daemon-reload, systemctl --user start db says Unit db.service not found. Step 1: check the file is in the right folder and ends in .container. Step 2: run the Quadlet generator in dry-run mode to see the error; here it reports an unknown key, a typo in PublishPorts. Step 3: fix it, reload and start. Step 4: confirm with systemctl --user status and podman ps.
ls ~/.config/containers/systemd/
/usr/libexec/podman/quadlet -dryrun -user 2>&1 | tail -5
# unsupported key 'PublishPorts' in group 'Container'
sed -i 's/^PublishPorts=/PublishPort=/' ~/.config/containers/systemd/db.container
systemctl --user daemon-reload
systemctl --user start db
systemctl --user status db --no-pager; podman psQuick reference
- Rootless Quadlet files: ~/.config/containers/systemd/NAME.container
- Rootful Quadlet files: /etc/containers/systemd/NAME.container
- NAME.container becomes NAME.service after daemon-reload
- Boot start comes from [Install] WantedBy=default.target, not systemctl enable
- loginctl enable-linger USER keeps user services running without a login
- Use ssh user@localhost, not su, before systemctl --user
- /usr/libexec/podman/quadlet -dryrun -user shows generator errors
- %h in a Quadlet file expands to the user's home folder
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Enable lingering for your user and confirm it is on.
- Exercise 2medium
Write a Quadlet unit for a rootless Redis container on port 6379 with a named volume, start it and check its logs.
- Exercise 3hard
Run a container as a system service owned by root that starts at boot, and compare where its unit and logs live with a rootless one.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is Quadlet?
- A systemd generator shipped with Podman. It turns small .container, .volume and similar files into full service units, so containers are managed like any other service.
- Is podman generate systemd still usable?
- It still works but is deprecated and gets no new features. Quadlet is the recommended way on current RHEL and Fedora.
- How do I see logs for a Quadlet container?
- journalctl --user -u NAME for rootless units, or journalctl -u NAME for rootful ones. podman logs CONTAINER also works.
- Do I need lingering for rootful containers?
- No. Lingering is only for rootless containers run by a user's systemd instance. Root's units run under the system manager, which always starts at boot.