[英]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
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.