docker-gen: Restart unless stopped not working

I noticed restart: unless-stopped is not working in docker-gen. Do I need to use always instead? Why is it not working?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 12
  • Comments: 22 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Cause

SIGHUP is used to trigger generation. If the signal however is received before docker-gen is fully initialized the default golang handler will call os.Exit(2). I fixed this by ignoring SIGHUP until docker-gen is ready.

Fix

You can use the docker image based on my fork: joellinn/docker-gen. It’s also build with up to date dependencies.

(I don’t fancy advertising forks but this project unfortunately seems to be unmaintained…)

I’m hitting the same issue - docker-gen container + nginx container - both restart=always - if I restart the docker service, nginx comes back up but docker-gen remains stopped with an exit code of 2

Nothing obvious in the logs

Docker start will get the container running again

I have the same problem. But also “always” does not work. After restart of the server I have to startup docker-gen manually. How can I find the reason for the exited docker-image? Why does it not restart automatically when I use always?

Yep, exactly the same for me. Possible to fix this issue? It is very annoying in a production environment.

Same here. Ubuntu 18.04.

We’re having the same issue: nginx-gen randomly goes into the “stopped” state, even though we have a restart directive…

We worked around this problem by wrapping docker-gen in a shell script that executes docker-gen in a loop and traps SIGHUP and SIGTERM signals:

#!/bin/sh

echo container started
echo parameters: $@

hup() {
        echo SIGHUP received

        if [ -z "$docker_gen_pid" ]
        then
                echo no pid: signal ignored
        else
                echo sending signal to pid: $docker_gen_pid
                kill -HUP $docker_gen_pid
        fi

        wait
}

trap hup SIGHUP

term() {
        echo SIGTERM received

        if [ -z "$docker_gen_pid" ]
        then
                exit 0
        else
                echo killing pid: $docker_gen_pid
                kill $docker_gen_pid

                wait
                exit_code=$?
                echo docker-gen terminated with exit code: $exit_code
                exit $exit_code
        fi
}

trap term SIGTERM

while :
do
        echo starting docker-gen...
        /usr/local/bin/docker-gen $@ &
        docker_gen_pid=$!

        wait
        docker_gen_pid=

        echo docker-gen terminated with exit code: $?
        sleep 10
done