Futile32
Futile32

Reputation: 934

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).

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.

Upvotes: 16

Views: 28085

Answers (6)

Christian Schmidt
Christian Schmidt

Reputation: 1

Actually, there is a way to create a network for docker compose using docker network create. I had similar issue where I needed to know before hand the network IPAM, but didn't want to hardcode it. This is what I used to create the network before running docker compose up:

docker network create -d bridge --attachable --label "com.docker.compose.network=default" --label "com.docker.compose.project=<name-of-the-compose-project>" <name-of-the-network>

Being the compose file something like:

version: "3.7"

services:
  main:
    image: busybox
    extra_hosts:
      - mylocaldns:${DOCKER_IPAM}

networks:
  default:
    name: <name-of-the-network>

With all this, I could run the below commands allowing the compose file to grab the newly created network IP gateway.

DOCKER_IPAM=$(docker network inspect -f '{{range .IPAM.Config}}{{.Gateway}}{{end}}' <name-of-the-network>) docker compose -p <name-of-the-compose-project> up

Hope it helps someone!

Upvotes: 0

yananet
yananet

Reputation: 151

Ugly, but worked for me.

version: "2.1"
                                                                                                                                
services:                                                                                                                                     
  network-only:                                                                                                                               
    image: tianon/true                                                                                                                        
    container_name: network-only                                                                                                              
    networks:
      - mybr0

networks:
  mybr0:
    name: mybr0
    driver: bridge
    driver_opts:
      com.docker.network.bridge.name: mybr0

Upvotes: 1

Jaosn Marcle
Jaosn Marcle

Reputation: 1

Actually you just need to set an "ipv4_address" which is in your bridge subnet range, just like @Alfa Bravo said.

Such as

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


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

So docker-compose can add your own bridge successfully.

Upvotes: 0

EthraZa
EthraZa

Reputation: 438

docker-compose-networks.yml:

version: '3.7'

services:
    works:
        image: scratch
        restart: always
        container_name: net_works
        deploy:
            placement:
                constraints:
                    - "node.hostname==scratch"
        networks:
            - mynet1
            - mynet2

networks:
    mynet1:
        name: mynet1
        driver: overlay
        attachable: false
    mynet2:
        name: mynet2
        driver: overlay
        attachable: true

Upvotes: 7

Alfa Bravo
Alfa Bravo

Reputation: 1991

All my docker images and networks etc.. files I like to run with docker-compose. So while my thought process is on docker-compose I just have docker-compose network files that spin up a hello-world image (many people already have the image, else it is like 2kb) and then it immediately exits the image of coarse but keeps the network.

version: '3.7'

services: 
    hello_world:
        image: hello-world:latest
        networks: 
            main_net:
                ipv4_address: 172.20.0.255
networks: 
    main_net:
        name: main_network
        ipam:
            config:
                - subnet: 172.20.0.0/16

This way I plan my network and lay it out nicely in docker-compose file with th minimum overhead...

Upvotes: 5

BMitch
BMitch

Reputation: 263627

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.

Upvotes: 15

Related Questions