Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker how to start container with defined nameservers in /etc/resolv.conf

Here is my Dockerfile

FROM javamachine_0.1.2
MAINTAINER Meiram
RUN /report/report.sh start
ENV LANG C.UTF-8 ENV LANGUAGE C.UTF-8 ENV LC_ALL C.UTF-8
RUN echo "nameserver 192.168.1.100" > /etc/resolv.conf
COPY resolv.conf /etc/resolv.conf
EXPOSE 9090

when creating container directive docker run --dns also do not change entries in /etc/resolv.conf

How to change entries in /etc/resolv.conf permanently?

like image 798
Meiram Chuzhenbayev Avatar asked Apr 28 '16 04:04

Meiram Chuzhenbayev


4 Answers

If you use docker-compose you can simple add your dns-server in docker-compose.yml

  my-app:
     build: my-app
     dns:
       - 10.20.20.1  # dns server 1
       - 10.21.21.2  # dns server 2
     dns_search: ibm-edv.ibmnet.int

see https://bitbucket.org/snippets/mountdiablo/9yKxG/docker-compose-reference-yaml-file-with

like image 147
Gerd Avatar answered Oct 16 '22 18:10

Gerd


if you use --dns you may need to remove those lines:

RUN echo "nameserver 192.168.1.100" > /etc/resolv.conf
COPY resolv.conf /etc/resolv.conf

also,

try to swap the lines too.. COPY command will override the previous one.

like image 28
harunyardimci Avatar answered Oct 16 '22 18:10

harunyardimci


I found the answer

When you need to create a container with default network settings (172.0.0.x)

The key --dns is working. But doesn't work with user-defined network --net some_name_of_your_network

So if you need to create container you can define hostnames in /etc/hosts

using this command

docker run --net my_net --ip 192.168.1.x --add-host webserver:192.168.1.100 --add-host mysqlserver:192.168.1.x -td someimage

But this solution is not acceptable for me. Because I need to edit /etc/resolv.conf

like image 2
Meiram Chuzhenbayev Avatar answered Oct 16 '22 20:10

Meiram Chuzhenbayev


See https://docs.docker.com/engine/userguide/networking/default_network/configure-dns/ for more details.

You'll want to add the following to your docker command line, instead of all the --add-host options (assuming that your hosts are resolvable through your dns)

--dns=192.168.1.100

Based on the fact that you're running multiple services, if your webserver and mysqlserver are also docker containers, you might want to configure the collection of containers via docker-compose. See https://docs.docker.com/compose/overview/ for more details.

like image 2
cjnygard Avatar answered Oct 16 '22 20:10

cjnygard