在python Docker容器中执行wait-for-it.sh

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

泊坞窗,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 

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

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

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

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