等待Docker容器准备就绪
我有以下docker-compose.yml
:
version: '2' services: server: build: . command: ["./setup/wait-for-postgres.sh", "tide_server::5432", "cd /app", "npm install", "npm run start"] ports: - 3030:3030 links: - database depends_on: - database database: image: postgres environment: - "POSTGRES_USER=postgres" - "POSTGRES_PASSWORD=postgres" - "POSTGRES_DB=tide_server" ports: - 5432:5432
我试着按照这个教程 ,使用下面的shell脚本来确定postgres
何时准备好。
#!/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是最小的,但我已经添加它作为参考:
FROM node:latest ADD . /app WORKDIR /app EXPOSE 3030
现在当我尝试运行docker-compose up
我得到以下(在postgres容器准备就绪后:
server_1 | Postgres is unavailable - sleeping server_1 | ./setup/wait-for-postgres.sh: line 10: psql: command not found server_1 | Postgres is unavailable - sleeping server_1 | ./setup/wait-for-postgres.sh: line 10: psql: command not found server_1 | Postgres is unavailable - sleeping server_1 | ./setup/wait-for-postgres.sh: line 10: psql: command not found server_1 | Postgres is unavailable - sleeping server_1 | ./setup/wait-for-postgres.sh: line 10: psql: command not found
现在我不知道这是一个链接问题,还是我的脚本有问题,但我已经尝试了所有我能想到的变化,并没有运气得到这个/
当您在docker-compose.yml
声明command
时,命令本身在声明为启动的容器中执行。捕捉我的漂移?
您的./setup/wait-for-postgres.sh
正在容器中执行,没有安装postgres。 而你不能改变它。
但是,不,实际上你可以。 在postgres容器中运行脚本。 但是如果你在database
部分定义command
,它将覆盖在postgres:latest
定义的默认CMD
,这只是CMD ["postgres"]
。
这意味着,你必须稍微改写你的脚本:
#!/bin/bash # wait-for-postgres.sh set -e host="$1" shift cmd="$@" postgres 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