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
3
  • try this: spring.datasource.url=jdbc:mysql://mysql-standalone:3306/grades localhost will not work since it refer to the grades container itself Commented Sep 18, 2019 at 9:27
  • why this works? Commented Sep 18, 2019 at 9:28
  • you need to connect using the service name which is defined in compose Commented Sep 18, 2019 at 9:29

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.