59

Is there a way I can reach my docker containers using names instead of ip addresses?

I've heard of pipework and I've seen some dns and hostname type options for docker, but I still am unable to piece everything together.

Thank you for your time.

I'm not sure if this is helpful, but this is what I've done so far:

  • installed docker container host using docker-machine and the vmwarevsphere driver
  • started up all the services with docker-compose
  • I can reach all of the services from any other machine on the network using IP and port

I've added a DNS alias entry to my private network DNS server and it matches the machine name that's used by docker-machine. But the machine always picks up a different IP address when it boots and connects to the network.

I'm just lost as to where to tackle this:

  • network DNS server
  • docker-machine hostname
  • docker container hostname
  • probably some combination of all of them

I'm probably looking for something similar to this question:

How can let docker use my network router to assign dhcp ip to containers easily instead of pipework?

Any general direction will be awesome...thanks again!

Community
  • 1
  • 1
Zhao Li
  • 3,184
  • 5
  • 28
  • 43
  • take a look at [this answer](https://stackoverflow.com/a/45071126/2979435), you can user DPS to achive this, at same time you can take advantage of some others features – deFreitas Dec 28 '18 at 20:59

6 Answers6

40

Docker 1.10 has a built in DNS. If your containers are connected to the same user defined network (create a network docker network create my-network and run your container with --net my-network) they can reference each other using the container name. (Docs).

Cool!

One caveat if you are using Docker compose you know that it adds a prefix to your container names, i.e. <project name>_<service name>-#. This makes your container names somewhat more difficult to control, but it might be ok for your use case. You can override the docker compose naming functionality by manually setting the container name in your compose template, but then you wont be able to scale with compose.

beat
  • 1,687
  • 1
  • 19
  • 31
datacarl
  • 2,140
  • 22
  • 19
  • 1
    (Correction) Just saw the last part you wrote regarding scaling, now I see where you are coming from! (Original) You should be able to use `docker-compose.yml` and set `container_name: anything` within a service and it shouldn't prepend anything but correct me if I'm wrong I've been doing it frequently on a custom network. – JREAM Jul 26 '18 at 10:32
11

Create a new bridge network other than docker0, run your containers inside it and you can reference the containers inside that network by their names.

Docker daemon runs an embedded DNS server to provide automatic service discovery for containers connected to user-defined networks. Name resolution requests from the containers are handled first by the embedded DNS server.

Try this:

docker network create <network name>
docker run --net <network name> --name test busybox nc -l 0.0.0.0:7000
docker run --net <network name> busybox ping test

First, we create a new network. Then, we run a busybox container named test listening on port 7000 (just to keep it running). Finally, we ping the test container by its name and it should work.

matfax
  • 527
  • 8
  • 15
rogamba
  • 162
  • 2
  • 10
4

EDIT 2018-02-17: Docker may eventually remove the links key from docker-compose, therefore they suggest to use user-defined networks as stated here => https://docs.docker.com/compose/compose-file/#links


Assuming you want to reach the mysql container from the web container of your docker-compose.yml file, such as:

web:
  build: .
  links:
    - mysql

mysqlservice:
  image: mysql

You'll be pleased to know that Docker Compose already adds a mysqlservice domain name (in the web container /etc/hosts) which point to the mysql container.

Instead of looking for the mysql container IP address, you can just use the mysqlservice domain name.

If you want to add custom domain names, it's also possible with the extra_hosts parameter.

Timothy Miller
  • 2,265
  • 1
  • 26
  • 33
Oliboy50
  • 2,482
  • 3
  • 23
  • 33
3

If you want out of the box solution, you might want to check for example Kontena. It comes with network overlay technology from Weave and this technology is used to create virtual private LAN networks between services. Thanks to that every service/container can be reached by service_name.kontena.local.

ColinM
  • 11,946
  • 2
  • 38
  • 45
Lauri
  • 3,568
  • 3
  • 16
  • 17
  • 1
    Does this allow you to connect to a port exposed by the container *from the host* using `service_name.kontena.local`? – erickson Sep 06 '17 at 16:20
3

You might want to try out dnsdock. Looks straight forward and easy(!) to set up. Have a look at http://blog.brunopaz.net/easy-discover-your-docker-containers-with-dnsdock/ and https://github.com/tonistiigi/dnsdock .

Andreas Steffan
  • 5,731
  • 2
  • 21
  • 25
2

I changed the --net parameter with --network parameter and it runs as expected:

docker network create <network name>
docker run --network <network name> --name <container name> <other container options>
docker run --network <network name> --name <container name> <other container options>
Sicco
  • 5,759
  • 3
  • 41
  • 58
Oscar Llop
  • 181
  • 1
  • 4