Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local hostnames for Docker containers

Beginner Docker question here,

So I have a development environment in which I'm running a modular app, it is working using Docker Compose to run 3 containers: server, client, database.

The docker-compose.yml looks like this:

############################# # Server ############################# server:   container_name: server   domainname: server.dev   hostname: server   build: ./server   working_dir: /app   ports:     - "3000:3000"   volumes:     - ./server:/app   links:     - database  ############################# # Client ############################# client:   container_name: client   domainname: client.dev   hostname: client   image: php:5.6-apache   ports:      - "80:80"   volumes:    - ./client:/var/www/html  ############################# # Database ############################# database:   container_name: database   domainname: database.dev   hostname: database   image: postgres:9.4   restart: always   environment:     - POSTGRES_USER=postgres     - POSTGRES_PASSWORD=root     - POSTGRES_DB=dbdev     - PG_TRUST_LOCALNET=true   ports:     - "5432:5432"   volumes:     - ./database/scripts:/docker-entrypoint-initdb.d # init scripts 

You can see I'm assigning a .dev domainname to each one, this works fine to see one machine from another one (Docker internal network), for example here I'm pinging server.dev from client.dev's CLI:

    root@client:/var/www/html# ping server.dev     PING server.dev (127.0.53.53): 56 data bytes     64 bytes from 127.0.53.53: icmp_seq=0 ttl=64 time=0.036 ms 

This works great internally, but not on my host OS network.

For convenience, I would like to assigns domains in MY local network, not the Docker containers network so that I can for example type: client.dev on my browsers URL and load the Docker container.

Right now, I can only access if I use the Docker IP, which is dynamic:

client: 192.168.99.100:80 server: 192.168.99.100:3000 database: 192.168.99.100:5432 

Is there an automated/convenient way to do this that doesn't involve me manually adding the IP to my /etc/hosts file ?

BTW I'm on OSX if that has any relevance.

Thanks!

Edit: I found this Github issue which seems to be related: https://github.com/docker/docker/issues/2335

As far as I understood, they seem to say that it is something that is not available outside of the box and they suggest external tools like:

  • https://github.com/jpetazzo/pipework
  • https://github.com/bnfinet/docker-dns
  • https://github.com/gliderlabs/resolvable

Is that correct? And if so, which one should I go for in my particular scenario?

like image 974
Edu Wass Avatar asked Mar 22 '16 10:03

Edu Wass


People also ask

What is the hostname of a docker container?

In the same way, a container's hostname defaults to be the container's ID in Docker. You can override the hostname using --hostname . When connecting to an existing network using docker network connect , you can use the --alias flag to specify an additional network alias for the container on that network.

Do docker containers have their own localhost?

Docker provides a host network which lets containers share your host's networking stack. This approach means localhost inside a container resolves to the physical host, instead of the container itself.

What is localhost IP from docker container?

Ok, your localhost server has a default docker interface docker0 with IP address 172.17. 0.1 .


1 Answers

OK,

so since it seems that there is no native way to do this with Docker, I finally opted for this alternate solution from Ryan Armstrong, which consists in dynamically updating the /etc/hosts file.

I chose this since it was convenient for me since this works as a script, and I already had a startup script, so I could just append this function in to it.

The following example creates a hosts entry named docker.local which will resolve to your docker-machine IP:

update-docker-host(){     # clear existing docker.local entry from /etc/hosts     sudo sed -i '' '/[[:space:]]docker\.local$/d' /etc/hosts      # get ip of running machine     export DOCKER_IP="$(echo ${DOCKER_HOST} | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"      # update /etc/hosts with docker machine ip     [[ -n $DOCKER_IP ]] && sudo /bin/bash -c "echo \"${DOCKER_IP}   docker.local\" >> /etc/hosts" }  update-docker-host 

This will automatically add or udpate the /etc/hosts line on my host OS when I start the Docker machine through my startup script.

Anyways, as I found out during my research, apart from editing the hosts file, you could also solve this problem by setting up a custom DNS server:

Also found several projects on Github which apparently aim to solve this problem, although I didn't try them:

  • https://github.com/jpetazzo/pipework
  • https://github.com/bnfinet/docker-dns
  • https://github.com/gliderlabs/resolvable
like image 159
Edu Wass Avatar answered Sep 16 '22 14:09

Edu Wass