Home > front end >  Docker doesn't start container cleanly after a process monitored by healthcheck crashes
Docker doesn't start container cleanly after a process monitored by healthcheck crashes

Time:02-11

I have a docker-compose with several services. The monitored processs inside one of the containers crashes from time to time and healtcheck detects this and restarts the container. Service is configured: restart: unless-stopped But there is an issue because after the restart the pid of rsyslog is not deleted and it cannot start. I can workaround this by deleting the pid in the start script before starting the rsyslog but I don't think it should behave this way. Docker container id is the same after the restart. It behaves like it has committed the container after the process crashed and then it only run the start script again. E.g. the container doesn't start cleanly but the changes made inside stay. The pid location is not a volume. I read the docker documentation on the topic - https://docs.docker.com/config/containers/start-containers-automatically/ but couldn't find anything helpful. Any idea?

CodePudding user response:

A container restart doesn't actually delete and recreate the container or its filesystem; it just restarts the main process within the existing container.

Since a container only runs one process, it's safe to unconditionally delete the pid file. I'd tend to use an entrypoint wrapper script for this:

#!/bin/sh

# Delete the pid file, if it exists
rm -f /var/run/rsyslog.pid

# Run whatever the image CMD or `docker run` command is
exec "$@"

In your Dockerfile, make this script be the ENTRYPOINT, and leave your existing CMD unchanged:

COPY entrypoint.sh .
ENTRYPOINT ["./entrypoint.sh"]
CMD ["rsyslogd", "--no-daemon"]
  • Related