How can I assign static ip and nameserver in /etc/resolv.conf to user-defined networks in containers in order to bind a dns name to it and use it for resolving between containers?
- What exactly do you need a “static ip” for? - aleksandr barakin
- to bind a dns name to it and be used for resolving between containers - Meiram Chuzhenbayev
- Containers do not work that way (more precisely, it is quite possible, but this is the wrong approach). You need SkyDNS, Consul or equivalent. - etki
- It is enough for me that Alexander Barakin responded. Especially docker network - the name is what I was looking for. - Meiram Chuzhenbayev
1 answer
for communication between containers you can use:
- internal docker network
- “Links” between containers
- since version 1.9 - docker networks
internal network
least flexible solution. each time you restart / re-create, the container may receive a different internal address; accordingly, each time you may need to manually adjust the connection of names and ip-addresses.
docker links
when creating ( create ) or launching ( run ) a container, you can specify the name of another container and the name under which it will be available in the new container:
$ docker run --link другой_контейнер:имя ... thanks to this, a line similar to the following will be added to the /etc/hosts created container:
ip-адрес-другого-контейнера имя that will allow to refer to the linked container by the given name.
docker networks
the most flexible solution, but only available from version 1.9.
First, using the network create command, you need to describe the network:
$ docker network create имя-сети and then, when creating / launching containers, specify that they should be “connected” to this network under the specified name. example:
$ docker run --net=имя-сети --name=имя-контейнера ... all containers connected to the same network will “see” each other by their assigned names.
dns
The ip-addresses of nameservers can be set for the container when it is created / launched using the --dns option:
$ docker run --dns ip-адрес ...