Docker-Compose 网络配置

一、未显式声明

  • docker-compose中未显式声明,会生成默认的网络
version: '3'
services:
  nginx:
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - 80:80
      - 443:443

二、Networks 关键字自定义网络

version: '3'
services:
  nginx:
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - 80:80
      - 443:443
    networks:
      - front
      - back
  tomcat:
    image: tomcat
    container_name: tomcat
    restart: always
    ports:
      - 8080:8080
    networks:
      - back    
networks:
  front:
    driver: bridge
  back:
    driver: bridge
    driver_opts:
      foo: "1"
      bar: "2"

三、配置默认网络

version: '3'
services:
  nginx:
    image: nginx
    container_name: nginx
    restart: always
    ports:
      - 80:80
      - 443:443
networks:
  default:
    driver: bridge

四、使用现有网络

4.1、创建专属网络

docker network create -d bridge --subnet 172.254.254.0/28 aka-network

4.2、使用专属网络

version: '3'
services:
  nginx:
    image: nginx
    container_name: nginx
    network_mode: bridge
    restart: always
    ports:
      - 80:80
      - 443:443
networks:
  default:
    external:
      name: vpn-network

五、Docker-Compose 中 network_mode

5.1、参数

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"

5.2、配置方法

version: '3'
services:
  nginx:
    image: nginx
    container_name: nginx
    network_mode: bridge
    restart: always
    ports:
      - 80:80
      - 443:443

六、修改网络接口数据

  • 如果需要修改网络接口数据,那么就建议使用--cap-add=NET_ADMIN,而不是使用--privileged。
    version: '3'
    services:
    nginx:
    image: nginx
    container_name: nginx
    restart: always
    cap_add:
      - NET_ADMIN
    ports:
      - 80:80
      - 443:443

版权声明:
作者:Akiraka
链接:https://www.akiraka.net/linux/docker/docker-compose/802.html
来源:Akiraka
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
海报
Docker-Compose 网络配置
一、未显式声明 docker-compose中未显式声明,会生成默认的网络 version: '3' services: nginx: image: nginx container_name: nginx ……
<<上一篇
下一篇>>