Welcome to the Linux Foundation Forum!

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:

  1. Installed podman on the system from root account:

dnf install podman -y

  1. 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

  1. 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

  1. 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 reboot
systemctl status podman.service
systemctl start podman.service

  1. Once service was started manually container haven't started .

systemctl enable podman.service - Didn't worked
docker container ls - Showed me that container is not running

  1. 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

  1. Then I reloaded systemctl and enabled new service and rebooted after

systemctl daemon-reload
systemctl enable httpddocker.service
shutdown -r now

  1. 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

  • luisviveropena
    luisviveropena Posts: 1,249
    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.

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.target

    Previous 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
    
  • 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.

Categories

Upcoming Training