How to configure custom DNS server with Docker?

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

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


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.