Can I use Docker-Compose to create Networks without Containers

I’ve been playing with Docker-Compose for the last few days to see if it would simplify my Docker Container and Network building process.

I’m pretty happy with it, but ran into a problem when I wanted to create a few ‘Networks’ that didn’t get used by any ‘Services’ (yet).

  • Unable to Pull image from quay.io: ERROR x509: certificate signed by unknown authority
  • [pacemaker]Docker compose autostart
  • How to execute commands in docker container as part of bash shell script
  • “golang.org/x/net/ipv4” working on Mac, but not on Linux
  • How to build docker image from github repository
  • Testing a Node library working with Docker containers
  • The reason I want this behavior was to have a Docker-Compose file to create my local fabric of ‘Private’ network and ‘Public’ Network. And a separate Docker-Compose for each of my projects that utilize those already created ‘external’ networks.

    I noted that I was able to just specify a dummy container to initialize the creation of the Networks, but it seemed unnecessary. e.g..

    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 a setting/flag that I’m overlooking, or is there currently no way to use Docker-Compose to create Networks without Containers.

    Additionally, am I just approaching this incorrectly?

    Essentially I’d like to have a network that live on regardless of what containers join/leave it.

  • Why is crond failing to run a non-root crontab on alpine linux?
  • one docker daemon can support how many docker containers? [closed]
  • Connecting to mongo container using node getting MongoError
  • App running in Docker on EB refuses connecting to self
  • Docker default CPU architecture doesn't match host's
  • Docker Compose Shared Folder
  • One Solution collect form web for “Can I use Docker-Compose to create Networks without Containers”

    There’s a check inside of docker-compose for whether the network is used, and if it’s unused, 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’d recommend doing this as a small shell script, calling docker network create instead of trying to use docker-compose for this task.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.