Docker服务内部端口映射到宿主linux机的端口上,从而实现外部可以通过端口直接访问呢docker容器。
1. 启动
docker run --name web -d nginx
[root@docker-node1 ~]# docker run --name web -d nginx
226d326523f64ca128d123cbe8015723951cdc8110ec7528c8fdbc2faf2047db
2. 因为docker 容器默认连接到bridge,所以查看bridge即可获得docker容器网络配置信息
[root@docker-node1 ~]# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "d273e8b45761a8d7c0aeed4e19ee38ce5be77b4422862238e2fb8950f689c7d1",
"Created": "2019-07-05T22:10:32.916590804Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"226d326523f64ca128d123cbe8015723951cdc8110ec7528c8fdbc2faf2047db": {
"Name": "web",
"EndpointID": "43142812df048ca8d8484440f9c4147b4d57f392d38d6cd193adf0f665609314",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"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": {}
}
]
3. 宿主机ping
ping 172.17.0.2
4. 宿主机telnet
[root@docker-node1 ~]# telnet 172.17.0.2 80
Trying 172.17.0.2...
Connected to 172.17.0.2.
Escape character is '^]'.
5. 宿主机curl
(1) 直接访问容器
[root@docker-node1 ~]# curl 172.17.0.2 80
hello
Hello Docker!
curl: (7) Failed to connect to 0.0.0.80: Invalid argument
(2)访问本地localhost
[root@docker-node1 ~]# curl 127.0.0.1
curl: (7) Failed connect to 127.0.0.1:80; Connection refused
无法访问
1.端口映射到宿主机,从而宿主机外部可以直接通过宿主机linux的ip和端口号直接访问docker 容器服务
[root@docker-node1 ~]# docker run --name web -d -p 80:80 nginx
b265e6d2639b281dae742fc32eaeba0fc198160390be72815f2ac70eeea87b05
2.宿主机访问127.0.0.1可以获得nginx web信息
[root@docker-node1 ~]# curl 127.0.0.1
hello
Hello Docker!
3.宿主机外部通过宿主机的ip访问docker服务
http://192.168.205.10:80/
访问本地127.0.0.1:8888,即可访问容器
则访问192.168.205.10:80