docker-compose使用host网络

根据官方文档, 如果使用stack命令部署,可以使用networks参数配置,如果直接使用docker-compose, 使用network_mode参数配置。

Use the host’s networking stack, or no networking. Equivalent to docker run –net=host or docker run –net=none. Only used if you use docker stack commands. If you use the docker-compose command, use network_mode instead. —-compose file reference: https://docs.docker.com/compose/compose-file/#devices

stack

version: "3.8"
services:
  web:
    networks:
      hostnet: {}

networks:
  hostnet:
    external: true
    name: host

docker-compose

services:
  web:
    network_mode: "host"

问题

如果在docker-compose中使用networks参数配置系统默认网卡,会报以下错误:

Creating zerotier-one ... error

ERROR: for zerotier-one  network-scoped alias is supported only for containers in user defined networks

ERROR: for one  network-scoped alias is supported only for containers in user defined networks
ERROR: Encountered errors while bringing up the project.

原因可以参考 http://yukinami.github.io/2017/03/24/Docker-compose%E4%BD%BF%E7%94%A8%E9%BB%98%E8%AE%A4%E7%9A%84bridge%E7%BD%91%E7%BB%9C/