简体   繁体   English

Docker-compose服务启动顺序

[英]Docker-compose services starting order

I'm trying to start 3 docker containers in a sequential order using the docker-compose. 我正在尝试使用docker-compose按顺序启动3个docker容器。 I have the following containers: 我有以下容器:

  • app-service-db container -> Database app-service-db容器->数据库
  • config-service container -> Spring boot application config-service容器-> Spring Boot应用程序
  • app-service container -> Spring boot application 应用程序服务容器-> Spring Boot应用程序

I want the ' app-service ' container to start only when the other two containers have finish starting. 我希望仅在其他两个容器完成启动后才能启动“ app-service”容器。 I'm using the ' wait-for ' script to wait for the services to become available ( https://github.com/Eficode/wait-for ). 我正在使用“ wait-for”脚本来等待服务变得可用( https://github.com/Eficode/wait-for )。

Here is what I have inside the docker-compose.yml 这是我在docker-compose.yml中拥有的

    version: '3.4'
services:
    config-service:
      image: "config-service:1.0"
      hostname: config-service
      container_name: config-service
      build:
        context: ../config
        dockerfile: config.dockerfile
      expose:
        - "8888"
      logging:
        driver: json-file
    app-service-db:
      image: "app-service-db:10.3"
      hostname: app-service-db
      container_name: app-service-db
      build:
        context: ../app-service
        dockerfile: app-db.dockerfile
      environment:
        MYSQL_ROOT_PASSWORD: password
        MYSQL_DATABASE: app
      expose:
        - "3306"
      logging:
        driver: json-file  
    app-service:
        image: "app-service:1.0"
        hostname: app-service
        container_name: app-service
        build:
          context: ../app-service
          dockerfile: app.dockerfile
        ports:
          - "8080:8080"
        expose:
          - "8080"
        logging:
          driver: json-file
        command: sh -c './wait-for app-service-db:3306 && config-service:8888'  
        depends_on:
          - config-service
          - app-service-db

config.dockerfile config.dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/config-1.0-final.jar config.jar
ENTRYPOINT ["java", "-jar", "config.jar"]
EXPOSE 8888

app-db.dockerfile APP-db.dockerfile

FROM mariadb:10.3

app.dockerfile app.dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/app-1.0-final.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
EXPOSE 8080

When I run the ' docker-compose up ' cmd the ' app-service ' container is starting before the ' app-service-db ' and ' config-service ' have finished and is exited because it can't find any connection. 当我运行“ docker-compose up” cmd时,“ app-service”容器在“ app-service-db”和“ config-service”完成之前启动,并退出,因为它找不到任何连接。 How can I make this work and force the ' app-service ' container to start only when the other two containers have started. 我该如何进行这项工作,并仅在其他两个容器启动后才强制启动“ app-service”容器。

Thanks in advance. 提前致谢。

You can ensure the start ordering of your services defined in your docker-compose.yml with the depends_on keyword: https://docs.docker.com/compose/compose-file/#depends_on 您可以确保使用depends_on关键字在depends_on docker-compose.yml定义的服务的开始顺序: https : depends_on

This is a Docker runtime thing. 这是Docker运行时的事情。 You don't specify this in a Dockerfile , from which you build your Docker image. 您无需在Dockerfile指定此Dockerfile ,就可以从该文件中构建Docker映像。

Docker does not wait for your config and db services to finish starting, before starting your service. Docker在启动服务之前不会等待您的配置和数据库服务完成启动。 But Docker has a article in the official documentation explaining two alternatives to solve this use-case: https://docs.docker.com/compose/startup-order/ 但是Docker在官方文档中有一篇文章解释了解决此用例的两种方法: https : //docs.docker.com/compose/startup-order/

  1. Use tools as wait-for-it ( https://github.com/vishnubob/wait-for-it ) or dockerize ( https://github.com/jwilder/dockerize ) 使用工具作为wait-for-ithttps://github.com/vishnubob/wait-for-it )或dockerizehttps://github.com/jwilder/dockerize
  2. or write your own wait script 或编写自己的等待脚本

In the article https://docs.docker.com/compose/startup-order/ are examples for both solutions. 文章https://docs.docker.com/compose/startup-order/中是这两种解决方案的示例。

I see more problems in your configs. 我在您的配置中看到更多问题。 There is problem with entrypoint and command relation, to fully uderstand that please see this What is the difference between CMD and ENTRYPOINT in a Dockerfile? 入口点和命令关系存在问题,要完全理解请参阅此内容。Dockerfile中的CMD和ENTRYPOINT有什么区别? So your entrypoint is to run java application - that's probably why wait is not working. 因此,您的切入点是运行Java应用程序-这可能就是为什么wait无法正常工作的原因。 I would suggest to override entrypoint for app-service by adding following section in docker-compose: 我建议通过在docker-compose中添加以下部分来覆盖应用程序服务的入口点:

entrypoint: sh -c './wait-for app-service-db:3306 && ./wait-for config-service:8888 && java -jar app.jar'

Also I can't see where you are adding this script wait-for into docker image (missing ADD in app's dockerfile?) 我也看不到您正在将此脚本等待添加到Docker映像的位置(在应用程序的dockerfile中缺少ADD吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM