在 docker-compose 1.22.0 中,depends_on 不会等待另一个服务 [英] depends_on doesn't wait for another service in docker-compose 1.22.0

查看:19
本文介绍了在 docker-compose 1.22.0 中,depends_on 不会等待另一个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的 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 个服务.我已使用 depends_on 指定 postgrasdb 服务在 python-app 服务之前启动,但 docker-compose 未按指定顺序运行服务.如何在 python-app 服务之前运行 postgrasdb 服务?我正在运行 docker-compose up --build --remove-orphans 命令.

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.

推荐答案

请注意,depends_on 只等待另一个容器启动,而不等待它正在运行的进程启动.在您的情况下可能发生的事情是,您正在尝试在其指定端口上连接到 postgres 进程,而它仍在启动.

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 -

  1. 为您的 python-app 容器指定某种 restart 子句 - 您可能会在其中看到您的 python-app 容器失败状态,所以你发布了这个问题.restart: on-failure:10 在您的 python-app 服务的 docker-compose.yml 中,如果它无法连接到 python-app 服务,它将重新启动您的容器最多 10 次code>postgres 容器.这将确保您在 postgres 容器启动并运行之前有足够的时间......就是这个过程.

  1. Specify some sort of restart clause for your python-app container - You are probably seeing your python-app container in failed state and so you have posted this question. restart: on-failure:10 in the docker-compose.yml for your python-app service will restart your container up to 10 times in case it fails connecting to the postgres container. This will ensure that you would have given it enough time before the postgres container is up and running...the process that is.

使用像 dockerize 这样的外部工具,您可以在启动容器之前等待其他服务.这基本上为您提供了 depends_on 所需的行为.

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.

这篇关于在 docker-compose 1.22.0 中,depends_on 不会等待另一个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆