简体   繁体   中英

Can't connect to MySQL from Spring-Boot container

I'm trying to connect my Spring-Boot application (one container) to a MySQL database (another container). I have following error while starting my Java app:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Here is my docker-compose.yml :

version: "3"
services:
  db:
    image: mysql:8
    environment:
      - MYSQL_ROOT_PASSWORD=family
      - MYSQL_DATABASE=family_db
      - MYSQL_USER=family_user
      - MYSQL_PASSWORD=family
    ports:
      - 9999:3306
  web:
      build:
        context: .
        dockerfile: Dockerfile-spring
      image: family:latest
      depends_on:
        - db
      ports:
        - 0:8080
      environment:
#        - DATABASE_HOST=db
        - DATABASE_USER=family_user
        - DATABASE_PASSWORD=family
        - DATABASE_NAME=family_db
        - DATABASE_PORT=3306
#        - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/family_db?useUnicode=true&characterEncoding=utf8&useSSL=false
#        - spring.datasource.url=jdbc:mysql://mysql:3306/test?useSSL=false
#        - spring.datasource.username=family_user
#        - spring.datasource.password=family
#        - spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  angular:
    container_name: angular-container
    build: 
      context: angular-app
      dockerfile: Dockerfile-angular
    ports:
      - 0:80

Here is my Dockerfile-spring:

FROM openjdk:8
VOLUME /tmp<br> COPY target/family.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]

What should I do to open the MySQL port for my Spring-Boot application?

解决方案是使用网桥驱动程序为Docker创建自己的网络。

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