简体   繁体   中英

Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose

Trying to connect Springboot app dicom_viewer: Imagename: sample with Mysql: Imagename: pb_mysql running in docker container. Error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure.

Application.properties file:

#MySQL database connection strings
spring.datasource.url=jdbc:mysql://pb_mysql:3306/test?autoReconnect=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=pb
spring.datasource.password=pb@123

#JPA property settings
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true

Docker-compose file:

version: '2'
services:
  pb_mysql:
    container_name: pb_mysql
    restart: always
    image: mysql/mysql-server:latest
    environment:
      MYSQL_ROOT_PASSWORD: 'root123' # TODO: Change this
    volumes:
      - sql-datavolume:/var/lib/mysql
  patient_browser:
    container_name: patient_browser
    restart: always
    image: patient_browser
    ports: 
      - 8080:8080
    volumes:
      - ./dicom_images:/usr/src/app/dicom_images
  springboot-image:
    container_name: dicom_viewer
    restart: always
    image: sample
    ports: 
      - 8081:8080
volumes:
  sql-datavolume:
networks:
  f4:
    driver: bridge

Dockerfile:

FROM openjdk:8-jdk-alpine

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY ./target/springboot-image-0.0.1-SNAPSHOT.jar /usr/src/app

COPY . /usr/src/app

EXPOSE 8080

ENTRYPOINT ["java","-jar","springboot-image-0.0.1-SNAPSHOT.jar"]

Database: test, table: pm_of_india

Docker images, docker ps -a

docker-compose up

Error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

There are two things to notice,

depends_on

You kept pb_mysql ahead of others in the yaml configuration file. This does not ensure the mysql-server to be up and running ahead of others.

You want to use depends_on , so the depended pb_mysql will be initiated first.

ports

You are using bridged network, so you need to map 3306 to 3306 for mysql-server.

version: '2'
services:
  patient_browser:
    container_name: patient_browser
    restart: always
    image: patient_browser
    ports: 
      - 8080:8080
    volumes:
      - ./dicom_images:/usr/src/app/dicom_images
      - 8081:8080
    depends_on:
      - pb_mysql
  springboot-image:
    container_name: dicom_viewer
    restart: always
    image: sample
    ports: 
      - 8081:8080
    depends_on: # Let mysql-server start first
      - pb_mysql
  pb_mysql:
    container_name: pb_mysql
    ports:
      - "3306:3306" # Port mapping.
    restart: always
    image: mysql/mysql-server:latest
    environment:
      MYSQL_ROOT_PASSWORD: 'root123' # TODO: Change this
    volumes:
      - sql-datavolume:/var/lib/mysql
volumes:
  sql-datavolume:
networks:
  f4:
    driver: bridge

I would avoid using restart: always unless I absolutely need it.

The rest looks good to me. Let me know if it is resolved.

You are not referencing your network. Try adding it to your services ie:

services:
  pb_mysql:
     networks:
          - f4

...
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM