docker容器中的java服务未连接到主机mysql

0mkxixxg  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(448)

我在docker容器中运行java微服务,它无法连接到本地托管的mysql。
docker正在ip地址为172.0的网络中运行。。。如果我直接以java-jar的形式执行java服务,它就可以连接到运行在10.0中的mysql。。
docker编写文件

version: '2.0'
services:
  config-server:
        image: test/config-server
        container_name: config-server
        environment:
          - GIT_USERNAME=${GIT_USERNAME}
          - GIT_PASSWORD=${GIT_PASSWORD}
        ports:
          - 8889:8889
        entrypoint: ["java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Drun.arguments=GIT_USERNAME=${GIT_USERNAME}, GIT_PASSWORD=${GIT_PASSWORD} -Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  discovery-server:
      image: test/discovery-server
      container_name: discovery-server
      links:
        - config-server
      depends_on:
        - config-server
      entrypoint: ["./wait-for-it.sh","config-server:8889","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
      ports:
        - 8761:8761
  web-authentication:
      image: test/web-authentication
      container_name: web-authentication
      links:
        - config-server
        - discovery-server
      depends_on:
        - discovery-server
      entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
      ports:
        - 8444:8444
  gateway:
      image: test/gateway
      container_name: gateway
      links:
        - config-server
        - discovery-server
        - web-authentication
      depends_on:
        - discovery-server
      entrypoint: ["./wait-for-it.sh","discovery-server:8761","--timeout=60","--","java", "-XX:+UnlockExperimentalVMOptions", "-XX:+UseCGroupMemoryLimitForHeap", "-Dspring.profiles.active=docker", "-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
      ports:
        - 81:8765
fhity93d

fhity93d1#

在docker-compose.yml中配置网络配置后问题得到解决,问题是mysql,docker容器运行在不同的子网中。

相关问题