1

In docker compose we have external_links option, but how it work?

Just link writes to /etc/hosts, but external_links is not.

Please explain how this option is working, because in docks no one words about it.

1

When you create a docker-compose.yml like this:

version: '2'

services:
  sleep-one-hour:
    image: alpine
    command: sleep 3600
    external_links:
      - google.com:216.58.222.110

And do not specify a default network configuration, the docker-compose actually creates a custom default network by the name of the folder you are running the docker-compose up command:

[myserver:external-links-test] docker-compose up
Creating network "externallinkstest_default" with the default driver
Creating externallinkstest_sleep-one-hour_1
Attaching to externallinkstest_sleep-one-hour_1

Then the container is created and joins this custom network, and the external link is set when the container joins the network. You can inspect the recently created container and see for yourself:

[myserver:~] docker inspect 1056bb135bc0
[
    {
        "Id": "1056bb135bc0200dcfff1fa25affd561042e0515641adf435e4b63a16903f93d",
        "Created": "2017-01-26T14:31:57.928385177Z",
        ... other info ...
        "NetworkSettings": {
            ... other info ...
            "Networks": {
                "externallinkstest_default": {
                    "IPAMConfig": null,
                    "Links": [
                        "google.com:216.58.222.110"
                    ],
                    "Aliases": [
                        "1056bb135bc0",
                        "sleep-one-hour"
                    ],
                    "NetworkID": "343121714be4750eebf12997fa73c3aaba0f2cf5faace633ab1ca683a2959632",
                    "EndpointID": "74f1a975236a8ab0f60379408faa04022f1b9e398c7b8a541158c4ca889aff98",
                    "Gateway": "172.20.0.1",
                    "IPAddress": "172.20.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:14:00:02"
                }
            }
        }
    }
]

The docker-compose up actually translates to something like this in pure docker commands:

[myserver:external-links-test] docker network create --attachable --subnet 61.0.0.0/24 externallinkstest_default
13f1b9caad1b997245be0fe48922ff04568c7bcf1e3bdd8846d87f53934ca390
[myserver:external-links-test] docker run -d --name externallinkstest_sleep-one-hour_1 alpine sleep 3600
17b642eb4a3cb562783ae76192589d55cb9174cd2c8a705464f62108ff11372c
[myserver:external-links-test] docker network connect --link google.com:216.58.222.110 externallinkstest_default externallinkstest_sleep-one-hour_1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.