docker / cli

The Docker CLI
Apache License 2.0
4.93k stars 1.93k forks source link

Feature Request: run --dns should also accept container names #385

Open shred opened 7 years ago

shred commented 7 years ago

Currently, the run --dns option only accepts an IP address of the DNS server to be used.

However, it is not possible to run a DNS server in a named docker container, and then use that DNS server in another docker container by its name. One would have to start the DNS server container, evaluate its IP address, and pass it to the other container's --dns option, in order to connect them. If the DNS server container's IP address changes, the other container would lose the connection.

It would be helpful if the --dns option also accepts the name of a docker container, and would use that container's IP for /etc/resolv.conf, all Docker magic included.

A simplified example:

docker run --name my-dns my/bind
docker run --dns my-dns my/tool    # does not work, must be an IP address
thaJeztah commented 7 years ago

I don't think this would be possible. The use-case you're describing probably only works if you run your containers in the default ("bridge") network. Any container started with either a custom bridge network (or overlay networks);

ping @sanimej any thoughts?

sanimej commented 7 years ago

@shred To achieve what you want you can create a custom bridge network with --subnet option and then you can create the DNS container with an IP specified from that subnet. If the container restarts it should get the same IP. If you are using swarm mode, you can also create a service for the DNS container and use its VIP as the --dns for other services.

shred commented 7 years ago

What I am currently doing as a workaround is, I start the my/tool container with a link to my-dns, and inside the my/tool container I run this command line:

echo "nameserver $(grep 'my-dns' /etc/hosts | cut -f1)" > /etc/resolv.conf

It is not elegant, but it works fine in bridge network mode. The disadvantage is that /etc/resolv.conf is managed by Docker, so I should better keep my fingers from it.

My thoughts are that Docker knows the IP of my-dns, and it manages the /etc/resolv.conf, so basically it should have everything to make a --dns my-dns work.

mefernandez commented 5 years ago

Hi! I documented a scenario where this feature would be very helpful. https://gitlab.com/onDevio/docker-dnsmasq-nginx-proxypass

In short, an nginx container is configured with a dns ip from a dnsmasq container solely to resolve unknown hosts to some fake IP like 1.2.3.4:

address=/#/1.2.3.4

@sanimej Would it be possible to achieve this configuring Docker Embedded DNS at 127.0.0.11?

Veitor commented 5 years ago

I have a solution, but is only available for nginx container see: https://github.com/docker/compose/issues/6180#issuecomment-484012983

djeikyb commented 5 years ago

i'd like this so i can run a simple dns container for service discovery. i'm running several services locally with docker compose. in prod, they're using dns srv records for discovery, and i'd like to do the same locally inside the compose network. it's easy to add a dnsmasq container to the compose file. but how to get the other services to use it? i hoped to just put the name of the dns container (or rather, compose service name) in each service's definition.

samrocketman commented 5 years ago

@djeikyb here's how I do it in docker-compose https://github.com/docker/compose/issues/6180#issuecomment-482598013