13_docker-compose使用自定义网络(五)-HOST

docker-compose使用自定义网络(五)-HOST

一、编写docker-compose.yml文件

在特殊情况下需要使用host映射方式将应用对外暴露,使用docker network 中的HOST模式

version: "2"
services:
  web:
    image: nginx:1.19.9-alpine
    restart: always
    ports:
    - 80:80
    network_mode: host

二、启动应用

[root@localhost nginx]# docker-compose docker-compose.yml up -d
Creating nginx_web_1 ... done

三、查看应用状态

[root@localhost nginx]# docker-compose ps
   Name                  Command               State   Ports
------------------------------------------------------------
nginx_web_1   /docker-entrypoint.sh ngin ...   Up

 查看docker network 

[root@localhost nginx]# docker network ls 
NETWORK ID     NAME            DRIVER    SCOPE
2d765eda5843   bridge          bridge    local
c2b1d33e2017   host            host      local
0b1c6439c050   none            null      local
[root@localhost  nginx]# docker network inspect c2b1d33e2017
    ...
      "ConfigOnly": false,
        "Containers": {
            "d696ca24005d78633de065268638cc93cf8de9061a2f3306b750a807f47fe114": {
                "Name": "nginx_web_1",
                "EndpointID": "ef14215e199c8a0192f422bfacca496988e8d8ee17a1026dd238b1d8f326878a",
                "MacAddress": "",
                "IPv4Address": "",
                "IPv6Address": ""
            }
        }
     ...

四、验证应用网络状态 

Host模式与Bridge模式操作系统显示下的区别

# HOST模式
[root@localhost nginx]# netstat -lnpt | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21485/nginx: master 
tcp6       0      0 :::80                   :::*                    LISTEN      21485/nginx: master
# Bridge模式
[root@localhost nginx]# netstat -lnpt |grep 80 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      19540/docker-proxy 

你可能感兴趣的:(#,docker-compose,dokcer,docker-compose,host,docker网络,docker,network)