docker host模式拿到nginx远程ip端口_docker网络模式实战
docker有四种网络模式,分别为Bridge、Host、Container、None。Bridge为docker的默认网络模式。这里我分享下Bridge、Host、Container模式下容器的创建及启动。
Bridge模式:当Docker进程启动时,会在主机上创建一个名为docker0的虚拟网桥,此主机上启动的Docker容器会连接到这个虚拟网桥上。从docker0子网中分配一个IP给容器使用,并设置docker0的IP地址为容器的默认网关。这样容器间就可以相互通信。

docker0
创建容器后,查看容器信息

容器信息
Bridge模式很好理解,而且在创建容器时,也不用指定,因为他是默认的。
Host模式:如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。这里我拿tomcat测试下

拉取tomcat8
由于host模式下使用的是宿主机的IP和端口,所以要注意宿主机有没有把端口开启的问题。如何打开端口,比如8080端口。

开启8080端口
firewall-cmd --add-port=8080/tcp --permanentfirewall-cmd --reload
通过host创建容器
docker run -d --net=host --name to1 94e31e5297d1
94e31e5297d1为tomcat8的镜像ID

host模式下创建容器

container模式:这个模式指定新创建的容器和已经存在的一个容器共享一个Network Namespace,而不是和宿主机共享。新创建的容器不会创建自己网卡,配置自己的IP,而是和一个指定的容器共享IP,端口范围。
这里我拿nginx和tomcat为例,先创建一个nginx容器,并开启80、8080端口
docker run -d --name nginx -p 80:80 -p 8080:8080 7743d9092020
7743d9092020为nginx镜像ID

创建nginx容器
分别访问80和8080端口

80端口

8080端口
8080不好使,是因为我们还没有开启。使用container创建tomcat8容器。
docker run -d --name to1 --net=container:nginx 94e31e5297d1
nginx:是我上面nginx的名称
94e31e5297d1:是tomcat8镜像ID

container创建tomcat8容器

访问8080
以上就是3种模式的基本用法、后期分享更多docker方面的内容