0

I have an nginx Docker container which runs fine from the command line but which does not restart at boot. It is started with --restart=on-failure which I understand should also start it at boot (it certainly works that way on my other Docker containers).

docker run --name nginx  --restart=on-failure --detach --mount type=bind,source=/etc/ssl,target=/etc/ssl,readonly --mount type=bind,source=/etc/pki,target=/etc/pki,readonly --mount type=bind,source=/etc/letsencrypt,target=/etc/letsencrypt,readonly -p 443:443 -d nginx

Any ideas why this might be the case or how I find out what's upsetting it? docker logs nginx gives me nothing, just the log up to the end of the shut-down of the machine. This is on Centos 8 and, in case it matters, the nginx configuration file contains:

server {
    listen       443 ssl;
    listen  [::]:443 ssl;
    server_name  myurl.net;

    ssl_certificate        /etc/letsencrypt/live/myurl.net/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/myurl.net/privkey.pem;

    ssl_client_certificate /etc/ssl/certs/my_ca.crt;
    ssl_crl                /etc/ssl/CA/ca.crl;
    ssl_verify_client on;


    location / {
        proxy_pass http://172.17.0.1:8080/;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

...where the location line is on a local docker network that lets nginx talk with another docker container that runs the HTTP server which ultimately does the serving (nginx is just in the loop to perform client authentication via SSL). Could this network bit somehow be causing a problem?

Rob
  • 865
  • 1
  • 8
  • 21
  • 1
    If `--restart=on-failure` didn't restart it when it quit, it didn't fail, it just quit. Try `--restart=always` – tripleee Jan 11 '23 at 18:13
  • Aha! Well done, I guess the other containers I have don't exit cleanly while `nginx` does and hence I thought that `--restart=on-failure` did the trick. Confirmed that `--restart=always` solves my problem, many thanks! – Rob Jan 11 '23 at 19:00
  • Does this answer your question? [Restart Docker Containers when they Crash Automatically](https://stackoverflow.com/questions/40109247/restart-docker-containers-when-they-crash-automatically) – tripleee Jan 12 '23 at 05:55

1 Answers1

0

As @triplee points out, --restart=on-failure doesn't actually start the Docker container when the machine is rebooted, it only appears to do this in some cases if the Docker image in question doesn't exit gracefully at system shutdown: the image appears to fail and hence Docker will restart it when the machine comes back up.

The correct directive to ensure that a well-behaved Docker image, one which exits gracefully like the nginx one and hence doesn't raise an error, is to use --restart=always.

Rob
  • 865
  • 1
  • 8
  • 21