iPogosov
iPogosov

Reputation: 367

How to restrict creating default network from docker-compose file

I have overlapping with default docker subnets, so I want to setup for some services custom network. docker-compose version:3

example of docker-compose.yml

version: '3'

services:
  service1:
    build:
      dockerfile: Dockerfile


  service2:
      build:
      dockerfile: Dockerfile1


  service3:
      build:
      dockerfile: Dockerfile2
      networks:
        - net1

networks:
  net1:
    ipam:
      driver: default
      config:
        - subnet: "172.101.0.0/24"

So when I execute the following command I got 2 networks : docker-compose up --build -d service3

project_default and project_net1

But I need default network for other services so I can't change default settings. What should I do to not create default network if it's not used for the service?

Upvotes: 2

Views: 5469

Answers (1)

David Maze
David Maze

Reputation: 158937

If you don't have a networks: specification for a given service, Docker Compose behaves as though you specified networks: [default]. In your example, service1 and service2 are on the default network and service3 is on the net1 network.

If you really need Docker Compose to not create the project_default network then you need to assign every container to some other network. From experimenting with a minimal docker-compose.yml file, explicitly adding networks: [net1] to the two services that don't already have it will cause the default network to not be created.

If your real issue is just around an IP address conflict, you're allowed to manually configure the default network and this might be easier.

version: '3'
services:
  service1:
    build: .
    # with no networks:, so it gets the default
networks:
  default:
    ipam:
      driver: default
      config:
        - subnet: "172.101.0.0/24"

Upvotes: 3

Related Questions