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=' >> /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' >> /etc/resolv.dnsmasq.conf
RUN echo 'nameserver' >> /etc/resolv.dnsmasq.conf
RUN echo 'address="/mydomain/"' >> /etc/dnsmasq.d/0hosts
RUN service dnsmasq start

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

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

What am I missing?


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

2 Answers 2


--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="

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


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= my/container cat /etc/resolv.conf


  • This doesn't work however — dnsmasq is down... Jul 12, 2014 at 19:30
  • 1
    You can also use the --dns option more then once. For example docker run --dns --dns my/container Oct 25, 2016 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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