Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the four network modes of Docker

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "What are Docker's four network modes". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "What are Docker's four network modes" can help you solve the problem.

1. install Docker

yum -y install docker-io

Complete indicates installation is complete.

2. Start docker service

service docker start

3. Set docker boot up

chkconfig docker on

4. Basic information View

docker version

docker info

docker images View Mirrors

docker ps View running containers

docker rmi remove mirror

docker save -o imagename:tag > path/name.tar Save image

docker load

< name.tar加载镜像 docker pull拉取镜像 5.如果要卸载的话,命令如下: sudo yum remove docker-ce sudo rm -rf /var/lib/docker 我们在使用docker run创建docker容器时,可以用--net选项指定容器的网络模式,docker有以下4种网络模式: · host模式,使用--net=host指定。 · container模式,使用--net=container:name_or_id指定。 · none模式,使用--net=none指定。 · bridge模式,使用--net=bridge指定,默认设置。 · 还有一种:用户自定义模式 下面分别介绍一下docker的各个网络模式。 1 host模式 格式: docker run -it --name myubuntu --net=host ubuntu /bin/bash 众所周知,docker使用了linux的namespaces技术来进行资源隔离,如pid namespace隔离进程,mount namespace隔离文件系统,network namespace隔离网络等。一个network namespace提供了一份独立的网络环境,包括网卡、路由、iptable规则等都与其他的network namespace隔离。一个docker容器一般会分配一个独立的network namespace。但如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的network namespace,而是和宿主机共用一个network namespace。容器将不会虚拟出自己的网卡,配置自己的ip等,而是使用宿主机的ip和端口。 例如,我们在10.10.101.105/24的机器上用host模式启动一个含有web应用的docker容器,监听tcp80端口。当我们在容器中执行任何类似ifconfig命令查看网络环境时,看到的都是宿主机上的信息。而外界访问容器中的应用,则直接使用10.10.101.105:80即可,不用任何nat转换,就如直接跑在宿主机中一样。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的。 2 container模式 格式: docker run -it --name myubuntu --net=container:name_or_id ubuntu /bin/bash 在理解了host模式后,这个模式也就好理解了。这个模式指定新创建的容器和已经存在的一个容器共享一个network namespace,而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的ip,而是和一个指定的容器共享ip、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。两个容器的进程可以通过lo网卡设备通信。 3 none模式 格式: docker run -it --name myubuntu --net=none ubuntu /bin/bash 这个模式和前两个不同。在这种模式下,docker容器拥有自己的network namespace,但是,并不为docker容器进行任何网络配置。也就是说,这个docker容器没有网卡、ip、路由等信息。需要我们自己为docker容器添加网卡、配置ip等。 4 bridge模式 bridge模式是docker默认的网络设置,此模式会为每一个容器分配network namespace、设置ip等,并将一个主机上的docker容器连接到一个虚拟网桥上。下面着重介绍一下此模式。 4.1 bridge模式的拓扑 当docker server启动时,会在主机上创建一个名为docker0的虚拟网桥,此主机上启动的docker容器会连接到这个虚拟网桥上。虚拟网桥的工作方式和物理交换机类似,这样主机上的所有容器就通过交换机连在了一个二层网络中。接下来就要为容器分配ip了,docker会从rfc1918所定义的私有ip网段中,选择一个和宿主机不同的ip地址和子网分配给docker0,连接到docker0的容器就从这个子网中选择一个未占用的ip使用。如一般docker会使用172.17.0.0/16这个网段,并将172.17.42.1/16分配给docker0网桥(在主机上使用ifconfig命令是可以看到docker0的,可以认为它是网桥的管理接口,在宿主机上作为一块虚拟网卡使用)。单机环境下的网络拓扑如下,主机地址为10.10.101.105/24。 docker完成以上网络配置的过程大致是这样的: 1. 在主机上创建一对虚拟网卡veth pair设备。veth设备总是成对出现的,它们组成了一个数据的通道,数据从一个设备进入,就会从另一个设备出来。因此,veth设备常用来连接两个网络设备。 2. docker将veth pair设备的一端放在新创建的容器中,并命名为eth0。另一端放在主机中,以veth65f9这样类似的名字命名,并将这个网络设备加入到docker0网桥中,可以通过brctl show命令查看。

3. Assign an ip from the docker0 subnet to the container and set the ip address of docker0 as the default gateway for the container.

After describing the network topology, let's look at how containers communicate in bridge mode.

4.2 Container communication in bridge mode

In bridge mode, containers connected to the same bridge can communicate with each other (or they can be prevented from communicating for security reasons by setting--icc=false in the docker_opts variable, so that only--link can make two containers communicate).

Containers can also communicate with the outside world. If we look at the iptable rule on the host, we can see this one

-a postrouting -s 172.17.0.0/16 ! -o docker0 -j masquerade

This rule will translate packets with source address 172.17.0.0/16 (i.e. packets originating from docker containers), and not originating from docker0, to the address of the host NIC. This may not be easy to understand, but give an example. Suppose the host has a NIC eth0, IP address 10.10.101.105/24, gateway 10.10.101.254. Ping Baidu (180.76.3.151) from a container with ip 172.17.0.1/16 on the host. IP packets are first sent from the container to its default gateway docker0, and when the packets arrive at docker0, they also arrive at the host. It then queries the host's routing table and finds that packets should be sent from eth0 on the host to gateway 10.10.105.254/24 on the host. The packet is then forwarded to eth0 and sent out from eth0 (ip_forward forwarding for the host should already be turned on). At this point, the iptable rule above will work, snat the packet, changing the source address to the address of eth0. Thus, to the outside world, this package is sent from 10.10.101.105, and the docker container is invisible to the outside world.

So, how does an outside machine access docker container services? We first create a container containing a web application, mapping port 80 of the container to port 80 of the host with the following command:

docker run -d --name web -p 80:80 fmzhen/simpleweb

Then look at the changes to the iptable rule and find that there is an additional rule:

-a docker ! -i docker0 -p tcp -m tcp --dport 80 -j dnat --to-destination 172.17.0.5:80

This rule is to perform dnat conversion on tcp traffic received by host eth0 with destination port 80, and send the traffic to 172.17.0.5: 80, which is the docker container we created above. Therefore, outsiders only need to visit 10.10.101.105: 80 to access the services in the container.

In addition, we can customize the ip address, dns, etc. used by docker, and even use our own bridges, but the way it works is the same.

User-defined mode

Users can customize the network via docker network drives or other network drives. You can connect many containers to the same network, and once connected to a custom network, containers can communicate with each other through each other's ip addresses and hostnames.

If a container is connected to a user-defined network, the container's/etc/hosts file adds the ip addresses of all other containers in the same network.

Because the container may change the/etc/hosts file at any time, programs in the container may read incomplete or even empty/etc/hosts files. Usually re-reading can solve this problem.

About "Docker four network modes are what" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report