I am trying to understand the difference between
docker run when attempting to resolve the hostname of a host on the network at my work from inside a container. In the example below, why does
docker run work only when I specify my work’s DNS address whereas
docker-compose works without any explicit DNS info? In this example,
myhost is another PC on the network at my work.
FROM ubuntu:16.04 MAINTAINER Kyle M. Douglass <email@example.com> RUN apt-get update && apt-get install -y wget CMD wget myhost > index.html
version: "3" services: my_service: build: . image: kmdouglass/my_image:latest command: wget myhost > index.html
Client: Version: 18.09.0 API version: 1.39 Go version: go1.10.4 Git commit: 4d60db4 Built: Wed Nov 7 00:48:57 2018 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 18.09.0 API version: 1.39 (minimum version 1.12) Go version: go1.10.4 Git commit: 4d60db4 Built: Wed Nov 7 00:16:44 2018 OS/Arch: linux/amd64 Experimental: false
$docker network ls NETWORK ID NAME DRIVER SCOPE 4e829bd60a47 bridge bridge local bdf7a8489980 host host local 8d15410c6cc1 none null local
Build the container and use
docker-run to fetch the web page from the host. In this case, it fails because I do not pass the DNS address:
$ docker build -t kmdouglass/my_image:latest . $ docker run -t --rm kmdouglass/my_image --2018-12-06 08:00:23-- http://myhost/ Resolving myhost (myhost)... failed: Name or service not known. wget: unable to resolve host address 'myhost'
The same as before, but specify the DNS address at my work. In this experiment the hostname resolution succeeds:
$ docker run -t --rm --dns=x.x.x.x kmdouglass/my_image --2018-12-06 08:02:27-- http://myhost/ Resolving myhost (myhost)... y.y.y.y Connecting to myhost (myhost)|y.y.y.y|:80... connected. HTTP request sent, awaiting response... 302 Found Location: /graphs/ [following] --2018-12-06 08:02:27-- http://myhost/graphs/ Reusing existing connection to myhost:80. HTTP request sent, awaiting response... 200 OK [...]
Now, remove the image to have a clean working environment and run the container with
docker-compose. This should have the same result as Experiment 1 because I do not specify my local DNS in the
docker-compose.yml file. However, the hostname resolution succeeds without any problems:
$ docker image rm kmdouglass/my_image:latest $ docker-compose up Creating network "docker_question_default" with the default driver Creating docker_question_my_service_1 ... done Attaching to docker_question_my_service_1 my_service_1 | --2018-12-06 08:11:54-- http://myhost/ my_service_1 | Resolving myhost (myhost)... y.y.y.y my_service_1 | Connecting to myhost (myhost)|y.y.y.y|:80... connected. my_service_1 | HTTP request sent, awaiting response... 302 Found my_service_1 | Location: /graphs/ [following] my_service_1 | --2018-12-06 08:11:54-- http://myhost/graphs/ my_service_1 | Reusing existing connection to myhost:80. my_service_1 | HTTP request sent, awaiting response... 200 OK
Thanks for the help!