我今天刚刚开始探索docker,我一直在尝试开发由MySQL DB支持的Spring Boot Web应用程序。
我提取了一个MySQL容器,并使用以下命令运行它:
docker run -t --name mysql-docker-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=**** -e MYSQL_DATABASE=spring_app_db -e MYSQL_USER=app_user -e MYSQL_PASSWORD=**** -d mysql我的application.properties文件:
spring.datasource.url=jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
spring.datasource.username=app_user
spring.datasource.password=test123
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect这样,当我在IDE中运行我的spring boot时,它就能够连接并在db表上执行CRUD。然而,当我试图将它部署到docker上并链接到MySQL容器时,它抛出了连接被拒绝的错误。我的Dockerfile:
FROM openjdk:11
LABEL maintainer="baljinder@gmail.com"
VOLUME /tmp
EXPOSE 8080
ADD target/demo-0.0.1-SNAPSHOT.jar bootmysql.jar
ENTRYPOINT ["java", "-jar", "bootmysql.jar"]我用来运行引导的docker镜像的命令:
docker run -t --name spring-jpa-container -p 8080:8080 --link mysql-docker-container:mysql spring-jpa-app有没有人能帮个忙。我尝试通过创建docker网络(docker network create -d bridge sql_spring_network)将两者部署在同一个容器网络上,但错误仍然存在。
发布于 2019-12-04 09:55:23
使用使用--link标志的容器的legacy链接时。“链接”容器可以作为运行容器中的主机,并带有它的容器名称。因此,在您的示例中,mysql容器在应用程序容器中可用,主机名为mysql。
因此,您的数据库url应该是:jdbc:mysql://mysql:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10
发布于 2019-12-04 10:08:48
使用:
jdbc:mysql://mysql-docker-container:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10替换:
jdbc:mysql://0.0.0.0:3306/spring_app_db?autoReconnect=true&failOverReadOnly=false&maxReconnects=10https://stackoverflow.com/questions/59164845
复制相似问题