简体   繁体   English

为什么我无法通过 Docker-compose 将 spring-boot 容器连接到 mysql sql 容器?

[英]Why Cannot i connect spring-boot container to mysql sql container by Docker-compose?

How can i connect mysql container with spring-boot app container.如何将 mysql 容器与 spring-boot 应用程序容器连接。 I try use network then i try docker-compose but it seems my spring-boot app can't connect.我尝试使用网络然后我尝试 docker-compose 但似乎我的 spring-boot 应用程序无法连接。

docker-compose.yml docker-compose.yml

version: '3'
services:
mysql-docker-container:
image: mysql:latest
environment:
  - MYSQL_ROOT_PASSWORD=root
  - MYSQL_DATABASE=my_app
volumes: 
  - /data/mysql
authenticate:
image: authenticate
build: 
  context: ./
  dockerfile: Dockerfile
depends_on:
  - mysql-docker-container
ports: 
  - "8080:8080"
volumes: 
  - /data/authentica

application.properties应用程序属性

#cấu hình Spring Datasource, JPA, App Properties
spring.datasource.url= jdbc:mysql://my-docker-container:3306/my_app?useSSL=false
spring.datasource.username= root
spring.datasource.password= root
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate,dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update
spring.datasource.platform=mysql
spring.datasource.initialization-mode=always
#App 
pin.app.jwtSecret = pinSecretKey
pin.app.jwtExpirationMs = 86400000

This's my result这是我的结果

Then i fix it by a network in compose然后我在 compose 中通过网络修复它

version: '3'
services:
  mysql-docker-container:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=my_app
    volumes: 
      - /data/mysql
    ports: 
      - "3306:3306"
    networks:
      - "my-network"
  authenticate:
    image: authenticate
    build: 
      context: ./
      dockerfile: Dockeerfile
    depends_on:
      - mysql-docker-container
    ports: 
      - "8080:8080"
    volumes: 
      - /data/authentica
    networks: 
      - "my-network"
networks:
  my-network:

It hasn't any different它没有任何不同

Your service name and the connection string is different, you should use the exact service name or container name that is defined in docker-compose.您的服务名称和连接字符串不同,您应该使用 docker-compose 中定义的确切服务名称或容器名称。

Change the connection string to将连接字符串更改为

spring.datasource.url= jdbc:mysql://mysql-docker-container:3306/my_app?useSSL=false

update:更新:

If the connection string does not resolve the issue, then there might be two possible reason如果连接字符串不能解决问题,则可能有两个原因

As in such cases depends_on: will not solve your problem, DB container takes some time to accept the connection.在这种情况下, depends_on:不会解决您的问题,DB 容器需要一些时间来接受连接。 You can do something with wait-for-it or handle retry logic on the application side.您可以使用等待或处理应用程序端的重试逻辑。

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

相关问题 无法在docker-compose中使用mysql容器和flyway连接docker Spring-Boot应用程序 - Cannot connect docker Spring-Boot app with mysql container and flyway in docker-compose 我无法使用 docker-compose 从服务器容器连接 mysql 容器 - I cannot connect the mysql container from server container using the docker-compose 无法使用docker-compose连接到容器 - cannot connect to container with docker-compose 无法在 docker-compose 中将 MySQL 与 Spring 连接 - Cannot connect MySQL with Spring Boot in docker-compose Docker-compose:无法将 api 容器连接到 Minio 容器 - Docker-compose: cannot connect api container to Minio container docker-compose:无法将应用程序容器与 mysql 容器连接 - docker-compose: Can't connect application container with mysql container Java 容器无法使用 docker-compose 连接到 MYSQL 容器 - Java container cant connect to MYSQL container with docker-compose 使用docker-compose时无法访问docker容器内的spring-boot rest-endpoint - Can't access spring-boot rest-endpoint inside docker container when using docker-compose 我可以在 ZB76E98AF9AAA680979BF5A65B2 上使用 docker 组合 mysql + spring 引导容器吗? - Can I use docker compose mysql + spring boot container on kubernetes? 无法将Spring Boot应用程序连接到Docker Mysql容器-未知数据库 - Cannot connect Spring Boot application to Docker Mysql container - unknown database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM