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 httpd
docker 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.service
systemctl 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-reload
systemctl enable httpddocker.service
shutdown -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 httpd
container-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
- 51 LFX Mentorship
- 104 LFX Mentorship: Linux Kernel
- 576 Linux Foundation IT Professional Programs
- 304 Cloud Engineer IT Professional Program
- 125 Advanced Cloud Engineer IT Professional Program
- 53 DevOps Engineer IT Professional Program
- 61 Cloud Native Developer IT Professional Program
- 5 Express Training Courses
- 5 Express Courses - Discussion Forum
- 2K Training Courses
- 19 LFC110 Class Forum
- 7 LFC131 Class Forum
- 27 LFD102 Class Forum
- 157 LFD103 Class Forum
- 20 LFD121 Class Forum
- 1 LFD137 Class Forum
- 61 LFD201 Class Forum
- 1 LFD210 Class Forum
- LFD210-CN Class Forum
- 1 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum
- LFD237 Class Forum
- 23 LFD254 Class Forum
- 611 LFD259 Class Forum
- 105 LFD272 Class Forum
- 1 LFD272-JP クラス フォーラム
- 1 LFD273 Class Forum
- 2 LFS145 Class Forum
- 24 LFS200 Class Forum
- 739 LFS201 Class Forum
- 1 LFS201-JP クラス フォーラム
- 11 LFS203 Class Forum
- 75 LFS207 Class Forum
- 300 LFS211 Class Forum
- 54 LFS216 Class Forum
- 47 LFS241 Class Forum
- 41 LFS242 Class Forum
- 37 LFS243 Class Forum
- 11 LFS244 Class Forum
- 36 LFS250 Class Forum
- 1 LFS250-JP クラス フォーラム
- LFS251 Class Forum
- 140 LFS253 Class Forum
- LFS254 Class Forum
- 1.1K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 93 LFS260 Class Forum
- 132 LFS261 Class Forum
- 33 LFS262 Class Forum
- 80 LFS263 Class Forum
- 15 LFS264 Class Forum
- 11 LFS266 Class Forum
- 18 LFS267 Class Forum
- 17 LFS268 Class Forum
- 23 LFS269 Class Forum
- 203 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- LFS274 Class Forum
- LFS281 Class Forum
- 235 LFW211 Class Forum
- 172 LFW212 Class Forum
- 7 SKF100 Class Forum
- SKF200 Class Forum
- 902 Hardware
- 219 Drivers
- 74 I/O Devices
- 44 Monitors
- 115 Multimedia
- 209 Networking
- 101 Printers & Scanners
- 85 Storage
- 763 Linux Distributions
- 88 Debian
- 66 Fedora
- 15 Linux Mint
- 13 Mageia
- 24 openSUSE
- 142 Red Hat Enterprise
- 33 Slackware
- 13 SUSE Enterprise
- 357 Ubuntu
- 479 Linux System Administration
- 41 Cloud Computing
- 70 Command Line/Scripting
- Github systems admin projects
- 95 Linux Security
- 78 Network Management
- 108 System Management
- 49 Web Management
- 68 Mobile Computing
- 23 Android
- 30 Development
- 1.2K New to Linux
- 1.1K Getting Started with Linux
- 537 Off Topic
- 131 Introductions
- 217 Small Talk
- 21 Study Material
- 826 Programming and Development
- 278 Kernel Development
- 514 Software Development
- 928 Software
- 260 Applications
- 184 Command Line
- 3 Compiling/Installing
- 76 Games
- 316 Installation
- 62 All In Program
- 62 All In 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)