如何从本地主机Windows(192.168)连接docker子网(172.18)中的mysql?
我的主机ip是如何从本地主机Windows(192.168)连接docker子网(172.18)中的mysql?,mysql,docker,networking,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 Suffi
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\u 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
在ipB类范围内
但我的本地主机在C范围内
那么我如何从192.168连接到mysql到172.18呢?
我在stackoverflow中找不到相同的问题
我在谷歌上搜索了两天,有人说:嘿,你们应该使用网关和路由器
但我如何将172映射到192?它们都是私有IP,而不是公共IP
我知道这个问题很愚蠢,这是一个计算机网络问题,但请帮我解决这个问题。您应该尝试使用localhost:3300连接到mysql容器 首先,在本地计算机上安装docker引擎时,默认情况下docker在localhost(ie)127.0.0.1 ipaddress中运行 根据您的docker编写文件
ports:
- "3300:3306"
3306端口暴露在容器内,与主机3300端口相连
接下来,您提到了容器的IP地址
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
这是您创建的网络,名称为onlinecode net“。此网络由docker引擎创建,该引擎在内部用于连接主机中未公开的容器。docker for Windows中的网络功能:。建议的方法是
将
端口发布到主机。您的docker引擎位于windows 10主机或virtualbox中。@Prem它是windows内核上的docker版本:4.9.93-linuxkit-aufs操作系统:docker for windows OSType:linux体系结构:x86_64 CPU:2总内存:1。934GiB@PremWindows 10 pro虚拟机监控程序。。不是VBox那么您应该尝试使用localhost:3300,这可能会起作用,因为据我所知,您正在尝试从主机中运行的应用程序连接到docker容器(mysql容器)。因为这是一个常见的混淆点:docker run-p
端口只能通过127.0.0.1或localhost
从浏览器或直接在主机系统上运行的其他工具访问。其他容器认为“localhost”意味着“我的私有容器名称空间”,这样就无法访问其他容器。