jtreminio / dashtainer

MIT License
50 stars 3 forks source link

DNS with traefik not working? #3

Open ejunker opened 6 years ago

ejunker commented 6 years ago

I am running Docker for Mac 18.03.0-ce-mac60. The instructions say to access traefik at http://docker.localhost:8080 but I have no idea how that is supposed to work unless I add an entry to my /etc/hosts file for docker.localhost. I can access the traefik dashboard at http://localhost:8080/dashboard/#/ and I see entries for the services.

It says I should be able to access my project at http://awesome.localhost but that does not work. How is it supposed to resolve that in DNS? Do I need to use dnsmasq or something like that?

❯ curl -H "Host: awesome.localhost" http://awesome.localhost
curl: (6) Could not resolve host: awesome.localhost

As expected it cannot resolve awesome.localhost Just wondering how this is supposed to work.

jtreminio commented 6 years ago

Dashtainer uses Traefik. You can test out the magic here: https://docs.traefik.io/#the-trfik-quickstart-using-docker

Let me know if that works. If it does not, something else is going on with your system.

ejunker commented 6 years ago

I tried the traefik quickstart and everything worked. In the quickstart they run commands like: curl -H Host:whoami.docker.localhost http://127.0.0.1 which work because it isn't depending on any DNS resolution since it is using 127.0.0.1.

When I try to use dashtainer there is no way for my local system to resolve awesome.localhost in DNS. That is the part that I don't understand how it is supposed to work. I need something to resolve *.localhost to localhost. I see many people mention using dnsmasq to do that.

I've been experimenting with dnsmasq and added the following to my docker-compose.yml

  dns:
    image: gists/dnsmasq
    ports:
      - "53:53/tcp"
      - "53:53/udp"
    volumes:
      - ./dnsmasq:/etc/dnsmasq.d
    restart: always

With the contents of dnsmasq/dnsmasq.conf as:

address=/.localhost/127.0.0.1

Ok, running the following commands will get macOS to use the local dns server for *.localhost:

sudo mkdir -v /etc/resolver
sudo bash -c 'echo "nameserver 127.0.0.1" > /etc/resolver/localhost'

Can then run scutil --dns to verify that it shows that dns lookups for *.localhost will use the local nameserver

resolver #9
  domain   : localhost
  nameserver[0] : 127.0.0.1
  flags    : Request A records, Request AAAA records
  reach    : 0x00030002 (Reachable,Local Address,Directly Reachable Address)
jtreminio commented 6 years ago

What MacOS version are you on? I remember traefik auto dns not working for MacOS < 10.13

ejunker commented 6 years ago

I'm on macOS 10.13.3

I don't think there is such a thing as "auto dns" with traefik. Traefik is a proxy that can understand HTTP headers like Host: awesome.localhost to route to the correct container but I don't think it has anything to do with DNS resolution.

Just curious, what do you get when you run the following locally on your mac assuming awesome.localhost is the hostname for your project

❯ host awesome.localhost
jtreminio commented 6 years ago

Here's on Linux:

[03:00 PM]-[jtreminio@desktop]-[~/docker/traefik] 
$ docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
c1c5fae74436        bridge              bridge              local
c7435738a06d        host                host                local
d2a350a35039        none                null                local

[03:00 PM]-[jtreminio@desktop]-[~/docker/traefik] 
$ curl dashtainer.localhost
curl: (7) Failed to connect to dashtainer.localhost port 80: Connection refused

[03:00 PM]-[jtreminio@desktop]-[~/docker/traefik] 
$ docker-compose up -d --build
Creating network "traefik_webgateway" with driver "bridge"
Creating traefik_proxy_1 ... done

[03:00 PM]-[jtreminio@desktop]-[~/docker/traefik] 
$ curl dashtainer.localhost
404 page not found

[03:00 PM]-[jtreminio@desktop]-[~/docker/traefik] 
$ cd ../dashtainer/

[03:00 PM]-[jtreminio@desktop]-[~/docker/dashtainer] 
$ docker-compose up -d --build
Creating network "dashtainer_william-buckland" with the default driver
Building nginx
Step 1/16 : FROM ubuntu:16.04
 ---> f975c5035748
Step 2/16 : ENV DEBIAN_FRONTEND noninteractive
 ---> Using cache
 ---> 3fec3cb931e1
Step 3/16 : RUN apt-get update && apt-get install -y apt-utils
 ---> Using cache
 ---> aa6b86935986
Step 4/16 : RUN apt-get install -y     curl     locales     software-properties-common     python-software-properties
 ---> Using cache
 ---> 83e926908e62
Step 5/16 : RUN locale-gen en_US.UTF-8
 ---> Using cache
 ---> 5f05a2138974
Step 6/16 : ENV LANG C.UTF-8
 ---> Using cache
 ---> a69e58e6947a
Step 7/16 : ENV LANGUAGE C.UTF-8
 ---> Using cache
 ---> 94ddead7973c
Step 8/16 : ENV LC_ALL C.UTF-8
 ---> Using cache
 ---> 62f4b7ac0dd5
Step 9/16 : RUN /usr/sbin/update-locale
 ---> Using cache
 ---> d9306b4beb02
Step 10/16 : RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys         14AA40EC0831756756D7F66C4F4EA0AAE5267A6C     && apt-get update
 ---> Using cache
 ---> ad7b78bd212a
Step 11/16 : ENV NGINX_PREFIX /etc/nginx
 ---> Using cache
 ---> 959294bcdc0d
Step 12/16 : ARG SYSTEM_PACKAGES
 ---> Using cache
 ---> ffc011a42f21
Step 13/16 : WORKDIR $NGINX_PREFIX
 ---> Using cache
 ---> a2a0a27ad3c9
Step 14/16 : RUN add-apt-repository ppa:ondrej/nginx     && apt-get update     && apt-get install --no-install-recommends --no-install-suggests -y         nginx ${SYSTEM_PACKAGES}     && apt-get -y --purge autoremove     && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*     && ln -sf /dev/stdout /var/log/nginx/access.log     && ln -sf /dev/stderr /var/log/nginx/error.log
 ---> Using cache
 ---> 5f5c79d7ce8c
Step 15/16 : EXPOSE 80
 ---> Using cache
 ---> 6225dc607e0f
Step 16/16 : CMD ["nginx", "-g", "daemon off;"]
 ---> Using cache
 ---> b95d3b391215
Successfully built b95d3b391215
Successfully tagged dashtainer_nginx:latest
Building php
Step 1/26 : FROM ubuntu:16.04
 ---> f975c5035748
Step 2/26 : ENV DEBIAN_FRONTEND noninteractive
 ---> Using cache
 ---> 3fec3cb931e1
Step 3/26 : RUN apt-get update && apt-get install -y apt-utils
 ---> Using cache
 ---> aa6b86935986
Step 4/26 : RUN apt-get install -y     curl     locales     software-properties-common     python-software-properties
 ---> Using cache
 ---> 83e926908e62
Step 5/26 : RUN locale-gen en_US.UTF-8
 ---> Using cache
 ---> 5f05a2138974
Step 6/26 : ENV LANG C.UTF-8
 ---> Using cache
 ---> a69e58e6947a
Step 7/26 : ENV LANGUAGE C.UTF-8
 ---> Using cache
 ---> 94ddead7973c
Step 8/26 : ENV LC_ALL C.UTF-8
 ---> Using cache
 ---> 62f4b7ac0dd5
Step 9/26 : RUN /usr/sbin/update-locale
 ---> Using cache
 ---> d9306b4beb02
Step 10/26 : RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys         14AA40EC0831756756D7F66C4F4EA0AAE5267A6C     && apt-get update
 ---> Using cache
 ---> ad7b78bd212a
Step 11/26 : ENV PHP_DIR /etc/php/7.2
 ---> Using cache
 ---> fcfcc7ad4d92
Step 12/26 : ENV PHP_FPM php7.2-fpm
 ---> Using cache
 ---> b1511f885569
Step 13/26 : ARG SYSTEM_PACKAGES
 ---> Using cache
 ---> 47a9fc72af83
Step 14/26 : ARG PHP_PACKAGES
 ---> Using cache
 ---> cc361e026ec1
Step 15/26 : WORKDIR $PHP_DIR
 ---> Using cache
 ---> ae7748bd7fac
Step 16/26 : RUN add-apt-repository ppa:ondrej/php     && apt-get update     && apt-get install --no-install-recommends --no-install-suggests -y         ${PHP_FPM} ${SYSTEM_PACKAGES} ${PHP_PACKAGES}     && apt-get -y --purge autoremove     && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
 ---> Using cache
 ---> 5bb9f79e2306
Step 17/26 : ARG PEAR_PACKAGES
 ---> Using cache
 ---> 9ee47c841524
Step 18/26 : RUN if [ ! -z "$PEAR_PACKAGES" ]; then     printf "\n" | pear install -o -f ${PEAR_PACKAGES} ;fi
 ---> Using cache
 ---> f92060598078
Step 19/26 : ARG PECL_PACKAGES
 ---> Using cache
 ---> c8bd038dd6e2
Step 20/26 : RUN if [ ! -z "$PECL_PACKAGES" ]; then     pecl channel-update pecl.php.net     && printf "\n" | pecl install -o -f ${PECL_PACKAGES} ;fi
 ---> Using cache
 ---> 206d69c8615c
Step 21/26 : ARG COMPOSER_INSTALL=0
 ---> Using cache
 ---> a448e4dea0c2
Step 22/26 : RUN if [ $COMPOSER_INSTALL -ne 0 ]; then     curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin         --filename=composer     && mkdir /.composer && chmod 1777 /.composer ;fi
 ---> Using cache
 ---> e15bef93d894
Step 23/26 : ARG BLACKFIRE_INSTALL=0
 ---> Using cache
 ---> cf4da5524e17
Step 24/26 : RUN if [ $BLACKFIRE_INSTALL -ne 0 ]; then     version=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;")     && curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s         https://blackfire.io/api/v1/releases/probe/php/linux/amd64/$version     && tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp     && mv /tmp/blackfire-*.so $(php -r "echo ini_get('extension_dir');")/blackfire.so     && printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" >         ${PHP_DIR}/cli/conf.d/blackfire.ini     && printf "extension=blackfire.so\nblackfire.agent_socket=tcp://blackfire:8707\n" >         ${PHP_DIR}/fpm/conf.d/blackfire.ini ;fi
 ---> Using cache
 ---> 150917ec28f0
Step 25/26 : EXPOSE 9000
 ---> Using cache
 ---> ddc94f6dda24
Step 26/26 : CMD ["/usr/sbin/php-fpm7.2", "--nodaemonize", "--force-stderr", "--allow-to-run-as-root"]
 ---> Using cache
 ---> e63c9032be0c
Successfully built e63c9032be0c
Successfully tagged dashtainer_php:latest
Creating dashtainer_php_1     ... done
Creating dashtainer_adminer_1 ... done
Creating dashtainer_nginx_1   ... done
Creating dashtainer_mariadb_1 ... done

[03:00 PM]-[jtreminio@desktop]-[~/docker/dashtainer] 
$ curl dashtainer.localhost
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/build/app.css">
    <title>Dashtainer - Docker, Simplified</title>
</head>
<body>

[03:00 PM]-[jtreminio@desktop]-[~/docker/dashtainer] 
$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
jtreminio commented 6 years ago

On MacOS, auto-dns seems to only work on Chrome. Safari does not forward *.localhost to 127.0.0.1, but Chrome does.

On Linux this works in everything. I am unsure in Windows.

ejunker commented 6 years ago

Ah, I was using Firefox which doesn't support *.localhost automatically. You might want to update the instructions to mention that you have to use Chrome if you are on macOS. Feel free to close this issue now if you want.

jtreminio commented 6 years ago

No, you've pointed out a fairly big issue I had not even thought of testing!

What if you follow this: https://gist.github.com/eloypnd/5efc3b590e7c738630fdcf0c10b68072

Can you let me know if this works? If it does, I will def. add those instructions to the website.

Now to find a Windows equivalent.

jtreminio commented 6 years ago

Just tried the above steps on my MBP; works a treat, all *.localhost resolve to 127.0.0.1 in CLI and all other apps.

ejunker commented 6 years ago

I did get dnsmasq to work but I added an entry to docker-compose.yml to run dnsmasq in a container rather than installing it with homebrew. See my previous comment where I show how I did this.

jtreminio commented 6 years ago

So I'm waffling on using the dnsmasq container because Linux already points *.localhost to 127.0.0.1

Windows seems like it needs Acrylic, its dnsmasq equivalent.

So I can either suggest MacOS folks install the brew formula and set it up, and come up with equivalent instructions for Windows folks, or have a MacOS-only container service and figure something out for Windows.

ejunker commented 6 years ago

Another option would be to do what Hotel does and run a web proxy. See https://github.com/typicode/hotel/blob/master/docs/README.md

Here is the template for their PAC (proxy auto config) file: https://github.com/typicode/hotel/blob/master/src/daemon/views/proxy-pac.pug

simshaun commented 6 years ago

I'm on Windows and set up the ISC BIND DNS server so I could have wildcard routing on *.loc. Unfortunately, it's kinda difficult to figure out if you're like me and not familiar with setting up BIND. A proxy auto-config file seems like it'd be a whole lot easier.

If anyone is interested in the config I'm using for BIND, I've added it below. Note that you should change loc to localhost. I'm just using domains like foo.loc and bar.loc personally (and have updated configs that Dashtainer generates for me to match). After you get BIND set up and running, you have to change your network adapter's DNS server to 127.0.0.1.

named.conf (8.8.8.8 and 8.8.4.4 are Google's DNS servers)

options {        
  directory "C:\Program Files\ISC BIND 9\etc";
  forwarders { 8.8.8.8; 8.8.4.4; };
  allow-transfer { none; };
};

zone "loc" IN {
  type master;
  file "loc.zone";
  allow-update { none; };
};

loc.zone

$TTL    60
$ORIGIN loc.
@                     1D IN SOA         localhost. root.localhost. (45 3H 15M 1W 1D)
                      1D IN NS          localhost.
                      1D IN A           127.0.0.1
*.loc.                60 IN A           127.0.0.1