Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Last active August 30, 2024 11:58
Show Gist options
  • Save tobiashm/b43d24a5204d8bb623a7edcec1aeea6e to your computer and use it in GitHub Desktop.
Save tobiashm/b43d24a5204d8bb623a7edcec1aeea6e to your computer and use it in GitHub Desktop.
Setting up local domain for Docker

Example for setting up <whatever>.docker as a local domain (for Docker services)

What to do:

  • Install dnsmasq for resolving hostnames
  • Configure dnsmasq to resolve .docker requests to localhost
  • Configure macOS to send .docker requests to dnsmasq
brew install dnsmasq
# Create config files
sh -c 'echo "address=/.docker/127.0.0.1\naddress=/.docker/::1\n" > $(brew --prefix)/etc/dnsmasq.conf'
sudo mkdir -p /etc/resolver
sudo sh -c 'echo "nameserver 127.0.0.1\n" > /etc/resolver/docker'
# Start service as root, and register it to launch on boot
sudo brew services start dnsmasq

You should be able to resolve any .docker hostnames;

dig whatever.docker @localhost # should have ANSWER SECTION with mapping to 127.0.0.1
ping -c 3 something.docker # should receive response from 127.0.0.1
scutil --dns # should list resolver for 'domain: docker' with 'nameserver: 127.0.0.1'

Explanation

A custom DNS server (dnsmasq) is installed and running on localhost (127.0.0.1), and configured to resolve all hostnames ending with .docker to 127.0.0.1 (or ::1 for IPv6). Then macOS is configured to use the local DNS (dnsmasq) for all docker domain requests, pointing to 127.0.0.1 as the nameserver.

Bonus: Docker proxy

There's a clever proxy available for Docker, which will listen for created containers and auto-generate an nginx config with proxying to exposed ports. This works nicely for regular Docker containers, but when you use docker-compose a separate network is created for the services which the proxy can't access, so it'll not work without some extra setup. What I've opted for is creating a named network for the proxy, and then mapping that into the docker-conmpose config using the override file.

First, we create the custom network, and start the proxy server connected to that network (see the documentation for remaining config):

docker network create proxy
docker run -d \
  --name proxy \
  --network proxy \
  -p 80:80 \
  -v /var/run/docker.sock:/tmp/docker.sock:ro \
  jwilder/nginx-proxy

Then assuming we have a docker-compose.yml with two services—app & other—we can create a docker-compose.override.yml which sets up the enviromnent variable for the proxy service, and connects to the proxy network. Notice: We'll have to also define a default network (here called default) to allow the services communicate between themself:

version: '3'
services:
  app:
    environment:
      VIRTUAL_HOST: app.docker
    networks:
      - default
      - nginx-proxy

  other:
    networks:
      - default

networks:
  default:
  nginx-proxy:
    external:
      name: proxy
@mtuchi
Copy link

mtuchi commented Aug 7, 2023

Creating config files

echo "address=/.docker/127.0.0.1\naddress=/.docker/::1\n" >> $(brew --prefix)/etc/dnsmasq.conf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment