查看ip
ip addr
lo是内网地址本机回环地址,eth0是外网地址,docker0是docker地址
docker run -d -P --name tomcat01 tomcat
docker exec -it tomcat01 ip addr //查看容器的ip地址
注意:如果出现docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "ip": executable file not found in $PATH: unknown.
说明容器内部没有安装包,此时需要手动安装
安装方法
apt-get update //更新apt
apt-get install net-tools
apt-get install iputils-ping //ifconfig
apt-get install iproute2 // ping
yum -y install initscripts // centos install ping commands
将自己添加后的容器提交到本地保存为镜像
docker commit -a="作者" -m="描述" 所提交的镜像id 名称:版本号
是可以ping通的。
容器网卡都是成对出现,就是用evth-pair对虚拟设备连接。容器之间也是可以ping通,所有的容器不指定网络的情况下,都是docker0路由的,docker会给容器分配一个默认的可用ip
docker网络使用的是LINUX桥接,诉诸集中是一个Docker容器的网桥Docker0。而且Docker中的所有网络都是虚拟的,虚拟的转发效率较高。
docker network ls
bridge:桥接docker
none: 不配置网络
host:和宿主机共享网络
container:容器网络连通,较少使用,因为局限很大
之前的容器的网络接口全无
docker rm -f $(docker ps -aq)
docker的特点就是默认,域名不能访问,但是–link可以连通
#直接启动命令 --net bridge, 这个就是docker0
docker run -d -P --name tomcat01 --net bridge tomcat // 如果没有--net则默认是bridge方式
--driver bridge
--subnet 192.168.0.0/16
--getway 192.168.0.0
docker network create --driver bridge --subnet 192.168.0.0/16 --getway 192.168.0.0 mynet
# 查看docker0网络
dcoker network ls
docker network inspect mynet
创建三个容器
dcoker run -d -P --name tomcat-net-0001 --net mynet lieontomcat:1.0
dcoker run -d -P --name tomcat-net-0002 --net mynet lieontomcat:1.0
dcoker run -d -P --name tomcat-net-0003 --net mynet lieontomcat:1.0
docker ps //查看运行中的容器
da0d5adf6137 lieontomcat:1.0 "catalina.sh run" 4 seconds ago Up 2 seconds 8080/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp tomcat-net-0003
daf14da9e1b1 lieontomcat:1.0 "catalina.sh run" 28 seconds ago Up 27 seconds 0.0.0.0:3355->3355/tcp, :::3355->3355/tcp, 8080/tcp tomcat-net-0002
1cb7339cc6c7 lieontomcat:1.0 "catalina.sh run" 51 seconds ago Up 50 seconds 0.0.0.0:3344->3344/tcp, :::3344->3344/tcp, 8080/tcp tomcat-net-0001
可以查看到三个容器的ip地址
docker network inspect mynet
自定义的网络docker都已经维护好了对应的关系。
dcoker exeec -it tomcat-net-0001 ping 192.168.0.3 #是可以ping通的
dcoker exeec -it tomcat-net-0001 ping tomcat-net-0002 #也是可以ping通的
从docker0网络的容器能否ping通自定义网络中的容器呢?
docker run -d -p 8080:8080 --name tomcat01 tomcat // 创建新的容器
docker run -d -p 9999:9999 --name tomcat02 tomcat // 创建新的容器
发现无法连接ping: tomcat-net-0001: Name or service not known
docker exec -it tomcat01 ping tomcat-net-0001
将tomcat01与mynet连通
docker network --help //查看帮助
其中的connect就是打通方式docker network connect [OPTIONS] NETWORK CONTAINER
#其中的connect就是打通方式
Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks
Run 'docker network COMMAND --help' for more information on a command.
打通
docker network connect mynet tomcat01
打通完毕之后再次查看mynet的信息就会发现,直接将tomcat01的复制进mynet网络下,也就是,一个容器有两个ip地址。
"Containers": {
"1cb7339cc6c7ad96126b4e9b55f26fa219e04407f4ab9262b513bb7fa83f7cae": {
"Name": "tomcat-net-0001",
"EndpointID": "58c2918ab335d62d64463ce0b85d6abedbe23ce134f66360c8de4154f33ce231",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"7bae3f9f86411dff1e26a90f93cfdc89c228b0f6c78e45ae0fcf5860ea7f9311": {
"Name": "tomcat01",
"EndpointID": "53347194ee5202b51e10deeda421ead733403f12003f0a979b48571cb952d267",
"MacAddress": "02:42:c0:a8:00:05",
"IPv4Address": "192.168.0.5/16",
"IPv6Address": ""
},
"da0d5adf6137c72da34d18785fb8bd6b3a89f490e1dae6a0675193ee999c8729": {
"Name": "tomcat-net-0003",
"EndpointID": "1c8defd3712d2a47cda075cdd5055db36bc0ef78d92c7db050ceb81b0d2f30ef",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16",
"IPv6Address": ""
},
"daf14da9e1b113a1d25f088a2bf26426081c7c63958b15d324d61cf02955fbfd": {
"Name": "tomcat-net-0002",
"EndpointID": "3e01494e79b02c00ac96ffee40d30c381a9500a70c6e1ca25e895f35dc7a195e",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
再次连通
docker exec -it tomcat01 ping tomcat-net-0001
可以连通
PING tomcat-net-0001 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net-0001.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.177 ms
64 bytes from tomcat-net-0001.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.138 ms
64 bytes from tomcat-net-0001.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.106 ms
tomcat02连通mynet
docker exec -it tomcat02 ping tomcat-net-0001
连通失败
ping: tomcat-net-0001: Name or service not known