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

Executing wait-for-it.sh in python Docker container

我有一个 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=""
 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

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