使用同一网络的多个项目的Docker DNS

问题描述:

我有以下 docker-compose.yml 文件:

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

基于从上面的文件中,我创建了两个使用相同网络(共享)和相同服务名称( frontend )的项目, 后端):

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

docker的DNS是否确保 docker-compose -p foo exec前端ping后端仅解析为项目 foo 中的后端容器反之亦然,对于项目 bar

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?

根据 https://github.com/docker/compose/issues/4645 ,其中的解析顺序不确定的情况。由于网络正在使用golang转换为无序dict,因此不会保留顺序。这意味着 https://github.com/docker/libnetwork/blob/ master / sandbox.go#L593 被查询的端点顺序与网络顺序不匹配。

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.

解决方案是定义如果使用docker-compose版本2,则https://docs.docker.com/compose/compose-file/compose-file-v2/#priority ,或者将标准dns名称作为 service.network ,例如 backend.foo_default backend.shared

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.