简体   繁体   English

启动Postgres DB后Docker容器立即停止

[英]Docker container stops right after starting Postgres DB

I am working on setting up Postgres 9.5 AS in Docker, and got everything installed. 我正在Docker中设置Postgres 9.5 AS,并安装了所有东西。 The issue however is, when I start the Docker Container, it appears that Postgres starts at first, but then the Container stops right away. 但是,问题是,当我启动Docker容器时,似乎Postgres首先开始,但随后容器立即停止。 (it does not show up with a docker container ls .). (它不会与docker container ls一起显示docker container ls 。)。 When I overwrite the Container startup with --entrypoint sh , and manually start Postgres, it all works fine. 当我使用--entrypoint sh覆盖Container启动并手动启动Postgres时,一切正常。

I also checked with docker logs <container-id> , but that does not give me any info at all. 我还检查了docker logs <container-id> ,但这根本没有给我任何信息。

The setup is like this : 设置是这样的:

Dockerfile : Dockerfile的:

ENTRYPOINT ["/opt/edb/9.5AS/bin/init.sh"]

init.sh : init.sh:

su enterprisedb -c '/opt/edb/9.5AS/bin/pg_ctl start -D /opt/edb/9.5AS/data'

From my command prompt I run : 在命令提示符下运行:

docker run -it -v pgdata:/opt/edb/9.5AS/data <image_name>

It almost looks like it does start, but as soon as the start process is done, the shell stops, and as a result the Container stops as well. 看起来好像确实在启动,但是一旦启动过程完成,shell就会停止,因此Container也将停止。

So how to get it so the Container starts, Postgres starts and everything stays running, preferable in detached mode of course? 那么,如何获得它以使Container启动,Postgres启动并且一切保持运行,当然最好在分离模式下进行?

After researching some more, I found the answer in part also by finding clues on Stackoverflow. 在研究了更多内容之后,我还通过找到有关Stackoverflow的线索找到了部分答案。

Anyway, I modified my init.sh script to look like this : 无论如何,我修改了init.sh脚本,使其看起来像这样:

/opt/edb/9.5AS/bin/pg_ctl start -D /opt/edb/9.5AS/data exec
"$@"

And the Dockerfile now ends like below : Dockerfile现在如下结束:

USER enterprisedb
ENTRYPOINT ["/opt/edb/9.5AS/bin/init.sh"]
CMD ["/bin/bash"]

The core of the solution is the last line in the init.sh script as well as the last line in the Dockerfile. 解决方案的核心是init.sh脚本的最后一行以及Dockerfile的最后一行。 Both combined make it so that once the DB started, a new shell (/bin/bash) gets started. 两者的结合使得数据库一旦启动,便会启动一个新的shell(/ bin / bash)。 This will run in the foreground, thus keeping the Container alive. 这将在前台运行,从而使Container保持活动状态。 By starting the container in detached mode, it now does exactly what we need it to do. 通过以分离模式启动容器,它现在可以完全执行我们需要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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