我可以创建仅创建网络的docker-compose.yaml吗?

时间:2019-11-03 13:05:23

标签: docker docker-compose

我有一组用于创建服务和自定义网络的docker-compose文件。一些撰写文件在其他文件中引用了网络。

例如:

version: '2.2'
services:
  (...)


volumes:
  (...)

networks:
  management:
    driver: macvlan
    driver_opts:
      parent: enp1s0.10 
    ipam:
      config:    
      - subnet: 192.168.10.0/24
  home:
    driver: macvlan
    driver_opts:
      parent: enp1s0
    ipam:
      config:
      - subnet: 192.168.3.0/24
        gateway: 192.168.3.1

此撰写文件创建了两个服务和两个网络。

现在,我还有另一个撰写文件,它也使用这些网络之一,并创建了它们自己的另一个网络:

version: '3'
services:
  (...)

networks:
  home:
    external:
      name:               my_other_service_home
  iot:
    driver: macvlan
    driver_opts:
      parent: enp1s0.12
    ipam:
      config:
      - subnet: 192.168.12.0/24

为清楚起见,我希望在单独的文件中创建所有网络。然后只需在其他撰写文件中将它们命名为外部网络即可。

但这不起作用:

version: '2'

networks:
  iot:
    driver: macvlan
    driver_opts:
      parent: enp1s0.12
    ipam:
      config:
      - subnet: 192.168.12.0/24
  management:
    driver: macvlan
    driver_opts:
      parent: enp1s0.10
    ipam:
      config:
      - subnet: 192.168.10.0/24
  home:
    driver: macvlan
    driver_opts:
      parent: enp1s0
    ipam:
      config:
      - subnet: 192.168.3.0/24
        gateway: 192.168.3.1

该文件已被接受,没有错误,但是我得到的消息是未使用网络,因此未创建网络。

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

如何在您的networks.yml文件中创建虚拟服务,该服务不执行任何操作并立即退出?

遵循以下原则:

创建网络

# networks.yml
version: '3.7'

services:
  dummy:
    image: alpine
    networks: [hello, sky]

networks:
  sky:
    name: skynet

  hello:
    name: hellonet

创建服务

# service.yml
version: '3.7'

services:
  service:
    image: alpine
    networks: [sky]

networks:
  sky:
    external:
      name: skynet

执行

$ docker-compose -f networks.yml up
$ docker-compose -f service.yml up