8

I need to add a few lines to /etc/hosts for my web-app to work inside a Docker container.

Docker's /etc/hosts is read-only.

I'm trying to use dnsmasq:

FROM ubuntu:14.04
# ...
RUN apt-get install -y -q dnsmasq
RUN echo 'listen-address=127.0.0.1' >> /etc/dnsmasq.conf
RUN echo 'resolv-file=/etc/resolv.dnsmasq.conf' >> /etc/dnsmasq.conf
RUN echo 'conf-dir=/etc/dnsmasq.d' >> /etc/dnsmasq.conf
RUN echo 'user=root' >> /etc/dnsmasq.conf
RUN echo 'nameserver 8.8.8.8' >> /etc/resolv.dnsmasq.conf
RUN echo 'nameserver 8.8.4.4' >> /etc/resolv.dnsmasq.conf
RUN echo 'address="/mydomain/127.0.6.1"' >> /etc/dnsmasq.d/0hosts
RUN service dnsmasq start

However, I can't get Docker to use my DNS server:

$ docker --dns=127.0.0.1 run my/container cat /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

What am I missing?

Configuration:

  • OS X 10.9.4
  • Docker version 1.1.0, build 79812e3
  • boot2docker Client version: v1.1.0, Git commit: 7e20d36
10

--dns works for docker run but not for docker build. So better solution is to adjust the settings globally with

DOCKER_OPTS="--dns=my-private-dns-server-ip --dns=8.8.8.8"

On Ubuntu I put this line into /etc/default/docker

| improve this answer | |
6

What I missing is that I messed up the order of options and arguments. If I specify --dns after run, everything works fine.

$ docker run --dns=127.0.0.1 my/container cat /etc/resolv.conf
nameserver 127.0.0.1

Doh.

| improve this answer | |
  • This doesn't work however — dnsmasq is down... – Alexander Gladysh Jul 12 '14 at 19:30
  • You can also use the --dns option more then once. For example docker run --dns 8.8.8.8 --dns 127.0.0.1 my/container – Bruno Bieri Oct 25 '16 at 8:36

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.