简体   繁体   English

无法从springboot docker容器连接到mysql docker容器

[英]Can't connect to mysql docker container from springboot docker container

I want to connect from the sprinboot container to the mysql container but i get this error: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure我想从 sprinboot 容器连接到 mysql 容器,但出现此错误:com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败

I am using docker-compose.我正在使用 docker-compose。

This is my docker-compose file:这是我的 docker-compose 文件:

version: '3'
services:

  trips-db:
    image: "mysql"
    container_name: "trips-db"
    volumes:
        - newDatabase:/home/sharedData
    ports:
        - 49152:3306
    environment:
      - MYSQL_USER=adham
      - MYSQL_PASSWORD=123 
      - MYSQL_ROOT_PASSWORD=admin

  trips-app:
    build: ./Wasalny_BE
    container_name: trips-app
    ports:
        - 8080:8080
    links:
        - trips-db

volumes: 
  newDatabase:

This is my spring boot docker file:这是我的 spring 引导 docker 文件:

FROM openjdk:8-jdk-alpine
COPY target/wasalny-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

This is my application.properties file:这是我的 application.properties 文件:

spring.datasource.url= jdbc:mysql://trips-db:49152/wslny?allowPublicKeyRetrieval=true&useSSL=false&createDatabaseIfNotExist=true
spring.datasource.username= root
spring.datasource.password= admin
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update

I run using the following command form the terminal:我从终端使用以下命令运行:

docker-compose up -d 
--force-recreate

Please create a docker.network and attach it into the compose file.请创建一个 docker.network 并将其附加到撰写文件中。

Create a docker Network first like below.首先创建一个 docker 网络,如下所示。

docker.network create ext.network docker.network 创建 ext.network

Add.network settings into your yaml file as below and give a try如下所示将网络设置添加到您的 yaml 文件中并尝试一下

version: '3'
services:

  trips-db:
    image: "mysql"
    container_name: "trips-db"
    volumes:
        - newDatabase:/home/sharedData
    ports:
        - 49152:3306
    networks:
      - ext-network
    environment:
      - MYSQL_USER=adham
      - MYSQL_PASSWORD=123 
      - MYSQL_ROOT_PASSWORD=admin

  trips-app:
    build: ./Wasalny_BE
    container_name: trips-app
    ports:
        - 8080:8080
    networks:
      - ext-network
    links:
        - trips-db
        
networks:
  ext-network:
    external: true

volumes: 
  newDatabase:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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