Barney Stinson
Barney Stinson

Reputation: 1002

Docker Mysql + Spring Connection Refussed

i setup a Mysql Server via Docker and while Running my Microservices inside of my IDE everything worked fine and i was able to connect to my Database via

spring.datasource.url= jdbc:mysql://docker:3079/vetdb?useSSL=false

Now i wanted to Test-Deploy my Microservices via Docker

Docker-Compose

version: '3'

services:
  vetdb:
    container_name: vetdb
    hostname: vetdb
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=vetdb
      - MYSQL_PASSWORD=root
    ports:
      - 3079:3306
    volumes:
      - ./mysql-data:/var/lib/mysql

  VET:
    hostname: ${VET_HOSTNAME}
    container_name: ${VET_HOSTNAME}
    image: openjdk:8-jdk-alpine
    ports:
      - "${VET_PORT}:8080"
    volumes:
      - "/dockervolumes/microservices/VET/tmp:/tmp"
      - "./jar:/jar"
    entrypoint:
      - "java"
      - "-Djava.security.egd=file:/dev/./urandom"
      - "-jar"
      - "/jar/${VET_JAR_NAME}.jar"
    depends_on:
      - vetdb
    links:
      - vetdb:vetdb

and therefore i changed the datasource url

spring.datasource.url= jdbc:mysql://vetdb:3079/vetdb?useSSL=false

Now when i run docker-compose up --build i get Caused by: java.net.ConnectException: Connection refused (Connection refused)

Application.Properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url= jdbc:mysql://vetdb:3079/vetdb?useSSL=false
spring.datasource.username=root  
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.platform=mysql

Im fairly new to Docker and i hope here is somebody that can help me. Sorry for my horrible english

Ty in advance

EDIT Thanks to @David Maze for the Solution! spring.datasource.url= jdbc:mysql://vetdb:3306/vetdb?useSSL=false

Docker Compose

version: '3'

services:
  vetdb:
    container_name: vetdb
    hostname: vetdb
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=vetdb
      - MYSQL_PASSWORD=root
    volumes:
      - ./mysql-data:/var/lib/mysql

  VET:
    hostname: ${VET_HOSTNAME}
    container_name: ${VET_HOSTNAME}
    image: openjdk:8-jdk-alpine
    ports:
      - "${VET_PORT}:8080"
    volumes:
      - "/dockervolumes/microservices/VET/tmp:/tmp"
      - "./jar:/jar"
    entrypoint:
      - "java"
      - "-Djava.security.egd=file:/dev/./urandom"
      - "-jar"
      - "/jar/${VET_JAR_NAME}.jar"
    depends_on:
      - vetdb

Upvotes: 1

Views: 1469

Answers (1)

David Maze
David Maze

Reputation: 158848

When you connect between containers using their container names as host names, you have to use the port number the server is running on directly. It doesn't matter if any ports have been published (docker run -p, Docker Compose ports: line); you'd use the second port number from these lines.

In your case that means using the standard MySQL port 3306 (even though you've published to a different port on the host)

jdbc:mysql://vetdb:3306/vetdb?useSSL=false

Upvotes: 3

Related Questions