如何从 Docker 容器内部连接到主机操作系统?

问题描述 投票:0回答:1

我的网络应用程序有一个如下所示的连接字符串

jdbc:mysql://127.0.0.1:3306/my-template?useSSL=false

在 Docker 外部运行时,一切都按预期工作。

当我从 Docker 运行/启动应用程序时,它不会连接到 mySQL。

我知道 Docker 是一个封闭的容器虚拟机,但我如何让它找到我的 mySQL 实例?

我尝试了一些我读到的东西,例如添加 extra_hosts 条目,但这似乎也不起作用。 jdbc:mysql://fc.database/my-template?useSSL=false

人们必须一直这样做,对吧?

version: '3'

services:

  tomcat:
    image: 'local/myapp:r1'
    build:
      context: '.'
    
    volumes:  
      - './logs:/usr/local/tomcat/logs'
    
    ports:
      - '8080:8080'
    network_mode: "host"
    extra_hosts:
      - "fc.database:127.0.0.1:3306"
volumes: {}

java mysql spring networking docker-compose
1个回答
1
投票

将 127.0.0.1 替换为

host.docker.internal
,然后重试。 实际上,127.0.0.1指向我自己,这样docker容器就会指向docker容器,而不是主机操作系统。但 AFAIK
host.docker.internal
将指向主机操作系统。 另外,请注意主机防火墙。因此,请检查您的操作系统防火墙以允许所需端口 (3306) 上的传入流量。

© www.soinside.com 2019 - 2024. All rights reserved.