问题
My current 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}
My docker-compose.yml file contains 2 services. I have specified postgrasdb service to start before python-app service using depends_on but the docker-compose in not running the services in specified order.
How can I get postgrasdb service to be run before python-app service? I am running docker-compose up --build --remove-orphans command.
回答1:
Note that depends_on only waits for the other container to be up, but not for the process it is running to start. The thing that could probably be happening in your case is that you are trying to connect to the postgres process on its specified port while it's still getting started.
There are two ways you can tackle such a scenario -
Specify some sort of
restartclause for yourpython-appcontainer - You are probably seeing yourpython-appcontainer in failed state and so you have posted this question.restart: on-failure:10in the docker-compose.yml for yourpython-appservice will restart your container up to 10 times in case it fails connecting to thepostgrescontainer. This will ensure that you would have given it enough time before thepostgrescontainer is up and running...the process that is.Use an external tool like dockerize that allows you to wait on other services before starting up the container. This essentially gives you the behavior you desire with
depends_on.
回答2:
depends_on just guaranteed that database service run before web service.
To run web service after database is ready, use wait_for_it.sh script
https://docs.docker.com/compose/startup-order/
For example, i have a docker-compose.yml file with two services: app and db, and i want to run container app after db service are ready:
docker-compose.yml
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
In Dockerfile to build employee-jdbc image:
Dockerfile
FROM openjdk:8
COPY ./target/*.jar ROOT.jar
COPY wait-for-it.sh /wait-for-it.sh
RUN chmod +x /wait-for-it.sh
ENTRYPOINT ["./wait-for-it.sh","db:3306","--","java","-jar","ROOT.jar"]
wait_for_it.sh file you can download at: https://github.com/vishnubob/wait-for-it
来源:https://stackoverflow.com/questions/52699899/depends-on-doesnt-wait-for-another-service-in-docker-compose-1-22-0