0

I have a simple java application that depends on MySQL.

This is how my docker-compose.yaml looks like:

version: "3.3"
services:
  docker-mysql:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=test
      - MYSQL_PASSWORD=root
    volumes:
      - /var/lib/mysql
    ports:
      - 3306:3306
  my-app:
    build: .
    depends_on:
      - docker-mysql
    ports:
      - 8080:8080

And this is my Dockerfile to create a containerized app, i.e. my-app:

FROM openjdk:8
EXPOSE 8080
ADD /target/Service1-0.0.1.jar Service1.jar
ENTRYPOINT ["java","-jar","Service1.jar"]

When I try docker-compose up, the application launches Tomcat successfully, but can't connect to MySQL container, since it launches after the app:

enter image description here Docker Compose Log

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
Sajad
  • 2,273
  • 11
  • 49
  • 92

2 Answers2

0

Normally, it's suppose to come up after the db container. What is the result of docker logs docker-mysql_1 --tail 25 -f Paste that as well.

Also, try to add a volumes section for your db container like so

environment:
  - MYSQL_ROOT_PASSWORD=root
  - MYSQL_DATABASE=test
  - MYSQL_PASSWORD=root
volumes:
  - /var/lib/mysql
Akongnwi Devert
  • 1,177
  • 12
  • 10
  • Thanks for mentioning `volumes`, See the updated question. – Sajad Sep 29 '18 at 12:40
  • Everything works just fine with the same configuration on my machine `devert@maviance:~/Projects/smobilpay-uats$ docker-compose up -d Creating network "smobilpay-uats_default" with the default driver Creating smobilpay-uats_docker-mysql_1 ... done Creating smobilpay-uats_my-app_1 ... done ` Don't know what you are doing wrong – Akongnwi Devert Sep 29 '18 at 20:21
  • I just say `docker-compose up` without it's rest `... -d Creating network ...` – Sajad Sep 30 '18 at 09:53
  • I'm not so sure what you mean. But `docker-compose up -d` is to bring up the containers from the images. the `d` flag is to start them in detached mode. Meaning you can close the terminal and containers won't shut down. – Akongnwi Devert Sep 30 '18 at 12:24
0

it says this very clearly in the doc https://docs.docker.com/compose/startup-order/ depends_on will only start the docker build in that order, it will not wait for the build to complete. You need to give your own command which will ensure that required services is started before you proceed with the next build. Or you can skip docker compose and simply use a bash script that executes docker run in the order you want it.

whitespace
  • 789
  • 6
  • 13