CentOS 7 VPS: Docker Containers, Dnsmasq Not Accessible Outside via ZeroTier
Introduction
In this article, we will discuss how to expose Docker containers running on a CentOS 7 VPS, making them accessible outside the host using the ZeroTier network. Specifically, we will cover Dnsmasq not being accessible outside the host, despite having properly configured Docker networks and ZeroTier connections.
Prerequisites
- A CentOS 7 VPS with Docker installed
- ZeroTier network configured and running
- Multiple Docker containers running on the VPS
Background
ZeroTier is a smart Ethernet network that allows devices to connect, creating local area networks (LANs) across the Internet. Docker is a platform that enables developers to build, ship, and run applications inside containers. Dnsmasq is a lightweight DNS forwarder and DHCP server.
Problem
We have multiple services (containers) running on a CentOS 7 VPS, exposed via Docker networks. These services are accessible within the VPS using the loopback IP, 127.0.0.1. However, when attempting to access these services from outside the VPS, through the ZeroTier interface, the connections fail.
Symptoms
- Containers accessible via 127.0.0.1 on the VPS
- Containers not accessible from outside the VPS via ZeroTier
- No DNS resolution for container hostnames from outside the VPS
Solution
To expose Docker containers and make them accessible via the ZeroTier network, we must perform the following steps:
- Configure Docker networks
- Configure ZeroTier network
- Configure Dnsmasq for DNS resolution
Step 1: Configure Docker Networks
First, ensure the Docker containers are accessible within the VPS using Docker networks. Create a Docker network, then connect the containers to this network:
# Create Docker network
docker network create --subnet=172.18.0.0/16 mynetwork
# Connect container1 to mynetwork
docker network connect mynetwork container1
# Connect container2 to mynetwork
docker network connect mynetwork container2Step 2: Configure ZeroTier Network
Configure the ZeroTier network to route traffic through the CentOS 7 VPS's ZeroTier interface:
# Edit the ZeroTier configuration file
sudo nano /etc/zerotier/zerotier.conf
# Add the following line
# ...
# route /32
route 172.18.0.0/16
# Save and close the file
# ...
# Restart the ZeroTier service
sudo systemctl restart zerotier-one Step 3: Configure Dnsmasq for DNS Resolution
Configure Dnsmasq to forward DNS queries for container hostnames:
# Edit the Dnsmasq configuration file
sudo nano /etc/dnsmasq.conf
# Add the following lines
# ...
# address=//
address=/container1/172.18.0.2
address=/container2/172.18.0.3
# Save and close the file
# ...
# Restart the Dnsmasq service
sudo systemctl restart dnsmasq By configuring Docker networks, the ZeroTier network, and Dnsmasq for DNS resolution, we have successfully exposed Docker containers to be accessible outside the CentOS 7 VPS via the ZeroTier network. This setup allows us to easily manage and access containers and their services from other devices connected to the same ZeroTier network.
References
- Docker Deep Dive, Nigel Poulton
- ZeroTier Documentation, ZeroTier
- Docker Networking Documentation, Docker