What to do:
- Install dnsmasq for resolving hostnames
- Configure dnsmasq to resolve
.dockerrequests to localhost - Configure macOS to send
.dockerrequests 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 dnsmasqYou 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'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.
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-proxyThen 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
Creating config files
echo "address=/.docker/127.0.0.1\naddress=/.docker/::1\n" >> $(brew --prefix)/etc/dnsmasq.conf