Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create networks automatically in Docker Compose

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?

like image 754
Marian Klühspies Avatar asked Jul 22 '17 13:07

Marian Klühspies


People also ask

Does Docker compose automatically create network?

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

How do I create a Docker compose 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 .

What is the default network that the Docker creates automatically?

When you start Docker, a default bridge network (also called bridge ) is created automatically, and newly-started containers connect to it unless otherwise specified. You can also create user-defined custom bridge networks. User-defined bridge networks are superior to the default bridge network.

When you install Docker how many networks are created automatically?

When you install docker it creates three networks automatically - Bridge, Host, and None. Of which, Bridge is the default network a container gets attached to when it is run.


2 Answers

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.

like image 107
Grimmy Avatar answered Oct 03 '22 21:10

Grimmy


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 
like image 38
helvete Avatar answered Oct 03 '22 21:10

helvete