How does docker-compose create aliases for link ips

5/30/2018

when running docker-compose, all containers have network aliases to the ip of the other containers running in the network. How are these aliases created ?

I need to recreate an alias in an nginx container within a Kubernetes cluster, because nginx conf doesn't allow environment variables and I reverse proxy requests to another container from there. I would normally edit the /etc/hosts file, but since docker-compose doesn't, I'm wondering how is the alias created and if I could do it the same way within my kubernetes cluster.

-- Adrien Lemaire
alias
docker
docker-compose
ip
kubernetes

2 Answers

5/30/2018

As written in the documentation

Containers for the linked service are reachable at a hostname identical to the alias, or the service name if no alias was specified

This means if you have a docker-compose file like this:

version: '3'
services:
  nginx:
     links:
       - app1
     ...
  app1:
    ...

Then nginx is able to access the other container by app1.

-- Mathias
Source: StackOverflow

5/30/2018

In a kubernetes cluster, there is a DNS server service deployed by default.

kubectl get svc should show the DNS service IP address.

You can find the DNS server IP address inside the containers /etc/resolv.conf file too.

See here for more info.

You can specify additional entries in container's /etc/hosts file using HostAliases. See here.

Snip:

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
-- leodotcloud
Source: StackOverflow