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
- 167 LFX Mentorship
- 219 LFX Mentorship: Linux Kernel
- 795 Linux Foundation IT Professional Programs
- 355 Cloud Engineer IT Professional Program
- 179 Advanced Cloud Engineer IT Professional Program
- 82 DevOps Engineer IT Professional Program
- 127 Cloud Native Developer IT Professional Program
- 112 Express Training Courses
- 112 Express Courses - Discussion Forum
- 6.2K Training Courses
- 48 LFC110 Class Forum - Discontinued
- 17 LFC131 Class Forum
- 35 LFD102 Class Forum
- 227 LFD103 Class Forum
- 14 LFD110 Class Forum
- 39 LFD121 Class Forum
- 15 LFD133 Class Forum
- 7 LFD134 Class Forum
- 17 LFD137 Class Forum
- 63 LFD201 Class Forum
- 3 LFD210 Class Forum
- 5 LFD210-CN Class Forum
- 2 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum - Discontinued
- 1 LFD233 Class Forum
- 2 LFD237 Class Forum
- 23 LFD254 Class Forum
- 697 LFD259 Class Forum
- 109 LFD272 Class Forum
- 3 LFD272-JP クラス フォーラム
- 10 LFD273 Class Forum
- 152 LFS101 Class Forum
- 1 LFS111 Class Forum
- 1 LFS112 Class Forum
- 1 LFS116 Class Forum
- 1 LFS118 Class Forum
- LFS120 Class Forum
- 7 LFS142 Class Forum
- 7 LFS144 Class Forum
- 3 LFS145 Class Forum
- 1 LFS146 Class Forum
- 3 LFS147 Class Forum
- 1 LFS148 Class Forum
- 15 LFS151 Class Forum
- 1 LFS157 Class Forum
- 33 LFS158 Class Forum
- 8 LFS162 Class Forum
- 1 LFS166 Class Forum
- 1 LFS167 Class Forum
- 3 LFS170 Class Forum
- 2 LFS171 Class Forum
- 1 LFS178 Class Forum
- 1 LFS180 Class Forum
- 1 LFS182 Class Forum
- 1 LFS183 Class Forum
- 29 LFS200 Class Forum
- 736 LFS201 Class Forum - Discontinued
- 2 LFS201-JP クラス フォーラム
- 14 LFS203 Class Forum
- 102 LFS207 Class Forum
- 1 LFS207-DE-Klassenforum
- 1 LFS207-JP クラス フォーラム
- 301 LFS211 Class Forum
- 55 LFS216 Class Forum
- 48 LFS241 Class Forum
- 42 LFS242 Class Forum
- 37 LFS243 Class Forum
- 15 LFS244 Class Forum
- LFS245 Class Forum
- LFS246 Class Forum
- 50 LFS250 Class Forum
- 1 LFS250-JP クラス フォーラム
- LFS251 Class Forum
- 154 LFS253 Class Forum
- LFS254 Class Forum
- LFS255 Class Forum
- 5 LFS256 Class Forum
- 1 LFS257 Class Forum
- 1.3K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 111 LFS260 Class Forum
- 159 LFS261 Class Forum
- 41 LFS262 Class Forum
- 82 LFS263 Class Forum - Discontinued
- 15 LFS264 Class Forum - Discontinued
- 11 LFS266 Class Forum - Discontinued
- 20 LFS267 Class Forum
- 24 LFS268 Class Forum
- 29 LFS269 Class Forum
- 1 LFS270 Class Forum
- 199 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- LFS274 Class Forum
- 3 LFS281 Class Forum
- 9 LFW111 Class Forum
- 260 LFW211 Class Forum
- 182 LFW212 Class Forum
- 13 SKF100 Class Forum
- 1 SKF200 Class Forum
- 1 SKF201 Class Forum
- 782 Hardware
- 198 Drivers
- 68 I/O Devices
- 37 Monitors
- 96 Multimedia
- 174 Networking
- 91 Printers & Scanners
- 83 Storage
- 743 Linux Distributions
- 80 Debian
- 67 Fedora
- 15 Linux Mint
- 13 Mageia
- 23 openSUSE
- 143 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 348 Ubuntu
- 461 Linux System Administration
- 39 Cloud Computing
- 70 Command Line/Scripting
- Github systems admin projects
- 90 Linux Security
- 77 Network Management
- 101 System Management
- 46 Web Management
- 64 Mobile Computing
- 17 Android
- 34 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 371 Off Topic
- 114 Introductions
- 174 Small Talk
- 19 Study Material
- 507 Programming and Development
- 285 Kernel Development
- 204 Software Development
- 1.8K Software
- 211 Applications
- 180 Command Line
- 3 Compiling/Installing
- 405 Games
- 309 Installation
- 97 All In Program
- 97 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)