gpt4 book ai didi

mysql - Docker with springboot 和 mysql 的链接和运行问题

转载 作者:行者123 更新时间:2023-11-29 16:59:04 25 4
gpt4 key购买 nike

我正在尝试将 spring-boot 应用程序(连接到 mySQL 数据库)作为 docker 容器运行(因此基本上是两个链接在一起的容器)。

但是由于 mySQL docker 容器不同,我很难链接这两个容器。我尝试外部化 application.properties 但随后我无法通过将参数传递给外部应用程序属性来运行它。

有没有办法有效连接这两个容器?<​​/p>

这是我的应用程序属性和 dockerfile

应用程序属性:

spring.application.name=customer-service
server.port:8203

#spring.datasource.url=jdbc:mysql://demo-mysql:3306/bankbotdb?autoReconnect=true&useSSL=false
spring.datasource.url=jdbc:mysql://localhost:3306/bankbotdb?autoReconnect=true&useSSL=false
spring.datasource.username=***
spring.datasource.password =*******
...

Dockerfile:

FROM openjdk:8
ADD target/nlp-bot-customer-service-0.0.1-SNAPSHOT.jar /opt/micro-services/
ADD config/application.properties /opt/micro-services/config/
WORKDIR /opt/micro-services/
EXPOSE 8203
ENTRYPOINT [“java”, “-jar”,”nlp-bot-customer-service-0.0.1-SNAPSHOT.jar — spring.config.name=application — spring.config.location=classpath:/op/micro-services/config”]

最佳答案

您的问题出在 application.properties 中: spring.datasource.url=jdbc:mysql://localhost:3306/bankbotdb?autoReconnect=true&useSSL=false

在 Docker 环境中,当您使用“localhost”时,将引用容器本身(在本例中为“bot-customer-service”容器),除非您使用不推荐的主机模式。

要解决此问题,您应该将该行更改为 spring.datasource.url=jdbc:mysql://demo-mysql:3306/bankbotdb?autoReconnect=true&useSSL=false (根据您的评论) 。将 demo-mysql 更改为您在启动 mysql 容器时所称的名称。

我建议使用docker-compose在编排服务时帮助提供概览和可重复性。

关于mysql - Docker with springboot 和 mysql 的链接和运行问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52399681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com