Question: I have the following networks configuration in my docker compose file.

```
networks:
    default:
        ipam:
            driver: default
            config:
                - subnet: ${DOCKER_SUBNET}

```

When `DOCKER_SUBNET` is set, the subnet specified in that variable is used as expected. When the variable is not set I get: `ERROR: Invalid subnet : invalid CIDR address:` because the variable is blank (which is entirely reasonable).

Is there a way to configure the ipam driver such that when the `DOCKER_SUBNET` variable is not set, docker-compose will choose an available subnet as it would normally do if the ipam configuration was not given?

Answer: 
Compose will only choose an available subnet if you don't provide any `ipam` configuration for the network. Compose doesn't have advanced functionality to modify config on the fly.

You could make the decision outside of compose, either with [multiple compose files](https://docs.docker.com/compose/extends/#multiple-compose-files) or a template based system, in shell or some other language that launches the `docker-compose` command.

Seperate the compose network config from the rest of the service config in files:

`docker-compose-net-auto.yml`

```
version: "2.1"
networks:
  default:

```

`docker-compose-net-subnet.yml`

```
version: "2.1"
networks:
  default:
    ipam:
      driver: default
      config:
        - subnet: ${DOCKER_SUBNET}

```

Then create a script `launch.sh` that makes the choice of which network file to include.

```
#!/bin/sh
if [ -z "$DOCKER_SUBNET" ]; then
  docker-compose -f docker-compose.yml -f docker-compose-net-auto.yml up
else
  docker-compose -f docker-compose.yml -f docker-compose-net-subnet.yml up
fi

```
```
networks:
  Mynetwork:
      driver: bridge
      ipam:
          driver: default
          config:
              - subnet: "192.168.0.0/24"
                gateway: "192.168.0.1"
  #- default:
      #driver: bridge

```