Spring Boot + docker-compose + MySQL:连接被拒绝 [英] Spring Boot + docker-compose + MySQL: Connection refused

查看:219
本文介绍了Spring Boot + docker-compose + MySQL:连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个依赖于docker-compose中名为 teste 的MySQL数据库的Spring Boot应用程序.发布 docker-compose up 之后,我得到:

I'm trying to set up a spring boot application that depends on a MySQL database called teste in docker-compose. After issuing docker-compose up I'm getting:

Caused by: java.net.ConnectException: Connection refused (Connection refused)

我正在Linux Mint上运行,我的 docker-compose版本是1.23.2 ,我的 docker版本是18.09.0 .

I'm running on Linux Mint, my docker-compose version is 1.23.2, my docker version is 18.09.0.

application.properties

application.properties

# JPA PROPS
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

spring.datasource.url=jdbc:mysql://db:3306/teste?useSSL=false&serverTimezone=UTC
spring.datasource.username=rafael
spring.datasource.password=password

spring.database.driverClassName =com.mysql.cj.jdbc.Driver

docker-compose.yml

docker-compose.yml

version: '3.5'
services:
  db:
    image: mysql:latest
    environment:
      - MYSQL_ROOT_PASSWORD=rootpass
      - MYSQL_DATABASE=teste      
      - MYSQL_USER=rafael
      - MYSQL_PASSWORD=password
    ports:
      - 3306:3306
  web:
    image: spring-mysql
    depends_on:
      - db
    links:
      - db
    ports:
      - 8080:8080
    environment:
      - DATABASE_HOST=db
      - DATABASE_USER=rafael
      - DATABASE_NAME=teste
      - DATABASE_PORT=3306

和Dockerfile

and the Dockerfile

FROM openjdk:8
ADD target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]

先谢谢!

推荐答案

Docker compose始终以依赖关系顺序或文件中的顺序顺序(如果未给出)启动和停止容器.但是docker-compose不能保证它会一直等到依赖容器运行之后.您可以在此处参考以获得更多详细信息.因此,这里的问题是,当您的spring-mysql容器尝试访问数据库时,数据库尚未准备就绪.因此,推荐的解决方案是您可以使用 wait- for-it.sh 或类似的脚本,用于从ENTRYPOINT开始包装spring-mysql应用.

Docker compose always starts and stops containers in dependency order, or sequential order in the file if not given. But docker-compose do not guarantee that it will wait till the dependency container is running. You can refer here for further details. So the Problem here is that your database is not ready when your spring-mysql container try to access database. So the recommended solution is you could use wait-for-it.sh or similar script to wrap your spring-mysql app starting ENTRYPOINT.

例如,如果您使用wait-for-it.sh,则在将上述脚本复制到项目根目录后,Dockerfile中的ENTRYPOINT应该更改为以下内容:

As example if you use wait-for-it.sh your ENTRYPOINT in your Dockerfile should change to following after copying above script to your project root:

ENTRYPOINT ["./wait-for-it.sh", "db:3306", "--", "java", "-jar", "app.jar"]

这里要考虑的另外两个重要事项是:

And two other important thing to consider here is:

  • 请勿使用已弃用的链接,而应使用用户定义的网络.如果您未明确定义任何网络,则docker-compose文件中的所有服务都将位于单个用户定义的网络中.因此,您只需要从撰写文件中删除链接即可.
  • 如果仅在用户定义的网络内使用Docker容器,则无需发布Docker容器的端口.
  • Do not use links they are deprecated you should use user-defined network instead. All services in docker-compose file will be in single user-defined network if you don't explicitly define any network. So you just have to remove the links from compose file.
  • You don't need to publish the port for docker container if you only use it inside the user-defined network.

这篇关于Spring Boot + docker-compose + MySQL:连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆