Dockerized Mac / Java应用程序无法与本地主机通信

MacOS Docker(版本17.12.0-ce-mac49(21995))在此处.我正在尝试对现有的Spring Boot应用程序进行Docker化.这是我的Dockerfile:

FROM openjdk:8

RUN mkdir /opt/myapp

ADD build/libs/myapp.jar /opt/myapp
ADD application.yml /opt/myapp
ADD logback.groovy /opt/myapp
WORKDIR /opt/myapp
EXPOSE 9200
ENTRYPOINT ["java", "-Dspring.config=.", "-jar", "myapp.jar"]

这是我的Spring Boot application.yml配置文件.如您所见,它期望Docker从环境文件中注入环境变量:

logging:
  config: 'logback.groovy'
server:
  port: 9200
  error:
    whitelabel:
      enabled: true
spring:
  cache:
    type: none
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://${DB_HOST}:3306/myapp_db?useSSL=false&nullNamePatternMatchesAll=true
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
    testWhileIdle: true
    validationQuery: SELECT 1
  jpa:
    show-sql: false
    hibernate:
      ddl-auto: none
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
        implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
    properties:
      hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
      hibernate.cache.use_second_level_cache: false
      hibernate.cache.use_query_cache: false
      hibernate.generate_statistics: false
      hibernate.hbm2ddl.auto: validate
myapp:
  detailsMode: ${DETAILS_MODE}
  tokenExpiryDays:
    alert: 5
  jwtInfo:
    secret: ${JWT_SECRET}
    expiry: ${JWT_EXPIRY}
  topics:
    adminAlerts: admin-alerts

这是我的myapp-local.env文件:

DB_HOST=localhost
DB_USERNAME=root
DB_PASSWORD=
DETAILS_MODE=Terse
JWT_SECRET=12345==
JWT_EXPIRY=86400000

值得注意的是,在上面的env文件中,我尝试了localhost,127.0.0.1和172.17.0.1,它们在下面都产生相同的错误.

然后我构建容器:

docker build -t myapp .

成功!然后我运行容器:

docker run -it -p 9200:9200 --net="host" --env-file myapp-local.env --name myapp myapp

…并且我看到容器迅速死于与MySQL连接相关的异常(无法连接到本地运行的MySQL机器).我可以确认Spring Boot应用程序在Docker之外以可执行(fat)jar的形式运行时,可以正常连接到MySQL,并且可以确认本地MySQL实例已启动并且正在运行,并且运行状况良好.

Unable to connect to database. }com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:590)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:57)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1606)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:633)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:347)

当我打开TRACE级日志记录时,我看到它正在尝试连接到:

url=jdbc:mysql://localhost:3306/myapp?useSSL=false&nullNamePatternMatchesAll=true

因此,看起来Docker确实已将env文件的var正确注入基于Spring YAML的配置中.因此,这不像是配置问题,而且这是一个与容器在Docker主机上运行的MySQL端口通讯的问题.

有人可以看到我要去哪里吗?

最佳答案
不建议从容器中访问主机.通常,可以通过将所需的服务包装到容器中并通过容器名称访问它来解决.

没有解决方案,只有解决方法,您可以使用其中一种:

在Mac上,您可以使用docker.for.mac.host.internal DNS名称访问主机服务.

您需要像这样设置环境变量:

DB_HOST=docker.for.mac.host.internal

并从您的连接字符串中引用DB_HOST.

有关更多详细信息see the documentation

From 17.12 onwards our recommendation is to connect to the special
Mac-only DNS name docker.for.mac.host.internal, which resolves to the
internal IP address used by the host.

注意:拥有–net =“ host”不能让您通过localhost访问主机.本地主机始终指向本地计算机,但是如果从容器内部调用它,则它指向容器本身.

转载注明原文:Dockerized Mac / Java应用程序无法与本地主机通信 - 代码日志