Create networks automatically in Docker Compose

80,381

Solution 1

external is to use an existing network. If you want compose to make networks you simply do:

networks:
  network1:
  network2:

.. to instruct compose to make the networks. They will be named <compose-dir>-<network name> Verify the creation by using docker network ls.

You can also override the default network or specify more properties.

networks:
  default:
    driver: bridge
    driver_opts:
      com.docker.network.driver.mtu: 1450

.. will override the default network setting MTU to 1450 (handy when the host have lower than 1500 mtu and path mtu discovery doesn't work properly). There are other bridge driver options.

external makes more sense when you want services in two compose setups to talk to each other or you have a manually created network.

Solution 2

As @Grimmy correctly states, docker creates all mentioned networks, so these can be later referenced by running another compose file.

But, the defaultly generated network name is hardly practlical or robust. As it could turn out to be very long or the docker team changes their opinion on naming strategy.

But since compose file version 3.5 (means docker-compose version 1.18.0) one can name the network as pleases, so the overal solution is even more robust.

Please see the following snippets demonstrating on how to achieve this:

Compose file 1

version: '3.5'
services:
  svc-name:

    ...                          

    networks:
      - specific-network-name
    container_name: "exact-container-reference"

    ...

networks:
  specific-network-name:
    external: false
    name: specific-network-name

Compose file 2

version: '2'
services:

  svc-using-svc-name:

    ...

    networks:
      - default
      - specific-network-name
    external_links:
      - exact-container-reference

    ...

networks:
  specific-network-name:
    external: true

Solution 3

I also had this error:

Network "NETWORK_NAME" needs to be recreated - option "driver" has changed
   docker network ls
   docker network rm NETWORK_NAME
   docker-compose up

After removing the problem network and recreating it, things were fine.

Share:
80,381
Marian Klühspies
Author by

Marian Klühspies

Enthusiastic Software Developer. Started with Android Apps in 2013, moved to backend and frontend development and knows about Spring, Jakarta EE and Node.js. Looking for the easiest, most profitable ad solution for Android, iOS, Unity and so on? Appodeal It combines all major ad networks with just a single registration required and optimizes your profit!

Updated on April 10, 2020

Comments

  • Marian Klühspies
    Marian Klühspies about 4 years

    When using custom networks in Docker like

    networks:
      default:
        external:
          name: service-tier
    

    And try to run that, I´m getting the error

    ERROR: Network service-tier declared as external, but could not be found. Please create the network manually using docker network create service-tier and try again.

    Is it possible to create networks automatically with docker-compose or is there no other way than creating them manually first?