12

I'm trying to set up a Spring Boot application that depends on a MySQL database called teste in docker-compose. After issuing docker-compose up, I'm getting:

Caused by: java.net.ConnectException: Connection refused (Connection refused)

I'm running on Linux Mint, my docker-compose version is 1.23.2, my Docker version is 18.09.0.

application.properties

# JPA PROPS
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

spring.datasource.url=jdbc:mysql://db:3306/teste?useSSL=false&serverTimezone=UTC
spring.datasource.username=rafael
spring.datasource.password=password

spring.database.driverClassName =com.mysql.cj.jdbc.Driver

docker-compose.yml

version: '3.5'
services:
  db:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=rootpass
      - MYSQL_DATABASE=teste      
      - MYSQL_USER=rafael
      - MYSQL_PASSWORD=password
    ports:
      - 3306:3306
  web:
    image: spring-mysql
    depends_on:
      - db
    links:
      - db
    ports:
      - 8080:8080
    environment:
      - DATABASE_HOST=db
      - DATABASE_USER=rafael
      - DATABASE_NAME=teste
      - DATABASE_PORT=3306

and the Dockerfile

FROM openjdk:8
ADD target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197

3 Answers3

6

I was facing the same issue and in case you do not want to use any custom scripts, this can easily be resolved using health checks along with depends on. A sample using these is as follows:

services:
  mysql-db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=vikas1234
      - MYSQL_USER=vikas
    ports:
      - 3306:3306
    restart: always
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
      timeout: 20s
      retries: 10

  app:
    image: shop-keeper
    container_name: shop-keeper-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    depends_on:
      mysql-db:
        condition: service_healthy
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql-db:3306/shopkeeper?createDatabaseIfNotExist=true
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: vikas1234
Vikas Kumar
  • 157
  • 1
  • 7
5

Docker compose always starts and stops containers in dependency order, or sequential order in the file if not given. But docker-compose does not guarantee that it will wait till the dependency container is running. You can refer here for further details. So the problem here is that your database is not ready when your spring-mysql container tries to access the database. So, the recommended solution is you could use wait-for-it.sh or similar script to wrap your spring-mysql app starting ENTRYPOINT.

As example if you use wait-for-it.sh your ENTRYPOINT in your Dockerfile should change to following after copying above script to your project root:

ENTRYPOINT ["./wait-for-it.sh", "db:3306", "--", "java", "-jar", "app.jar"]

And two other important thing to consider here is:

  • Do not use links they are deprecated you should use user-defined network instead. All services in docker-compose file will be in single user-defined network if you don't explicitly define any network. So you just have to remove the links from compose file.
  • You don't need to publish the port for docker container if you only use it inside the user-defined network.
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Hansika Weerasena
  • 3,046
  • 1
  • 13
  • 22
  • 1
    Thanx a lot for this answer that helped me get my docker-compose work. However I have lost a lot of time trying this without success, because the default timeout of 15 seconds in `wait-for-it.sh` was not enough, so I had to increase this. I would not have lost that much time if I knew that this timeout can be changed, can you please update your answer to tell users that default timeout can be changed. – Moussa Aug 27 '19 at 17:03
  • well at the least, @Moussa, I have read your comment and immediately increased the timeout interval :) – pzelenovic Dec 05 '20 at 06:49
  • THANK YOU! I've been trying this for hours. In the docker compose up logs it always showed Mysql Started before the app kicked in so I assumed mysql completely starts, but I was still getting Connection refused! Only by using this script I was able to get it running! BTW on your host change permissions to that .sh script to 777 otherwise you'll get permission errors. – ACV Jun 11 '21 at 22:44
1

Your config looks nice, I would just recommend:

  • Remove links: db. It has no value in user-defined bridge networking
  • Remove port exposing for db unless you want to connect from outside docker-compose - all ports are exposed automatically inside user-defined bridge network.

I think the problem is that database container takes more time to start than web. depends_on just controls the order, but does not guarantee you database readiness. If possible, set several connection attempts or put socket-wait procedure in your web container.

grapes
  • 8,185
  • 1
  • 19
  • 31
  • This indeed sounds like an issue with the mysql container not fully having started yet. You can look into https://github.com/jwilder/dockerize to wait. You'll have to add this application to the image and then wait before starting the java app. – Gerben Jongerius Jan 11 '19 at 11:18