Docker --restart always
Dear @luisviveropena ,
I was practicing docker (podman) on centos8 Stream and while doing it I found some issues:
Anyway I wanted to run docker with --restart=always option in order for container to auto start after system boot so I did following:
- Installed podman on the system from root account:
dnf install podman -y
- Instead of pulling http image and then running it, I have used following command to pull and run in same time. With this command I have pulled httpd image and made container named httpd run in detached mode and allowed port 80 to be exposed. As found in documentation --restart option should autostart this container once its off even after reboot of the system if docker daemon is running.
docker run --name httpd --restart=always -d -p 80:80 httpddocker container ls - Showed container in running state
- checked that docker is running with docker container ls & confirmed it with lynx. Tried to reboot the system with shutdown -r now, but httpd docker didnt started automatically.
shutdown -r now
- After it I have checked the service again and figured out that podman.service is not running so i have tried to run it manually to see if container will start
docker container ls - Shows container not active after rebootsystemctl status podman.servicesystemctl start podman.service
- Once service was started manually container haven't started .
systemctl enable podman.service - Didn't workeddocker container ls - Showed me that container is not running
- After that i have found 2 solutions to run this container on boot but both solutions are not with docker (podman). First i have inserted line in crontab to run it @reboot . And then I decided to remove it from there and make a custom service which would start and stop this container, so i have created a following service:
vim /usr/lib/systemd/system/httpddocker.service
[Unit]
Description=httpddocker
After=multi-user.target[Service]
Type=oneshot
ExecStart=/usr/bin/podman start httpd
RemainAfterExit=yes
ExecStop=/usr/bin/podman stop httpd[Install]
WantedBy=custom.target
- Then I reloaded systemctl and enabled new service and rebooted after
systemctl daemon-reloadsystemctl enable httpddocker.serviceshutdown -r now
- After that http container started automatically after boot and I was also able to stop it with stopping of httpddocker.service.
@luisviveropena - My question is - Can you please tell me what are the other options to run this container after boot and is this a known bug with centos8 or I did something wrong or misunderstood --restart=always option in docker documentation?
Regards&
Thank you.
Best Answer
-
Hi @marejovanovic,
I was checking on the documentation, and I found that's the expected behavior. Look at the podman manual at https://docs.podman.io/en/latest/markdown/podman-run.1.html:
--restart=policy
Please note that restart will not restart containers after a system reboot. If this functionality is required in your environment, you can invoke Podman from a systemd.unit(5) file, or create an init script for whichever init system is in use. To generate systemd unit files, please see podman generate systemd.
And the suggestion for what you are looking for is just there.
I hope that points you in the right direction.
Regards,
Luis.0
Answers
-
Hello @luisviveropena , Thank you for reply. I was reading docker documentation and obviously there are some differences between docker and podman documentations.
I am glad that i was on right track with creating a service which runs container, only podman allows this to be done faster and automatically if we use following command ( podman generate systemd {name of existing container} )podman generate systemd httpdcontainer-a3419c6e5982f12904721e27670c1f4243d0315cfc227c9a49cece982e651e36.service
autogenerated by Podman 3.2.3
Mon Nov 15 08:27:58 +04 2021
[Unit]
Description=Podman container-a3419c6e5982f12904721e27670c1f4243d0315cfc227c9a49cece982e651e36.service
Documentation=man:podman-generate-systemd(1)
Wants=network.target
After=network-online.target
RequiresMountsFor=/run/containers/storage[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStart=/usr/bin/podman start a3419c6e5982f12904721e27670c1f4243d0315cfc227c9a49cece982e651e36
ExecStop=/usr/bin/podman stop -t 10 a3419c6e5982f12904721e27670c1f4243d0315cfc227c9a49cece982e651e36
ExecStopPost=/usr/bin/podman stop -t 10 a3419c6e5982f12904721e27670c1f4243d0315cfc227c9a49cece982e651e36
PIDFile=/run/containers/storage/overlay-containers/a3419c6e5982f12904721e27670c1f4243d0315cfc227c9a49cece982e651e36/userdata/conmon.pid
Type=forking[Install]
WantedBy=multi-user.target default.targetPrevious command generated output above so we could use same command to redirect output to systemd and then enable this service to run this container at boot as shown below:
podman generate systemd httpd > /usr/lib/systemd/system/httpd-docker.service systemctl daemon-reload systemctl enable httpd-docker.service --now
1 -
Hi @marejovanovic, yep, you were in the right path

I'm glad you were able to get it done! And about the differences you found, yes, it uses to happen when you try a second tool/service to implement a functionality.
Regards,
Luis.0
Categories
- All Categories
- 175 LFX Mentorship
- 175 LFX Mentorship: Linux Kernel
- 745 Linux Foundation IT Professional Programs
- 372 Cloud Engineer IT Professional Program
- 168 Advanced Cloud Engineer IT Professional Program
- 73 DevOps IT Professional Program - Discontinued
- 3 DevOps & GitOps IT Professional Program
- 98 Cloud Native Developer IT Professional Program
- 7.6K Training Courses & Learning Paths
- AI & ML Training
- Blockchain & Decentralized Identity Training
- Cloud & Containers Training
- Cybersecurity Training
- DevOps & Site-Reliability Training
- Linux Kernel Development Training
- Networking Training
- Open Source Best Practice Training
- System Administration Training
- System Engineering Training
- Web & Application Development Training
- 2 LFD103-JP クラス フォーラム
- 4 LFD210-CN Class Forum
- 764 LFD259 Class Forum
- 681 LFS101 Class Forum
- 2 LFS158-JP クラス フォーラム
- 162 LFS207 Class Forum
- 3 LFS207-DE-Klassenforum
- 4 LFS207-JP クラス フォーラム
- 61 LFS241 Class Forum
- 52 LFS242 Class Forum
- 42 LFS243 Class Forum
- 19 LFS244 Class Forum
- 4 LFS250-JP クラス フォーラム
- 166 LFS253 Class Forum
- 19 LFS256 Class Forum
- 1.4K LFS258 Class Forum
- 165 LFS261 Class Forum
- 26 LFS267 Class Forum
- 792 Hardware
- 202 Drivers
- 68 I/O Devices
- 37 Monitors
- 95 Multimedia
- 173 Networking
- 91 Printers & Scanners
- 87 Storage
- 768 Linux Distributions
- 81 Debian
- 67 Fedora
- 22 Linux Mint
- 13 Mageia
- 24 openSUSE
- 150 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 465 Linux System Administration
- 31 Cloud Computing
- 73 Command Line/Scripting
- Github systems admin projects
- 98 Linux Security
- 78 Network Management
- 101 System Management
- 46 Web Management
- 106 Mobile Computing
- 18 Android
- 73 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 392 Off Topic
- 121 Introductions
- 181 Small Talk
- 29 Study Material
- 945 Programming and Development
- 310 Kernel Development
- 617 Software Development
- 977 Software
- 369 Applications
- 182 Command Line
- 5 Compiling/Installing
- 68 Games
- 317 Installation
- Archived
- 2 LFD140 Class Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)