1

I have the following docker-compose.yml file:

version: '3'
services:
    frontend:
        image: alpine
        command: tail -f /dev/null
        networks:
            - shared
            - default
    backend:
        image: alpine
        command: tail -f /dev/null
        networks:
            - shared
            - default
networks:
    shared:
        external: true

Based on the file from above I create two projects which use the same network (shared) and the same service names (frontend and backend):

docker-compose -p foo up -d
docker-compose -p bar up -d

Does the DNS of docker make sure that docker-compose -p foo exec frontend ping backend only resolves to the backend container in project foo and vice versa for project bar?

Morty Choi
  • 2,466
  • 1
  • 18
  • 26

3 Answers3

2

According to https://github.com/docker/compose/issues/4645, the resolve order in this case in non deterministic. Since the network is being converted to unordered dict in golang, the order is not preserved. Which implies https://github.com/docker/libnetwork/blob/master/sandbox.go#L593 the order of endpoints being queried don't match the order of network.

The solution is to define https://docs.docker.com/compose/compose-file/compose-file-v2/#priority if using docker-compose version 2. Or fully qualified dns name as service.network such as backend.foo_default or backend.shared.

Morty Choi
  • 2,466
  • 1
  • 18
  • 26
1

Based on your setup I have used nslookup to find out whether the DNS resolution is isolated or not.

$ docker-compose -p foo exec frontend nslookup backend

Name:      backend
Address 1: 172.19.0.2 foo_backend_1.shared
Address 2: 172.19.0.4 bar_backend_1.shared

As you can see from the output above, backend resolves to both of the containers.

prd
  • 2,272
  • 2
  • 17
  • 32
  • If assign a local network to both services. It seems that it only resolve to the local network service. I am not sure it’s the order of the network is important or not. – Morty Choi Feb 04 '19 at 00:42
  • 1
    @prd Was the description (i.e. `docker-compose.yml` file) different, when you wrote your answer? `nslookup` only finds the service of the `backend` of the network `default` on my Mac (`docker-compose version 1.29.2`) following the current description of the question and your answer. Maybe something changed in the newer Docker Compose version. – maiermic Jul 24 '21 at 17:24
  • 1
    @maiermic I guess something with Docker's name resolution must have changed. I could verify that the same setup does not work as described in my answer. [This](https://stackoverflow.com/a/48024244/10975585) might be relevant. – prd Jul 25 '21 at 20:05
0

If you use docker swarm you can qualify hostnames with the service name to disambiguate containers. But I don't believe docker-compose does this.

Ryan
  • 4,594
  • 1
  • 32
  • 35