【问题标题】:Executing wait-for-it.sh in python Docker container在 python Docker 容器中执行 wait-for-it.sh
【发布时间】:2017-06-05 21:19:01
【问题描述】:

我有一个 Python docker 容器,需要等到另一个容器(postgres 服务器)完成设置。我尝试了标准的 wait-for-it.sh,但没有包含几个命令。我尝试了基本睡眠(再次在 sh 文件中),但现在它在尝试最终执行我正在等待的命令时报告 exec: 300: not found

如何解决这个问题(最好不要更改图像,或者不必扩展图像。)

我知道我也可以只运行一个 Python 脚本,但理想情况下,我想使用 wait-for-it.sh 来等待服务器启动,而不仅仅是休眠。

Dockerfile(用于填充器):

 FROM python:2.7.13

 ADD ./stuff/bin /usr/local/bin/
 ADD ./stuff /usr/local/stuff
 WORKDIR /usr/local/bin

 COPY requirements.txt /opt/updater/requirements.txt
 COPY internal_requirements.txt /opt/stuff/internal_requirements.txt

 RUN pip install -r /opt/stuff/requirements.txt
 RUN pip install -r /opt/stuff/other_requirements.txt

docker-compose.yml:

 version: '3'
 services:
   local_db:
     build: ./local_db
     ports:
     - "localhost:5432:5432"

   stuffer:
     build: ./
     depends_on:
     - local_db
     command: ["./wait-for-postgres.sh", "-t", "300", "localhost:5432", "--", "python", "./stuffing.py", "--file", "./afile"]

我想使用的脚本(但不能因为没有 psql 或 exec):

 #!/bin/bash
 # wait-for-postgres.sh

 set -e

 host="$1"
 shift
 cmd="$@"

 until psql -h "$host" -U "postgres" -c '\l'; do >&2 echo "Postgres is unavailable - sleeping"
   sleep 1
 done

 >&2 echo "Postgres is up - executing command"
 exec $cmd

【问题讨论】:

  • 请给我看一下 Dockerfile 和入口点好吗?
  • 根据需要安装 bash 命令的最佳方式是什么。我知道理论上我可以在 python 映像中安装 postgres 以获得 psql,但我不应该安装 exec。
  • 尝试将行 command: ["./wait-for-postgres.sh", "-t", "300", "localhost:5432", "--", "python", "./stuffing.py", "--file", "./afile"] 更改为 command: ["./wait-for-postgres.sh", "localhost:5432", "-t", "300", "--", "python", "./stuffing.py", "--file", "./afile"] 等待它预计第一个参数将是主机名:端口
  • 我想我看到了这个问题。就像你说的那样。 “300”不是 exec 的命令。只是看着它,我以为那是某处的行号。为这个愚蠢的问题道歉。

标签: postgresql docker exec docker-compose wait


【解决方案1】:

谢尔盖的评论。我的论点顺序错误。这个问题与 docker 无关,与我无法阅读有关。

【讨论】:

    【解决方案2】:

    我做了一个例子,你可以看到它的工作原理:

    https://github.com/nitzap/wait-for-postgres

    另一方面,您也可以在脚本执行过程中出现错误,以验证服务是否正常工作。你不应该引用 localhost .... 因为这是在容器的上下文中,如果你想指向另一个容器必须通过服务的名称。

    【讨论】:

    • 你能解释一下吗?我的目标是将 postgres 容器绑定到主机上的 localhost,然后第二个容器连接到它。 (这些容器的目的是有一个标准的本地开发数据库设置)。
    • 如果可以看到项目,应用程序容器预计 postgres 容器已准备就绪。现在如果你想单独使用基础,端口也会像本地一样发布。
    猜你喜欢
    • 2019-01-30
    • 2018-08-07
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-18
    相关资源
    最近更新 更多