Andrey Kurnikovs
Andrey Kurnikovs

Reputation: 427

Why do I have to state networks twice in docker-compose.yml?

I'm a bit confused. In docker-compose.yml file:

version: "3"
networks:
  proxy:
    external: true
services:
  eliko:
    image: irmscher/eliko:v1
    labels:
      - traefik.backend=eliko
      - traefik.frontend.rule=Host:eliko.bloggercraft.com
      - traefik.docker.network=proxy
    networks:
      - proxy

Why do I have to specify network twice? First time in networks: proxy and 2nd time within the services networks: -proxy?

I actually have created 'proxy' network with docker network create proxy beforehand. I thought networks: within the docker-compose CREATE networks, so I decided to remove it and only leave the network name within my services (eliko) header. But then I have received an error: ERROR: Service "eliko" uses an undefined network "proxy"

So my question is.. what does this exactly do:

networks:
  proxy:
    external: true

Since, as far as I understand, this:

    networks:
      - proxy

connects to a network, so the previous supposed to create the network? What if I create a network with docker network create, is it identical or?

A bit confused here...

Upvotes: 0

Views: 2182

Answers (1)

larsks
larsks

Reputation: 312370

The top-level networks stanza is telling docker-compose "these are the networks that exist". Without that, docker-compose wouldn't know what to do when you try to connect your eliko container to the proxy network. You'd get this:

ERROR: Service "eliko" uses an undefined network "proxy"

Remember that in the standard case, networks in docker-compose are prefixed with the project name. If you have a non-external network, like this:

version: "3"
networks:
  proxy:
services:
  eliko:
    image: irmscher/eliko:v1
    labels:
      - traefik.backend=eliko
      - traefik.frontend.rule=Host:eliko.bloggercraft.com
      - traefik.docker.network=proxy
    networks:
      - proxy

Then compose will actually create a network named project_proxy (where project is by default the name of the directory that contains your docker-compose.yml). It's only because you've declared the network to be external that compose knows you want to use an existing network.

Upvotes: 1

Related Questions