0

building a container-based web-app right now and facing following problem:

My Spring app depends on a mariaDB container to be fully up and running. When my Spring container starts it throws a Connection Refused Exception.

 java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=waterloo-db)(port=9003)(type=master) : Socket fail to connect to host:waterloo-db, port:9003. Connection refused (Connection refused)
waterloo            |   at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73) ~[mariadb-java-client-2.6.1.jar!/:na]
waterloo            |   at org.mariadb.jdbc.internal.util.exceptions.ExceptionFactory.create(ExceptionFactory.java:192) ~[mariadb-java-client-2.6.1.jar!/:na]

Even on a reconnect time of 30 seconds I get the connection refused exceptions.

My Spring Config:

datasource:
      url: jdbc:mariadb://waterloo-db:9003/waterloo?autoReconnect=true
      username: waterloo
      password: xxx
      driver-class-name: org.mariadb.jdbc.Driver
      tomcat:
        test-on-borrow: true
        validation-query: SELECT 1
        validation-interval: 30000

docker compose:

#Waterloo
#waterloo-db
  waterloo-db:
    container_name: waterloo-db
    image: mariadb
    ports:
      - 9003:3306
    volumes:
      - ./waterlooDB:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: waterloo
      MYSQL_USER: waterloo
      MYSQL_PASSWORD: xxx
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "--silent"]
      
  waterloo:
    container_name: waterloo
    image: docker.pkg.github.com/magister-media/waterloo/waterloo:dev
    ports:
      - 8085:8085
      

I hope it's something you guys can help me with.

Cheers!

R. Polito
  • 544
  • 6
  • 21
  • is the container running before you start spring app? can you show us results of `docker ps` – J Asgarov Aug 14 '20 at 09:12
  • `b3d2a4f29059 docker.pkg.github.com/magister-media/waterloo/waterloo:dev "java -jar waterloo.…" 5 seconds ago Up 2 seconds 0.0.0.0:8085->8085/tcp waterloo 5 seconds ago Up 1 second (health: starting) 0.0.0.0:9003->3306/tcp waterloo-db` The mariadb *container* is running, the mariadb database is not running when starting the app. I need a way to make sure the database runs, not just the db container – R. Polito Aug 14 '20 at 11:20
  • 1
    You need to connect to the "normal" database port 3306; `ports:` have no effect on inter-container connections (and aren't required). There is also a startup-order dependency and you should read through [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) (`depends_on:` on its own is not enough). – David Maze Aug 14 '20 at 13:24
  • Why do I have to connect to port 3306 instead of 9005? Don't I do port forwarding from 9005(outside) to 3306(inside) ? – R. Polito Aug 14 '20 at 14:25

1 Answers1

0

add depends_on to waterloo app in the docker-compose file.

Example:

 waterloo:
    container_name: waterloo
    image: docker.pkg.github.com/magister-media/waterloo/waterloo:dev
    ports:
       - 8085:8085
    depends_on: 
       - "waterloo-db"

Docker docs. about depends_on:

Express dependency between services. Service dependencies cause the following behaviors:

docker-compose up starts services in dependency order. In the following example, db and redis are started before web. docker-compose up SERVICE automatically includes SERVICE’s dependencies...

  • Doesn't the depends_on flag just order the container? The container may be up but the Database itself is not, do you know what I mean? – R. Polito Aug 14 '20 at 14:23