-4

I’m running Spring Boot, React, and MySQL inside Docker on a Hostinger VPS (Ubuntu 22.04) using the following docker-compose.yml:

version: '3.8'

services:
  mysql:
    image: mysql:8
    container_name: mysql_container
    environment:
      MYSQL_ROOT_PASSWORD: abcd@123
      MYSQL_DATABASE: tbappmasterdb
      MYSQL_USER: abcd
      MYSQL_PASSWORD: abcd@123
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - app-network

  springboot:
    build:
      context: ./track_your_business
    container_name: springboot_backend2_container
    depends_on:
      - mysql
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql:3306/tbappmasterdb
      SPRING_DATASOURCE_USERNAME: abcd
      SPRING_DATASOURCE_PASSWORD: abcd@123
    command: sh -c "sleep 20 && java -jar app.jar"
    ports:
      - "8082:8080"
    networks:
      - app-network

  react:
    build:
      context: ./trackbusiness_rct
    container_name: react_frontend2_container
    depends_on:
      - springboot
    ports:
      - "3001:80"
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

volumes:
  mysql_data:

⚠️ Problem:

When I run:

docker compose up -d

the MySQL container stays up, but the Spring Boot container exits with:

Communications link failure: The driver has not received any packets from the server.

What I’ve Checked:

Both containers are on the same network (app-network)

MySQL port 3306 is exposed

Tried using both mysql and mysql_container as hostnames

MySQL log shows it started correctly

Question:

What’s the correct way for Spring Boot to connect to the MySQL container inside the same Docker Compose network? Is my SPRING_DATASOURCE_URL correct or am I missing something in the setup?

Update: For security reasons, I replaced the real database username and password with dummy values ("abcd", "abcd@123") in the question. In my actual docker-compose.yml, both MySQL and Spring Boot use the same credentials.

0

1 Answer 1

-1

The problem is a simple credentials mismatch in your docker-compose.yml. Your mysql service is configured with these credentials:

MYSQL_USER: parth
MYSQL_PASSWORD: parth@123

But your springboot service is trying to connect with these:

SPRING_DATASOURCE_USERNAME: abcd
SPRING_DATASOURCE_PASSWORD: abcd@123

The Spring Boot application is being denied access because it's using the wrong username and password

Update

Have you tried healthcheck for mysql container?

services:
  mysql:
    image: mysql:8
    container_name: mysql_container
    environment:
      MYSQL_ROOT_PASSWORD: abcd@123
      MYSQL_DATABASE: tbappmasterdb
      MYSQL_USER: abcd
      MYSQL_PASSWORD: abcd@123
    ports:
      - "3306:3306"
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - app-network
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 30s

Maybe need to use also

depends_on:
  mysql:
    condition: service_healthy
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.