繁体   English   中英

如何从本地主机Windows(192.168)连接docker子网(172.18)中的mysql?

[英]How to connect mysql in docker subnet(172.18) from my local host Windows(192.168)?

我的主机IP是192.168.8.100

这是我在Windows10中的ipconfig

Ethernet adapter Ethernet:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . : DHCP HOST

Ethernet adapter vEthernet (Default Switch):

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : fe80::cd90:1c37:f269:c1b5%10
IPv4 Address. . . . . . . . . . . : 172.21.93.241
Subnet Mask . . . . . . . . . . . : 255.255.255.240
Default Gateway . . . . . . . . . :

Ethernet adapter vEthernet (DockerNAT):

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : fe80::a80e:8b65:b853:7976%13
IPv4 Address. . . . . . . . . . . : 10.0.75.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :

Ethernet adapter VirtualBox Host-Only Network:

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : fe80::dcad:f104:d3ff:60f%7
IPv4 Address. . . . . . . . . . . : 192.168.56.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :

Ethernet adapter Npcap Loopback Adapter:

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : fe80::f85f:91c6:fcb6:c971%21
Autoconfiguration IPv4 Address. . : 169.254.201.113
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :

Wireless LAN adapter Local Area Connection* 1:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 2:

Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

Connection-specific DNS Suffix  . :
Link-local IPv6 Address . . . . . : fe80::e58a:9017:15ae:2a26%17
IPv4 Address. . . . . . . . . . . : 192.168.8.100
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.8.1

我从docker-compose.ymldocker-compose.yml创建一个新子网

  db:
    image: mysql:8.0
    container_name: onlinecodedb
    volumes:
    - onlinecode-database:/var/lib/mysql
    environment:
    MYSQL_ROOT_PASSWORD: mysqlrootpassword
    MYSQL_PASSWORD: mysqlpassword
    MYSQL_USER: mysql
    MYSQL_DATABASE: onlinecode
    ports:
    - "3300:3306"
    networks:
    - onlinecode-net

networks:
onlinecode-net:
    driver: bridge

docker network inspect onlinecode_onlinecode-net

[
    {
        "Name": "onlinecode_onlinecode-net",
        "Id": "b915ecd03a9acdb0d28b8b355dca0a479a186c2b5e6fbb35efb81de1684aa63d",
        "Created": "2018-10-02T02:17:36.4110372Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.18.0.0/16",
                    "Gateway": "172.18.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": true,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "0730d1115929f5b476116fd6147782bf15d7f469fb7203779d2d8f5953bdea49": {
                "Name": "onlinecodeapp",
                "EndpointID": "aa0c0bf03740e500821b1f00d0da2f09d3642723035e0b2e384ac18746bf182a",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            },
            "c3abc13980d415dd0a6494e3e5113847448e004f3b720f1603c826ebbaa2b9db": {
                "Name": "onlinecodedb",
                "EndpointID": "7e5261e88fcded21e1864906de976ed1c2d5b30d4c94ae65375bf21e0035706c",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "onlinecode-net",
            "com.docker.compose.project": "onlinecode",
            "com.docker.compose.version": "1.22.0"
        }
    }
]

ip B class range onlinecodedb ,但C range localhost

那么,我如何从192.168到172.18连接到mysql? 我从stackoverflow中找不到相同的问题

我用Google搜索了两天,有人说:嘿,您应该使用网关和路由器。

但是我如何将172映射到192? 它们都是私有IP,而不是公共IP。

我知道这个问题很愚蠢,这是计算机网络问题,但是请帮助我解决这个问题。

您应该尝试使用localhost:3300连接到mysql容器。

首先,当您的docker引擎安装在本地计算机中时,默认情况下docker在本地主机(即127.0.0.1 ipaddress)中运行。

根据您的docker-compose文件

ports:
- "3300:3306"

3306端口暴露在容器内部,并与主机的3300端口连接。

接下来,您提到了容器的Ipaddress

        "Config": [
            {
                "Subnet": "172.18.0.0/16",
                "Gateway": "172.18.0.1"
            }
        ]

这是您使用“ onlinecode-net ”名称创建的网络 。该网络由docker-engine创建,在内部用于在主机之间未公开的容器之间进行连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM