depends_on不等待docker-compose 1.22.0中的其他服务 [英] depends_on doesn't wait for another service in docker-compose 1.22.0

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

问题描述

我当前的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不在运行按指定顺序提供服务。
如何在 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 容器的code> restart 子句-您可能会看到 python-app 容器处于失败状态,因此您已发布此问题。 重新启动:失败:10 在您的 python-app 服务的docker-compose.yml中将重新启动容器如果连接到 postgres 容器失败,则最多可达10次。这将确保您在 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.

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

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