简体   繁体   中英

Preventing the Docker container from exiting when the main process dies

我正在使用带有 repmgr 的 Postgres,我遇到的一个小问题是有时 repmgr 必须停止并启动 Postgres 服务,这只会杀死容器,我在 Dokcerfile 中在线尝试了一些解决方案,但似乎没有工作,有什么我可以添加到 docker-compose 文件中以防止 docker 立即退出,我不想永远活着,但也许几分钟?

Remember that docker-composer is mostly development thing. For production there are other ways, like kubernetes.

The only solution i know is to run our own .sh script as main process, which would have infinite loop with necessary checks in it.

This way you can control how to check - like ps aux and grep what you need. and exit main process if you need to by doing logic.

sh scrip would look something like:

while sleep 180; do
  ps aux |grep postgres_service_name |grep -v grep
  POSTGRESS=$?

  if [ $POSTGRESS -ne 0 ]; then
# do what you need before exiting whole container
    exit 1
  fi

done

make sure you replace postgres_service_name with real name of Postgres service on linux.

Use that script as a startup script in docker compose or whatever you would use in prod.

if you really need 2 minutes before it is off - i would implement logic which would measure time after first time process is not there

The way docker is designed it will start a new container by starting the command specified as entrypoint/command to it and when this process terminates docker will kill all remaining processes in that container and shut it down.

So to keep the container running while the Postgres process is restarted you need to have another command running as the root process in the container.

You can achieve this by writing a simple shell script as a wrapper which will only exit when no Postgres process is running anymore or by using a dedicated init tool such as supervisord .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM