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

Example Analysis of Docker installation method and four Network modes of Docker

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces the installation method of Docker and the example analysis of four network modes of Docker. The article is very detailed and has a certain reference value. Interested friends must read it!

1. Install docker

Yum-y install docker-io

The complete appears indicating that the installation is complete

two。 Start the docker service

Service docker start

3. Set docker to boot

Chkconfig docker on

4. Basic information view

Docker version

Docker info

Docker images View Mirror

Docker ps view the container that is running

Docker rmi Delete Mirror

Docker save-o imageName:tag > path/name.tar save the 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。另一端放在主机中,以veth75f9这样类似的名字命名,并将这个网络设备加入到docker0网桥中,可以通过brctl show命令查看。

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

After the introduction of the network topology, let's take a 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 (for security reasons, you can also disable communication between them by setting-- icc=false in the DOCKER_OPTS variable so that only-- link can enable the two containers to communicate).

The container can also communicate with the outside. If we take a look at the Iptable rules on the host, we can see this.

-A POSTROUTING-s 172.17.0 MASQUERADE 16!-o docker0-j MASQUERADE

This rule translates the source address to the address of the host network card from a packet with a source address of 172.17.0 Docker 16 (that is, a packet generated from the docker0 container) that is not issued from the host network card. This may not be easy to understand. Let me give you an example. Suppose the host has a network card with the eth0,IP address 10.10.101.105Universe 24 and the gateway 10.10.101.254. Ping Baidu (180.76.3.151) from a container on the host with an IP of 172.17.0.1). The IP package is first sent from the container to its default gateway, docker0, and when the packet arrives at docker0, it also arrives on the host. The routing table of the host is then queried and it is found that the packet should be sent from the host's eth0 to the host's gateway 10.10.105.254and24. The packet is then forwarded to eth0 and sent out of eth0 (ip_forward forwarding for the host should have been turned on). At this point, the above Iptable rule will take effect, SNAT the packet and change the source address to the address of the eth0. In this way, to the outside world, the packet is sent from 10.10.101.105, and the Docker container is not visible to the outside.

So how does the outside machine access the service of the Docker container? Let's first create a container containing the web application with the following command, mapping port 80 of the container to port 80 of the host.

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

Then look at the changes in the Iptable rules and find that there is one more rule:

-A DOCKER!-I docker0-p tcp-m tcp-- dport 80-j DNAT-- to-destination 172.17.0.5 tcp 80

This rule is to DNAT the tcp traffic received by the host eth0 with destination port 80, and send the traffic to 172.17.0.5 Docker 80, which is the Docker container we created above. Therefore, outsiders only need to visit 10.10.101.105virtual 80 to access the service in the container.

In addition, we can customize the IP address, DNS and other information used by Docker, or even use our own defined bridge, but it still works the same way.

User-defined mode

Users can customize the network through 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 address and host name.

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

Because the container may change the / etc/hosts file at any time, the program in the container may read an incomplete or even empty / etc/hosts file. Rereading usually solves this problem.

The above is all the contents of this article entitled "sample Analysis of Docker installation method and four Docker Network modes". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

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

Servers

Wechat

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

12
Report