简体   繁体   English

Docker-Compose - 控制 docker-compose 执行顺序

[英]Docker-Compose - Control docker-compose execution order

I need to control the order of Docker containers instantiation, the problem is that I want to build a Jar file with the Docker maven container then pass that jar to an OpenJDK Docker container in order to build an image and then instantiate a MongoDB container and a Java-App container with the OpenJDK image generated before that communicates between them via docker-compose.我需要控制 Docker 容器实例化的顺序,问题是我想用 Docker maven 容器构建一个 Jar 文件,然后将该 jar 传递给 OpenJDK Docker 容器以构建一个镜像,然后实例化一个 MongoDB 容器和一个带有之前生成的 OpenJDK 映像的 Java-App 容器通过 docker-compose 在它们之间进行通信。

The problem is the Build always fails because some of the Unit tests talk to the database before it's initialized and since the tests fail the build also fails.问题是构建总是失败,因为某些单元测试在初始化之前与数据库对话,并且由于测试失败,构建也会失败。

This is my dockerfile:这是我的 dockerfile:

FROM maven:3.5-alpine
COPY ./ /app
RUN cd /app && mvn package

FROM openjdk:8
COPY spring-rest-iw-exam.jar /tmp/spring-rest-iw-exam.jar
EXPOSE 8087
ENTRYPOINT ["java", "-jar", "/tmp/spring-rest-iw-exam.jar"]

This is my Docker-Compose:这是我的 Docker-Compose:

version: '2'
services:
  mongodb:
    image: mongo
    container_name: iw_exam_mongo
    restart: always
    ports:
    - "27017:27017"
    environment:
    - MONGO_INITDB_DATABASE=fizz_buzz_collection
    volumes:
    - /opt/iw-exam/data:/data/db
  spring-app:
    container_name: iw_exam_java_rest_api
    build: ./
    restart: always
    ports:
    - "8087:8087"
    depends_on:
    - mongodb

I tried with depends_on and did some other tests with a tool call dockerize but none of it works, the maven build always starts before docker-compose even start to instantiate mongodb.我尝试使用depends_on 并使用工具调用dockerize 进行了一些其他测试,但没有一个有效,maven 构建总是在docker-compose 甚至开始实例化mongodb 之前启动。

This is the github repository of the proyect: https://github.com/dsalasboscan/exam这是项目的github仓库: https : //github.com/dsalasboscan/exam

I need to instantiate Mongodb first and THEN start with the maven build and java image generation.我需要先实例化 Mongodb,然后从 maven 构建和 java 图像生成开始。

I came across similar problem before, and would like to share my experience.我以前遇到过类似的问题,想分享我的经验。

Basically, we need to wait for a while to make sure mongodb is completely boot up, here is the tool that you can leverage.基本上,我们需要等待一段时间以确保 mongodb 完全启动,这是您可以利用的工具 It's fairly easy to use.它很容易使用。

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

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