我当前的docker-compose.yml-
# This docker-compose file uses '.env' file present in the current directory,
# for database credentials. If you want to change the credentials please
# change the data in '.env'.
# '.env' file might be hidden as dot files are hidden please unhide to see it.
# Know more about '.env' file: https://docs.docker.com/compose/env-file/
version: '3'
services:
postgresdb:
image: postgres:9.5
environment:
POSTGRES_USER: ${ENV_POSTGRES_USER}
POSTGRES_PASSWORD: ${ENV_POSTGRES_PASSWORD}
POSTGRES_DB: ${ENV_POSTGRES_DB}
volumes:
- "../app/volumes/postgres/data:/var/lib/postgresql/data"
# This is python service. It uses python 3.6 as base image.
# It will build this service using the Dockerfile present in current directory
# To modify the values of environment variables please open '.env' file.
# This service will not run until postgresdb service gets started
python-app:
image: python:3.6
build: . # Builds using Dockerfile from current directory
depends_on:
- postgresdb
ports:
- "5001:5001"
tty: true
volumes:
- "../app/volumes/trained_knn_model.clf:/usr/src/app/my-app/trained_knn_model.clf"
- "../app/volumes/XYPickle.pickle:/usr/src/app/my-app/XYPickle.pickle"
environment:
- POSTGRES_USER=${ENV_POSTGRES_USER}
- POSTGRES_PASSWORD=${ENV_POSTGRES_PASSWORD}
- POSTGRES_HOST=${ENV_POSTGRES_HOST}
- POSTGRES_PORT=${ENV_POSTGRES_PORT}
- POSTGRES_DB=${ENV_POSTGRES_DB}
我的docker-compose.yml文件包含2个服务。我已指定 postgrasdb 服务在使用depends_on
的 python-app 服务之前启动,但docker-compose并未按指定的顺序运行服务。
如何使 postgrasdb 服务在 python-app 服务之前运行?我正在运行docker-compose up --build --remove-orphans
命令。
答案 0 :(得分:4)
请注意,depends_on
仅等待另一个容器启动,而不等待它正在运行的进程。在您的情况下,可能会发生的事情是,您尝试在postgres
进程仍处于启动状态的指定端口上连接到该进程。
有两种方法可以解决这种情况-
答案 1 :(得分:0)
depends_on 仅保证数据库服务先于Web服务运行。
要在数据库准备就绪后运行Web服务,请使用wait_for_it.sh脚本
https://docs.docker.com/compose/startup-order/
例如,我有一个具有两个服务的town
文件:docker-compose.yml
和app
,并且我想在db
服务准备就绪后运行容器app
:< / p>
docker-compose.yml
db
在 Dockerfile 中构建version: "2"
services:
app:
image: employee-jdbc
ports:
- "8080:8080"
networks:
- employee-mysql
depends_on:
- mysqldb
container_name: employee-jdbc
db:
image: mysql:5.7
networks:
- employee-mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=bootdb
container_name: mysqldb
networks:
employee-mysql:
external: true
映像:
Dockerfile
employee-jdbc
您可以从以下位置下载wait_for_it.sh 文件: https://github.com/vishnubob/wait-for-it