docker访问宿主机mysql_docker容器链接宿主机mysql

查看docker容器虚拟ip

$ docker inspect --format '{{ .NetworkSettings.IPAddress }}' [容器ID]

可以查看容器的具体IP地址,如果输出是空的说明没有配置IP地址

宿主机IP

与容器同网段,而且是XXX.XXX.XXX.1

比如容器查出来ip是192.169.0.2,那么宿主机ip就是192.168.0.1

在容器内,可以通过192.168.0.1访问宿主机

容器内连接宿主机上的mysql服务器

也很简单,把mysql服务器地址改成192.168.0.1即可。已经实践成功。

不确定因素

我的服务器上查出来是192.168.0.1,至于这个是否是固定的,我不太确定

限定因素

感谢 @zkoma 的批评。之前写的《不确定因素》对群众不太负责。

因为已经简单的实践成功并且已在运行,所以也没有详细探究下去,自己做个笔录而已。

为此,阅读了下docker文档

https://docs.docker.com/engin...

并且在此记录下我的理解,如果错误,请各位路过大神更正

docker容器默认联网方式

先摘一段文档

The bridge network represents the docker0 network present in all Docker installations. Unless you specify otherwise with the docker run --network= option, the Docker daemon connects containers to this network by default. You can see this bridge as part of a host’s network stack by using the ifconfig command on the host.

可见bridge是docker容器的默认联网方式,而默认的bridge是通过宿主机上的docker0来实现。

$ ifconfig

通过以上命令可以查看docker0的详情,我的几台服务器上docker0的inet都是192.168.0.1

docker默认安装的docker0的作用

再摘一段文档

The default docker0 bridge network supports the use of port mapping and docker run --link to allow communications between containers in the docker0 network. These techniques are cumbersome to set up and prone to error. While they are still available to you as techniques, it is better to avoid them and define your own bridge networks instead.

由此可见,通过默认安装的docker0,提供了以下功能

1、映射容器端口到宿主机端口

2、可以通络link的方式,建立容器之间的通讯

不过官方还是推荐自己再重新一定一个bridge

查看当前docker的网络连接方式

$ docker network ls

NETWORK ID NAME DRIVER

7fca4eb8c647 bridge bridge

9f904ee27bf5 none null

cf03ee007fb4 host host

查看某个联网方式的详情

$ docker network inspect bridge

docker network inspect bridge

[

{

"Name": "bridge",

"Id": "229fce428f978b559be16c9877ac53f2d5fe1caefbb8c9ce107a055623fd3050",

"Created": "2017-03-15T10:25:48.873918192+08:00",

"Scope": "local",

"Driver": "bridge",

"EnableIPv6": false,

"IPAM": {

"Driver": "default",

"Options": null,

"Config": [

{

"Subnet": "192.168.0.0/20"

}

]

},

"Internal": false,

"Attachable": false,

"Containers": {

"3d07b11a2a7ba5550a5df4086b684a6304f36d38f6f5cc49615b80d1684c6a05": {

"Name": "atfirst_redis_1",

"EndpointID": "8302df17b41be981df1f695daf95709e1e4b03dfec03fc5f872808e1e9b9d1f5",

"MacAddress": "02:42:c0:a8:00:03",

"IPv4Address": "192.168.0.3/20",

"IPv6Address": ""

},

"50e29986918d5959baa88a0a0a1a25650b6c859d169a247dd5325fdbe8a7463e": {

"Name": "atfirst_tomcat_1",

"EndpointID": "2f1b9d7c15f1e8a3322c7d990a283b2bf8071fbf29a90789270302de90ed00a7",

"MacAddress": "02:42:c0:a8:00:02",

"IPv4Address": "192.168.0.2/20",

"IPv6Address": ""

}

},

"Options": {

"com.docker.network.bridge.default_bridge": "true",

"com.docker.network.bridge.enable_icc": "true",

"com.docker.network.bridge.enable_ip_masquerade": "true",

"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",

"com.docker.network.bridge.name": "docker0",

"com.docker.network.driver.mtu": "1500"

},

"Labels": {}

}

]

至此,我想应该差不多已经能看明白,容器和宿主机之间的ip关系了吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值