Setting up a Dockerized WireGuard Custom DNS Multi-Lab Environment
In this article, we will cover how to set up a WireGuard custom DNS multi-lab environment using Docker. This setup is ideal for users who want to create multiple labs with a user-defined bridge network on a single host. For the purpose of this article, we will assume that the host has an IP address of 192.168.1.3 and that we will be creating two labs with IP ranges of 10.0.1.0/24 and 10.0.2.0/24, respectively.
Prerequisites
Before we begin, make sure you have the following installed:
- Docker
- WireGuard
- A text editor
Setting up the Docker Network
The first step is to create a user-defined bridge network in Docker. This will allow the labs to communicate with each other while isolating them from the host network. To create the network, run the following command:
docker network create --subnet=10.0.0.0/16 mynet
This will create a network with the name mynet and a subnet of 10.0.0.0/16. We will use this network to connect our labs.
Setting up WireGuard
Next, we need to set up WireGuard on the host. This will allow the labs to communicate with each other and with the host. To set up WireGuard, run the following command:
wg-quick up wg0
This will create a WireGuard interface with the name wg0. We will use this interface to connect our labs.
Setting up the Labs
Now we can start setting up our labs. For each lab, we will create a Docker container and connect it to the network we created earlier. To create the first lab, run the following command:
docker run -d --name lab1 --network mynet --ip 10.0.1.2 myimage
This will create a container with the name lab1 and an IP address of 10.0.1.2. We will use this container as the first lab.
To create the second lab, run the following command:
docker run -d --name lab2 --network mynet --ip 10.0.2.2 myimage
This will create a container with the name lab2 and an IP address of 10.0.2.2. We will use this container as the second lab.
Setting up the Custom DNS
Now that our labs are set up, we can set up the custom DNS. This will allow the labs to resolve domain names without relying on the host's DNS. To set up the custom DNS, we will use the dnsmasq container. Run the following command to create the container:
docker run -d --name dns --network mynet --ip 10.0.0.2 -p 53:53/udp -v /etc/dnsmasq.d:/etc/dnsmasq.d mydnsimage
This will create a container with the name dns and an IP address of 10.0.0.2. The container will listen on port 53 for both TCP and UDP traffic. We will also mount a volume to provide the container with a configuration file.
Now we can configure the DNS. Create a file named dnsmasq.conf with the following contents:
# /etc/dnsmasq.d/dnsmasq.conf
domain:lab
dhcp-range=10.0.1.100,10.0.1.200,255.255.255.0,12h
dhcp-range=10.0.2.100,10.0.2.200,255.255.255.0,12h
This will configure the DNS to use the lab domain and provide DHCP services for both labs. The DHCP range for each lab is 10.0.1.100-200 and 10.0.2.100-200, respectively. The DHCP leases will expire after 12 hours.
Testing the Setup
Now that everything is set up, we can test the setup. From inside the first lab, try pinging the second lab:
docker exec -it lab1 ping lab2
You should see packets being sent and received. This indicates that the labs can communicate with each other.
Next, try resolving a domain name from inside the first lab:
docker exec -it lab1 nslookup example.com
You should see the IP address of the domain name being returned. This indicates that the custom DNS is working correctly.
In this article, we covered how to set up a WireGuard custom DNS multi-lab environment using Docker. We created a user-defined bridge network, set up WireGuard, created two labs, and set up a custom DNS. We also tested the setup to ensure that everything was working correctly.