docker / libcompose

*Unmaintained/Deprecated* An experimental go library providing Compose-like functionality
https://godoc.org/github.com/docker/libcompose
Apache License 2.0
585 stars 191 forks source link

Only one network alias created #495

Closed jordanabderrachid closed 6 years ago

jordanabderrachid commented 6 years ago

Description

When defining several network aliases, only one is created.

System Info

Client:
 Version:      17.06.0-ce
 API version:  1.30
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:17:04 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.06.0-ce
 API version:  1.30 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:15:57 2017
 OS/Arch:      linux/amd64
 Experimental: false

Reproduce case

docker-compose.yml

version: '2'
networks:
  demo_network:
    driver: bridge
services:
  server:
    image: nginx
    container_name: server
    networks:
      demo_network:
        aliases:
          - server
          - foo.server
          - bar.server

main.go

package main

import (
    "github.com/docker/libcompose/docker"
    "github.com/docker/libcompose/docker/ctx"
    "github.com/docker/libcompose/project"
    "github.com/docker/libcompose/project/options"
    "golang.org/x/net/context"
)

func main() {
    project, err := docker.NewProject(&ctx.Context{
        Context: project.Context{
            ComposeFiles: []string{"./docker-compose.yml"},
        },
    }, nil)
    if err != nil {
        panic(err)
    }

    err = project.Up(context.Background(), options.Up{})
    if err != nil {
        panic(err)
    }
}

docker inspect server

[
    {
        "Id": "73a4813435c27d692b548ffab40feb7532c47d6570541d0301ffc88f21757e21",
        "Created": "2017-09-22T15:30:28.303493263Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 9454,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2017-09-22T15:30:28.79599801Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:da5939581ac835614e3cf6c765e7489e6d0fc602a44e98c07013f1c938f49675",
        "ResolvConfPath": "/var/lib/docker/containers/73a4813435c27d692b548ffab40feb7532c47d6570541d0301ffc88f21757e21/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/73a4813435c27d692b548ffab40feb7532c47d6570541d0301ffc88f21757e21/hostname",
        "HostsPath": "/var/lib/docker/containers/73a4813435c27d692b548ffab40feb7532c47d6570541d0301ffc88f21757e21/hosts",
        "LogPath": "/var/lib/docker/containers/73a4813435c27d692b548ffab40feb7532c47d6570541d0301ffc88f21757e21/73a4813435c27d692b548ffab40feb7532c47d6570541d0301ffc88f21757e21-json.log",
        "Name": "/server",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": [],
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "bug_demo_network",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Dns": null,
            "DnsOptions": null,
            "DnsSearch": null,
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                0,
                0
            ],
            "Isolation": "",
            "CpuShares": 0,
            "Memory": 0,
            "NanoCpus": 0,
            "CgroupParent": "",
            "BlkioWeight": 0,
            "BlkioWeightDevice": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DeviceCgroupRules": null,
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": 0,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": [],
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Data": null,
            "Name": "aufs"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "73a4813435c2",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.13.5-1~stretch",
                "NJS_VERSION=1.13.5.0.1.13-1~stretch"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "nginx",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "com.docker.compose.config-hash": "4625e2b936c65aaf401fca0fc68bc66e80dd6057",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "bug",
                "com.docker.compose.service": "server",
                "com.docker.compose.version": "1.5.0",
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "8473725da1cc517812f76d864b20f46b38e4891970d8056c6838d046cd41ae83",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/8473725da1cc",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bug_demo_network": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "server",
                        "73a4813435c2"
                    ],
                    "NetworkID": "97467dcfb1ddfb9b0d9a2c42ebc9284ba4de44822e2754b4580db700c60757f6",
                    "EndpointID": "18593318fbebeb13bad144e183e738589c8df83f78125f3e65cddbc4918c6640",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]
xihan88 commented 6 years ago

Possibly related issue: https://github.com/moby/moby/issues/33661

vito-c commented 6 years ago

@vdemeester what will it take to get this fixed? updating docker vendor to moby?

I just noticed this issue and I saw that if I use two networks the 2nd one works (as long as the 2nd one is not the default).

---
version: '2'
services:
    test.env:
        container_name: test.env
        image: alpine
        environment:
            FOO: ${ABC:-foo}
        command: env
        networks:
            default:
                aliases:
                    - does.not.work
            notdefault:
                aliases:
                    - works

Which brought me to another question. How do you create an alias for an external network in docker-compose v2??

You can't duplicate keys so this won't work

---
version: '2'
services:
    test.env:
        container_name: test.env
        image: alpine
        environment:
            FOO: ${ABC:-foo}
        command: env
        networks:
            - something:
         networks:
                something:
                    aliases:
                      - dunno
networks:
  something:
     external: true
vito-c commented 6 years ago

@vdemeester I have a "hack" that I was able to implement to get aliases working (at least for my project).

./vendor/github.com/docker/libcompose/docker/service/service.go

+       if len(net.Aliases) >= 1 {
+               client.NetworkConnect(ctx, net.RealName, containerID, &network.EndpointSettings{
+                       Aliases:   aliases,
+                       Links:     links,
+                       IPAddress: net.IPv4Address,
+                       IPAMConfig: &network.EndpointIPAMConfig{
+                               IPv4Address: net.IPv4Address,
+                               IPv6Address: net.IPv6Address,
+                       },
+               })
+               client.NetworkDisconnect(ctx, net.RealName, containerID, true)
+       }

essentially reconnect quickly to the network to trick the docker client into creating the alias.

vito-c commented 6 years ago

I will make a PR with this so it's easier to see the change