Can I use Docker-Compose to create containerless networks

I have been playing around with Docker-Compose for the past few days to make sure it simplifies the Docker Container and Network building process.

I am very happy with this, but ran into a problem where I wanted to create several "networks" that were not used by any "services" (yet).

I want this behavior to create a Docker-Compose file to create my 'Private' and 'Public' Networks. And a separate Docker-Compose for each of my projects that use the already created "external" networks.

I noticed that I was able to just specify a dummy container to initiate networking, but that seemed unnecessary. eg..

version: '2'
services:
  # Dummy Service
  dummy:
    image: busybox
    container_name: dummy
    hostname: dummy
    networks:
      private:

networks:
  # Private Network for all Services (across Projects)
  private:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 172.18.0.0/24
        gateway: 172.18.0.1

      

Is there an option / flag I'm missing, or there is currently no way to use Docker-Compose to create containerless networks.

Also, am I just approaching this the wrong way?

Basically, I would like to have a network that lives regardless of which containers join / leave it.

+3


source to share


1 answer


Inside docker-compose it checks if the network is in use and if it is not in use it skips creating the network:

$ cat docker-compose.net-only.yml
version: '2'

networks:
  test1:
  test2:

$ docker-compose -f docker-compose.net-only.yml --verbose up
.....
WARNING: compose.network.from_services: Some networks were defined but are not used by any service: test1, test2
.....

$ docker network ls | grep test

$

      



I would recommend doing this as a small shell script by calling docker network create

instead of trying to use docker-compose for this task.

+4


source







All Articles