4

As in the title:

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

This worked for me sometime ago but now unfortunately not. Script that I execute contains:

mvn clean install -> docker-compose build -> docker-compose up

Dockerfile:

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

docker-compose.yaml

version: '3'

services:
  mysql-standalone:
    image: mysql:latest
    environment:
    - MYSQL_ROOT_PASSWORD=password
    - MYSQL_DATABASE=grades
    - MYSQL_USER=root
    - MYSQL_PASSWORD=password
    ports:
    - "33061:3306"
    volumes:
    - /data/mysql
  grades:
    image: grades
    build:
      context: ./
      dockerfile: Dockerfile
    depends_on:
    - mysql-standalone
    ports:
    - 8080:8080
    volumes:
    - /data/grades

And application.properties:

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${MYSQL_DB:aws_eb_db}
spring.datasource.username=${MYSQL_USERNAME:root}
spring.datasource.password=${MYSQL_PASSWORD:password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Mateusz Gebroski
  • 1,274
  • 3
  • 26
  • 57

3 Answers3

1

localhost for you Docker container is not the localhost of the host machine (the one where your Docker containers live). Basically it points to your Docker container itself, where MySQL doesn't live. So you have to point to your MySQL instance, or the host for your containers, as you are mapping the 3306 port of your MySQL to your host's 3306 port.

I would definitely point to the MySQL itself as @LinPy suggested:

spring.datasource.url=jdbc:mysql://mysql-standalone:3306/grades
Hasan Can Saral
  • 2,950
  • 5
  • 43
  • 78
0

This should help somebody.

Make sure to run the command below to invalidate the current image:

docker rmi -f <your image id>

version: '3.1'

services:

  db:
    image: mariadb:10.5.5
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=softeasydb
      - MYSQL_USER=root
      - MYSQL_PASSWORD=root
    volumes:
      - ./db:/var/lib/mysql
    ports:
      - 3306:3306
    networks:
      - common-network   

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080
    networks:
      - common-network
      
  api-users:
    build: .
    depends_on: 
      - db
    ports:
      - 9090:9090   
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - DATABASE_HOST=db
      - DATABASE_USER=root
      - DATABASE_PASSWORD=root
      - DATABASE_NAME=softeasydb
      - DATABASE_PORT=3306
      - SERVER_PORT=9090
    restart: always
    networks:
      - common-network

networks:
  common-network:
      driver: bridge
viniciusalvess
  • 756
  • 8
  • 18
0

In your docker-compose.yaml you have a typo in the port mappings for the mysql-standalone container, you need to change the following:

ports:
- "33061:3306"

to

ports:
- "3306:3306"
Ellis
  • 1
  • 1