【问题标题】:How to exit a postgres docker container from a bash script如何从 bash 脚本中退出 postgres docker 容器
【发布时间】:2020-04-08 00:36:32
【问题描述】:

我有一个 docker-compose.yml 配置为创建数据并将其添加到数据库。然后我的 compose 文件加载到一个 bash 脚本中,该脚本将对数据运行一些简短的测试,并在测试失败时退出 1。这按预期工作,但是当我希望我的容器在所有测试通过但 docker-entrypoint.sh 脚本似乎忽略我的 exit 0 命令时退出。我试图编写一个不同的入口点脚本,它将调用 docker-entrypoint.sh,希望我可以退出容器,但我没有任何运气。有没有简单的方法来实现这个?

输出示例:

postgres_1  | PostgreSQL init process complete; ready for start up.
postgres_1  |
postgres_1  | 2020-04-06 19:29:58.511 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1  | 2020-04-06 19:29:58.511 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres_1  | 2020-04-06 19:29:58.511 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres_1  | 2020-04-06 19:29:58.517 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1  | 2020-04-06 19:29:58.534 UTC [102] LOG:  database system was shut down at 2020-04-06 19:29:58 UTC
postgres_1  | 2020-04-06 19:29:58.540 UTC [1] LOG:  database system is ready to accept connections

【问题讨论】:

  • 容器中还有其他东西在运行吗?容器在其中的所有进程都死掉之前不会退出。
  • 查看我的编辑 - 它只是说数据库系统已准备好接受连接。我希望它在我所有的测试都通过后结束。

标签: bash postgresql docker


【解决方案1】:

这很正常; entrypoint 将忽略任何命令,为了解决这个问题,您可以在 Dockerfile 中的 entrypoint 声明后使用 CDM 参数。 比如Dockerfile中的如下sn -p

ENTRYPOINT ["/bin/echo", "Hello"]
CMD ["world"]

当容器运行时

docker run -it <image> 

将产生输出:

世界你好

但是当容器运行时

docker run -it <image> John

将导致:

你好约翰

【讨论】:

  • 感谢您的回复。所以在我的例子中,我什么时候想退出以确保 docker-entrypoint.sh 脚本可以在正确的时间退出?
  • 手动退出可以使用这个方法,如果你想在某事之后做某事应该在入口点为此编写bash脚本。 @Polyphase29
猜你喜欢
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 2023-01-28
  • 2017-02-21
  • 1970-01-01
  • 2017-12-01
相关资源
最近更新 更多