Skip to content

Instantly share code, notes, and snippets.

@NAR8789
Last active September 26, 2024 11:34
Show Gist options
  • Save NAR8789/92da076d0c35b434107fb4f4f198fd12 to your computer and use it in GitHub Desktop.
Save NAR8789/92da076d0c35b434107fb4f4f198fd12 to your computer and use it in GitHub Desktop.
wildcard dns for docker-compose using dnsmasq
# explicitly define host-ip mappings
address=/myapp.local/172.16.1.2
# dnsmasq entries are always wildcard entries, so this maps both myapp.local and *.myapp.local
# (yes, it's fine for this to be your entire dnsmasq config. the defaults are pretty sensible)
version: '2' # v2 required for ip_range config support. v3 won't work.
networks:
default:
ipam:
config:
- subnet: 172.16.0.0/23 # In order to specify static IPs, we must explicitly declare subnet.
ip_range: 172.16.0.0/24 # Range for dynamic IPs. We'll make sure to assign static IPs outside this range.
# docs: https://docs.docker.com/compose/compose-file/compose-file-v2/#ipam
# As of this writing, `ip_range` is not supported in v3.
services:
dnsmasq:
image: strm/dnsmasq
volumes:
- ./dnsmasq.conf:/etc/dnsmasq.conf
ports:
- 53:53/udp
cap_add:
- NET_ADMIN
# dnsmasq container config above is taken verbatim from https://hub.docker.com/r/strm/dnsmasq
networks:
default:
ipv4_address: 172.16.1.1 # Static IP here makes it possible to point other containers' dns here.
myapp:
# ...
# your app config here
# ...
networks:
default:
ipv4_address: 172.16.1.2 # Static IP here makes dnsmasq config easy to write.
appclient:
# ...
# more container config
# ...
dns:
- 172.16.1.1 # dnsmasq container
# `appclient` should now be able to access `myapp` as myapp.local or foo.myapp.local
@boomshadow
Copy link

Thanks for this! I was able to easily adapt this to support docker compose v3.
Remove ip_range and then simply pick static IP's from the rear of the address pool.

In your example, the subnet is 172.16.0.0/23. You can safely choose static IP's of:

  • 172.16.1.254
  • 172.16.1.253

DHCP will start allocation from the top of the pool. You will likely not need 512 DHCP addresses, so you can use the last 2. If you do think there would be collision because you are running so many containers, simply make the subnet larger.

@ppatidar-conga
Copy link

ppatidar-conga commented Jul 26, 2022

@boomshadow can you please share entire sample here?

@boomshadow
Copy link

@ppatidar-conga Sure. Here is what we use at my job. Everyone is able to have working wildcard DNS with this: https://gist.github.com/boomshadow/20677ef02f110e448ee058ae6149af3a

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