David Salas Boscan
David Salas Boscan

Reputation: 101

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.

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:

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:

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.

This is the github repository of the proyect: https://github.com/dsalasboscan/exam

I need to instantiate Mongodb first and THEN start with the maven build and java image generation.

Upvotes: 3

Views: 4280

Answers (1)

sayboras
sayboras

Reputation: 5165

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. It's fairly easy to use.

Upvotes: 2

Related Questions