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

标签 mysql docker networking

我的主机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 创建了一个新子网 来自docker-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网络检查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"
        }
    }
]

onlinecodedb 在 ip B 类范围 但我的本地主机在 C 范围

那么我如何从192.168连接到172.18呢? 我在 stackoverflow 上找不到同样的问题

我google了两天,有人说:嘿,你应该使用网关和路由器。

但是我如何将 172 映射到 192 呢?所有这些都是私有(private)IP,而不是公共(public)IP。

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

最佳答案

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

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

根据您的 docker-compose 文件

ports:
- "3300:3306"

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

接下来你提到了容器的IP地址

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

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

关于mysql - 如何从本地主机Windows(192.168)连接docker子网(172.18)中的mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52622267/

相关文章:

java - 创建一个新的wireshark文件并删除某些数据包

mysql - 从该查询中的另一个表中获取值

mysql - 为什么此搜索查询不返回任何内容?

php - Doctrine 2 中的行数

docker - 当我们显示Docker机器时,DOCKER字段是什么?

amazon-web-services - 亚马逊AWS的 "network performance"是什么意思?

PHP 检查 IPAddress 是否是本地的

java - where查询区分大小写

node.js - tar EPERM : operation not permitted, future

Dockerized IBM DB2 : No start database manager command was issued. SQLSTATE=57019