I am trying to create hostname alias using docker compose according to the documentation, however it doesn't seem to be working - nslookup/ping fails. What am I doing wrong?
Here is docker-compose.yml
version: "3"
services:
  service1:
    image: alpine:latest
    command: ["sleep", "infinity"]
    networks:
      mynet:
        aliases:
          - service1alias
  service2:
    image: alpine:latest
    command: ["nslookup", "service1alias"]
    networks:
      - mynet
    depends_on:
      - service1
networks:
    mynet:
Output:
~/temp  docker compose up --build
[+] Running 2/0
⠿ Container temp_service1_1  Created
⠿ Container temp_service2_1  Created
Attaching to service1_1, service2_1
service2_1  | Server:       127.0.0.11
service2_1  | Address:  127.0.0.11:53
service2_1  |
service2_1  | ** server can't find service1alias: NXDOMAIN
service2_1  |
service2_1  | ** server can't find service1alias: NXDOMAIN
service2_1  |
service2_1 exited with code 1
When I am changing command: ["nslookup", "service1alias"] to command: ["nslookup", "service1"] (without aliasing) - it works as expected:
~/temp  docker compose up --build
[+] Running 2/0
⠿ Container temp_service1_1  Created
⠿ Container temp_service2_1  Recreated
Attaching to service1_1, service2_1
service2_1  | Server:       127.0.0.11
service2_1  | Address:  127.0.0.11:53
service2_1  |
service2_1  | Non-authoritative answer:
service2_1  | Name: service1
service2_1  | Address: 172.31.0.2
service2_1  |
service2_1  | Non-authoritative answer:
service2_1  | *** Can't find service1: No answer
service2_1  |
service2_1 exited with code 0
Host system, docker version, network details, etc.:
~/temp  sw_vers                                                                                              INT ✘  00:24:24
ProductName:    macOS
ProductVersion: 11.3.1
BuildVersion:   20E241
~/temp  docker --version
Docker version 20.10.6, build 370c289
~/temp  brew info homebrew/cask/docker
docker: 3.3.3,64133 (auto_updates)
https://www.docker.com/products/docker-desktop
/usr/local/Caskroom/docker/3.3.3,64133 (120B)
From: https://github.com/Homebrew/homebrew-cask/blob/HEAD/Casks/docker.rb
==> Names
Docker Desktop
Docker Community Edition
Docker CE
==> Description
App to build and share containerized applications and microservices
==> Artifacts
Docker.app (App)
==> Analytics
install: 19,099 (30 days), 56,412 (90 days), 211,283 (365 days)
~/temp  docker network inspect temp_mynet
[
    {
        "Name": "temp_mynet",
        "Id": "e346bb39e435812cdcde3ba1ba554f8bca48c46d012ac944ea2e87db8f630cde",
        "Created": "2021-05-21T21:13:07.966393Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.31.0.0/16",
                    "Gateway": "172.31.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {},
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "mynet",
            "com.docker.compose.project": "temp",
            "com.docker.compose.version": "1.0-alpha"
        }
    }
] 
OK, I can answer myself: looks like a bug in a docker compose: "docker compose up --build" fails while "docker-compose up --build" works as expected (note dash in docker-compose).
I've spot it while reviewing this question and noted "com.docker.compose.version": "1.0-alpha"
seems to be fixed in v.1.0.15 I can confirm the issue disappears in Docker Compose version v2.3.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With