<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Docker on Tech Philosopher</title><link>https://blog.bktus.com/en/tags/docker/</link><description>Recent content in Docker on Tech Philosopher</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sat, 03 Jan 2026 00:36:04 +0100</lastBuildDate><atom:link href="https://blog.bktus.com/en/tags/docker/index.xml" rel="self" type="application/rss+xml"/><item><title>Long-Term Personal VPS Self-Hosting: Cleanliness and Security</title><link>https://blog.bktus.com/en/archives/vgjftw/</link><pubDate>Sat, 03 Jan 2026 00:36:04 +0100</pubDate><guid>https://blog.bktus.com/en/archives/vgjftw/</guid><description>&lt;p>I’ve been seriously tinkering with personal self-hosting for about five or six years now. Strictly speaking, the time I started managing my own VPS goes back even further. The original motivation was actually pretty simple: there were some services I used every day, like a password manager, music playback, note syncing, a personal website, code repositories and so on. So why not just deploy them myself? That way the data stays in my own hands, and I’m not pushed around by all kinds of platform subscriptions and price increases.&lt;/p></description><content:encoded><![CDATA[<p>I’ve been seriously tinkering with personal self-hosting for about five or six years now. Strictly speaking, the time I started managing my own VPS goes back even further. The original motivation was actually pretty simple: there were some services I used every day, like a password manager, music playback, note syncing, a personal website, code repositories and so on. So why not just deploy them myself? That way the data stays in my own hands, and I’m not pushed around by all kinds of platform subscriptions and price increases.</p>
<p>Looking back on these years, to be honest, self-hosting isn’t hassle-free. I have to maintain the system, back up data, keep an eye on security, and once in a while put out fires. But what I get back is also very clear:</p>
<ul>
<li>My data is fully under my own control</li>
<li>I’m no longer pushed around by subscription prices</li>
<li>I understand systems, networking and security a lot better</li>
</ul>
<p>Now I run a dozen or so services on one server, and the cost is just the price of that one server. After canceling quite a few third-party subscriptions over the past two years, the overall cost may actually be lower when I do the math. For me, this “complex but under control” state feels more reassuring than depending on a pile of external services. Of course, this isn’t for everyone. I feel it suits people who have some technical background, enjoy tinkering, and are willing to put in time for stability and control. When all three come together in one person, this kind of setup fits.</p>
<p>Even now, I don’t think I’ve become any kind of “expert,” but my feelings about two things have grown stronger and stronger: <strong>security</strong> and <strong>how clean the server environment is</strong>. These are also the two main threads of this post. These two questions pretty much decide whether the services on your server can keep running stably over the long term. They also decide how much work it is when you need to migrate, for example when you switch to a cheaper or better VPS provider. (I’ve switched several times myself, and only settled down once I really couldn’t face moving again.)</p>
<h2 id="the-rough-early-days">The Rough Early Days</h2>
<p>In the beginning, my approach was very basic. I installed services directly on the host, and then installed Nginx or Apache2 (httpd). For example, I used Nginx as a reverse proxy. The good thing about web servers like Nginx is that the config files can be split up, one per site, and services are told apart by domain name instead of port number, so everything looks neat and tidy. Back then I thought this setup was pretty elegant.</p>
<p>But as the number of services slowly grew, problems started to show up one by one. Some services needed specific versions of dependencies, and it was hard for me to install different versions of various libraries or databases in the same environment. Some left all kinds of temporary files in the system. Config files were scattered under <code>/etc</code>, data was in <code>/srv</code>, and logs were in <code>/var</code>. At first you could still remember “this belongs to that service,” but after a while it all got mixed up.</p>
<p>During this time, when I wanted to remove a service I no longer used, I wasn’t even sure I could remove it cleanly. This was especially true for services that come with a “one-click install script.” Installing them feels great at the time, but when you want to get rid of them, you find you have no idea what changes the script made back then, or how to undo them. It’s hard to know whether there are leftover configs or background processes, and you don’t even know whether deleting the wrong thing will break other services. This uncertainty is a risk in itself. For someone with a bit of a cleanliness obsession like me, it was deeply uncomfortable every time I thought about it.</p>
<h2 id="the-containerization-phase">The Containerization Phase</h2>
<p>Later I moved almost all of my services into Docker. And these days I lean more and more toward containerizing every service or tool I use before deploying it. For services I really want to use but that don’t offer a standard containerized deployment, I just package them into containers myself and make my own containerized solution.</p>
<p>What really changed the experience wasn’t Docker itself, but <strong>Docker Compose</strong>. Containers basically shouldn’t be treated as “things to maintain long-term.” They can be deleted and rebuilt at any time, and you need to be aware of that. Early on, I thought Docker Compose files were a hassle. Why not just create a container with a command and let it run for a long time? Later, when I needed to update it, or when the service depended on several other containers underneath, the problems slowly showed up. If I had written the deployment down in a config file from the start, I could just run that file every time and reproduce the original deployment perfectly. I wouldn’t need to set up a bridge network, a hostname or a restart policy by hand. This saves a lot of time and energy, and the chance of making mistakes during configuration also goes way down.</p>
<p>Looking back now, I can say there are only three things that really need long-term maintenance:</p>
<ul>
<li>Service configuration (including version, network, storage and so on)</li>
<li>Data (mainly volumes and other mapped directories)</li>
<li>Deployment configuration (the various Docker Compose YAML files)</li>
</ul>
<p>Whether I reinstall the system or switch to another machine, as long as these three are still there, the services can be cloned perfectly to the new place. And the host itself finally became “clean.” Its directories are no longer full of things you installed years ago and have since forgotten the purpose of. So at this stage, I created a separate Git repository to collect and manage the Docker Compose files for all the services I use. After a year with it, I feel this approach holds up in all kinds of situations.</p>
<h2 id="one-service-one-database">One Service, One Database</h2>
<p>This one is worth mentioning. For a long time, all of my services shared one database instance. Logically it looks very reasonable: it saves resources, the structure is simple, and management is centralized. But after maintaining it for a long time, you’ll find this is actually a <strong>very unfriendly design</strong>.</p>
<p>Every new service means another set of users, permissions and passwords. If you want to migrate a service, you have to carefully pull its database out. Once the database version is upgraded, every service has to share the risk. For people who aren’t familiar with a certain database, managing permissions and users is a headache. You have to use the command line or some database management tool to carefully keep each service’s users apart and keep their passwords safe.</p>
<p>Later I just did the opposite: <strong>each service gets its own database instance.</strong> That means each Docker Compose file has its own database service. After watching this for a long time, I found that for personal self-hosting, it’s not as “wasteful” as you might think. A database container usually only takes 100 or 200 MB of memory. On a VPS with 4 cores and 8 GB of RAM, or even 2 cores and 4 GB, or on a home server, that’s acceptable.</p>
<p>But what I get in return is very real. The services are fully isolated from each other, which improves security a lot, and I no longer have endless friction with permissions and compatibility. I can just pack up all the data and config in one go and take it with me, and each database only holds the data of its own service.</p>
<h2 id="the-principle-of-minimal-exposure">The Principle of Minimal Exposure</h2>
<p>Early on, I exposed almost all of my services directly to the public internet, and protected them with a reverse proxy plus login:</p>
<ul>
<li>One Nginx Proxy Manager</li>
<li>A public domain for every service</li>
<li>Username/password protection at the application layer</li>
</ul>
<p>Back then I also thought: “Every service has a username and password, so it should be fine, right?” Only later did I slowly realize that this was just fooling myself. As long as <strong>one service</strong> has a brute-force weakness, an authentication bypass or an RCE (remote code execution) vulnerability, an attacker may get access to the container or even the host. What first happened to me was with a GitLab instance I ran without a container. GitLab once had a serious remote code execution vulnerability, and I didn’t find out about it in time. Then one day I found that a trojan had been planted on my server, and I only knew because my cloud provider told me. If I had kept photos, videos or backups on that server back then, the result would have been a disaster.</p>
<p>Also, the security maturity of different services varies a lot, and their tech stacks vary widely. Some projects are still young, and their security thinking is far from mature. And I can’t watch every security advisory every day. Once one of these services has a vulnerability, or the frameworks or dependencies they use do, like the recent security vulnerabilities around the React stack, that service or those services can blow up in a short time. If containerization and isolation aren’t done well at that point, the attacker may get more than that one service. Quite possibly they get the whole machine. And then come the hidden trojan scripts or crypto-mining scripts.</p>
<p>This is also a core principle I came to later: if a service doesn’t have to be open to the public internet, don’t open it. Blogs and static pages don’t lose much if they get attacked, but things like notes, password managers and admin panels are a disaster if something goes wrong. So you need to take a good look at which services you’re exposing right now. From my own experience, the ones I feel safest about are static websites, or pages that are only generated from parameters with no database behind them. But there are a lot of services I want to use, so how do I expose some and not others? My current practice is to expose only the services that really need it to the public internet, like a static blog and a very small number of public services. Git repositories are kept private as much as possible, or only expose a read-only static view (through gitweb or cgit). Dynamic services, admin panels, notes, password managers and so on are never exposed directly to the public internet.</p>
<h3 id="deploying-a-private-vpn">Deploying a Private VPN</h3>
<p>Later I found that the key to solving this problem is a VPN. I deployed a private VPN on the server (WireGuard, for example) and put all private services into <strong>a network that can only be reached from the internal network</strong>. Only after I connect to the VPN and get a virtual internal IP can I reach these services, starting from that internal IP. The overall idea is:</p>
<ul>
<li>The VPN provides a virtual private subnet (like <code>10.8.x.x</code>)</li>
<li>Docker internal services use another private subnet (like <code>172.x.x.x</code>)</li>
<li>The two subnets can reach each other through routing</li>
<li>The public internet can’t reach these services directly</li>
</ul>
<p>What this step means is that I no longer need to design a separate “defense plan” for each service, because they’re not on the attack surface at all. Add a reverse proxy and DNS that only work inside the internal network, and the experience is actually better than on the public internet: clear domain names, stable services, and real peace of mind.</p>
<h3 id="internal-reverse-proxy-and-dns-resolution">Internal Reverse Proxy and DNS Resolution</h3>
<p>To improve the experience further, I deployed an internal Nginx container inside the internal network (under 172.24.0.0/16), completely isolated from the Nginx instance that serves the public (for example, 172.20.0.0/16). This internal Nginx instance is bound to a fixed private IP (for example, 172.24.0.2), and it only serves traffic that comes in through the VPN network.</p>
<pre class="mermaid">
  flowchart LR
  %% Clients
  PUB[&#34;Public Internet User&#34;] --&gt;|&#34;HTTPS&#34;| PNGX[&#34;Public Nginx (*.example.com)&#34;]
  PNGX --&gt; PBLOG[&#34;Public Service (e.g., blog/static)&#34;]

  %% VPN access
  DEV[&#34;Your Device\n(WireGuard Client)&#34;] --&gt;|&#34;VPN Tunnel&#34;| WG[&#34;WireGuard Server (10.8.0.0/24)&#34;]

  %% Internal name + reverse proxy (VPN-only)
  WG --&gt;|&#34;DNS&#34;| DNS[&#34;Internal DNS (172.24.0.4)&#34;]
  WG --&gt;|&#34;HTTPS&#34;| INGX[&#34;Internal Nginx (172.24.0.2)&#34;]

  %% Private services
  subgraph DOCKER[&#34;Docker Private Networks&#34;]
    INGX --&gt; VAULT[&#34;Vaultwarden&#34;]
    INGX --&gt; NOTES[&#34;Notes / Joplin&#34;]
    INGX --&gt; GIT[&#34;Git Service&#34;]
  end
</pre>
<p>Then I added a mandatory internal DNS service. All traffic coming in through the VPN can only use this DNS for name resolution. There are many mature options for this in practice, like AdGuard Home, which can also take care of ad blocking and DNS hijacking at the same time. Besides resolving the normal network traffic that passes through, this DNS service also specifically handles all DNS resolution under *.vpn.example.com, for example:</p>
<ul>
<li><code>git.vpn.example.com</code></li>
<li><code>notes.vpn.example.com</code></li>
<li><code>vault.vpn.example.com</code></li>
</ul>
<p>All of these resolve to the IP the internal Nginx listens on, 172.24.0.2. So when the VPN is connected, I can just type the domain name into the browser and get straight to the internal service. All the traffic stays inside the internal network. It’s best if this domain is registered under your own name. Then, in your external DNS settings, set *.vpn.example.com to NXDOMAIN or your VPS’s IP address. That way, if you accidentally try to reach an internal service without going through the VPN, the traffic won’t get forwarded somewhere else by mistake.</p>
<h3 id="isolating-database-containers">Isolating Database Containers</h3>
<p>A database shouldn’t be on the same Docker network as unrelated services. So I did one more thing: each service and its database get their own isolated bridge. On this bridge, only these two containers can talk to each other, and the bridge isn’t connected to any external network. In other words, even if a service gets compromised, the only thing it can reach is its own database, not every database on the server. Like this:</p>
<pre tabindex="0"><code>services:
  db:
    image: postgres:16-alpine
    ...
    networks:
      - iso
    restart: unless-stopped

  joplin:
    image: git.stdv.de/saturneric/joplin:latest
    ...
    depends_on:
      - db
    networks:
      - iso
      - vpn
    restart: unless-stopped
networks:
  iso:
    internal: true
  vpn:
    external: true
</code></pre><p>You can see that db only talks one-to-one with joplin on the iso network, and this network blocks direct access from inside it to the outside network (internal: true). So if another service gets compromised, it generally can’t reach my joplin database over the network. But this isn’t “absolutely secure.” It always assumes the attacker hasn’t got access to the host. Still, in the real world, <strong>reducing the chance of lateral movement</strong> is a really important layer of protection in itself.</p>
<p>This actually also helps avoid hostname / database name conflicts. When a lot of databases on the same network use the same hostname, some services may end up connecting to the wrong database now and then.</p>
<h2 id="firewall-strategy">Firewall Strategy</h2>
<p>If you’re using a VPS, my suggestion is to use the firewall from your cloud provider first, and keep host firewall rules to a minimum. For the external firewall, limit the inbound ports you allow as strictly as you can, and reach your services through domain names and the reverse proxy. There are a few reasons. Two layers of firewall get confusing very easily, and it’s easy to forget which one you configured and which one you didn’t. I also found that Docker port mapping sometimes bypasses the local firewall, which can expose internal service ports by accident, and a lot of the time I didn’t even notice. And the cloud firewall can block large-scale scanning traffic early, which moves that compute cost onto the cloud provider.</p>
<p>Usually the cloud firewall only allows these inbound ports:</p>
<ul>
<li>80 / 443</li>
<li>The VPN port</li>
<li>SSH</li>
<li>Other ports you think really need to be open</li>
</ul>
<p>Even if other ports are listening on the host, they’re invisible from the outside as long as the cloud firewall doesn’t allow them. You can only reach them through the VPN, which saves a lot of worry.</p>
<h2 id="ssh-security">SSH Security</h2>
<p>There are lots of other great articles on this topic, so I’ll just cover some very basic but really important points. These are also things I still do all the time:</p>
<ul>
<li>Disable SSH login for root. If you need root, use a normal user plus sudo.</li>
<li>Don’t make the normal user’s SSH username too easy to guess, like admin.</li>
<li>Give the normal user a long random password (I suggest generating it with a password manager), and use <strong>key-based login</strong> by default day to day.</li>
<li>Optional: only allow key-based login for the normal user and disable passwords. (Be careful about the case where the key isn’t backed up and then you lose the device.)</li>
</ul>
<p>A lot of people suggest changing the SSH port. On whether to change it in a personal environment, here’s my own view:</p>
<ul>
<li>In a personal environment, the security benefit is limited</li>
<li>Some configuration later on gets more complicated</li>
<li>In the end it depends on your own habits</li>
</ul>
<h2 id="afterword">Afterword</h2>
<p>Actually, I’ve now moved to a dedicated server instead of a VPS, which is like renting a physical server in a data center that you can actually see. That’s because I now need to host dozens of containers (I went through them all, and I need every one of them). I think this shows that self-hosting really has become technical infrastructure I can’t do without day to day. It also shows that I think spending more money on it every month is really worth it.</p>
<p>Also, I should point out that in some places, the setup in this post, where one private VPN carries both your internal service traffic and your normal browsing traffic, is hard to pull off. For example, the server’s uplink bandwidth may be very expensive there, or the traffic may be metered. In that case, my suggestion is to do split routing ahead of time on the software router at home: separate out the self-hosting traffic first, and let normal browsing traffic go out through your home connection.</p>
<p>One more thing. Even if your server has enough uplink bandwidth to support a private VPN, a lot of the time you probably don’t want to browse the web with your VPS’s IP, because that directly exposes your identity. A better option is to send the traffic on to a VPN server from another VPN provider, and then go out through the exit IP it provides. I have a mature solution for this that I’ve been using myself for a long time. You can check out my blog post: <a href="https://blog.bktus.com/en/archives/qqyy2t/">Building a Personal Chained VPN Network: Efficient Privacy Protection, Unlimited Device Management, and Remote Access</a>.</p>
]]></content:encoded></item><item><title>Building a Personal Chained VPN Network: Efficient Privacy Protection, Unlimited Device Management, and Remote Access</title><link>https://blog.bktus.com/en/archives/qqyy2t/</link><pubDate>Mon, 02 Jun 2025 19:20:00 +0200</pubDate><guid>https://blog.bktus.com/en/archives/qqyy2t/</guid><description>&lt;p>In some situations, you might want to set up a chained VPN. That means your devices on the internal network first connect to a self-hosted WireGuard server (a personal VPS, for example), and that server then forwards all traffic to another VPN (a company, school or commercial VPN service) before it goes out to the Internet. This kind of “VPN over VPN” setup has some clear advantages:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>Strong privacy protection: Traffic from your devices first goes through your own encrypted WireGuard tunnel to your personal VPS. The VPS then forwards it a second time through the commercial or company VPN, and it finally reaches the Internet from that VPN’s exit IP. This way, the VPS never uses its own IP address to access the target network directly, which greatly reduces the risk of your personal IP being tracked or leaked.&lt;/p></description><content:encoded><![CDATA[<p>In some situations, you might want to set up a chained VPN. That means your devices on the internal network first connect to a self-hosted WireGuard server (a personal VPS, for example), and that server then forwards all traffic to another VPN (a company, school or commercial VPN service) before it goes out to the Internet. This kind of “VPN over VPN” setup has some clear advantages:</p>
<ul>
<li>
<p>Strong privacy protection: Traffic from your devices first goes through your own encrypted WireGuard tunnel to your personal VPS. The VPS then forwards it a second time through the commercial or company VPN, and it finally reaches the Internet from that VPN’s exit IP. This way, the VPS never uses its own IP address to access the target network directly, which greatly reduces the risk of your personal IP being tracked or leaked.</p>
</li>
<li>
<p>Centralized and easy management: All client devices connect to one WireGuard service, so you don’t have to configure every device one by one. More importantly, it also neatly gets around the device limit per account that some commercial VPN providers set. You pay once, and all your devices share it.</p>
</li>
<li>
<p>Flexible and easy to switch: The commercial or company VPN configuration only needs to be changed once, on your self-hosted WireGuard server, and downstream clients (phones, computers and so on) don’t need any changes. So replacing or reconfiguring the upstream VPN is much faster and easier, and day-to-day maintenance gets a lot more efficient.</p>
</li>
<li>
<p>Your own virtual LAN, so devices can talk to each other: Once all your devices are connected to your self-hosted WireGuard VPN server, each one automatically gets an IP address in the same VPN virtual LAN (for example, 10.8.0.0/16). Devices in this network can talk to each other directly, without any extra setup.</p>
</li>
</ul>
<p>This convenience means that wherever you are, you can easily do things like:</p>
<ul>
<li>Remote SSH login: access your servers at home or in the office at any time.</li>
<li>Private NAS media access: reach the media files on your home NAS while you’re out.</li>
<li>Remote work: securely access internal company network resources from anywhere, at any time.</li>
</ul>
<h2 id="the-solution">The Solution</h2>
<p>After some exploring and hands-on work, I gradually worked out my own chained VPN setup. In general, it’s deployed entirely with Docker, and the core components are:</p>
<ol>
<li><strong>Gluetun</strong>: a Docker image with built-in OpenVPN/WireGuard clients that can connect to all the major VPN providers. In this example, we configure it to act as a VPN gateway inside a Docker network.</li>
<li><strong>WG-Easy</strong>: a simplified WireGuard Docker image with a built-in web interface, which makes it easy to create and manage WireGuard configuration files. Downstream devices connect to this service with a WireGuard client, and all their traffic is forwarded to the Gluetun container, which then sends it on to the upstream VPN.</li>
</ol>
<p>The advantage of this setup is that it’s fully containerized, so it doesn’t pollute the environment of your server or VPS. It’s also fairly stable. In my own daily use, I haven’t noticed any obvious latency, stuttering or dropped connections, and the performance is good (tested on my home connection). Also, the setup now supports IPv6. Even if a device is on an IPv4-only network, it can still reach IPv6 resources through the chain.</p>
<p>In terms of network topology, the whole system only needs one Docker bridge network:</p>
<ul>
<li><strong>vpn</strong>: the “VPN network” between Gluetun and WG-Easy. Gluetun acts as the gateway in it, and all traffic from WG-Easy enters this network and is forwarded by Gluetun to the upstream VPN.</li>
</ul>
<pre class="mermaid">
  flowchart TD
 subgraph s1[&#34;Your Devices&#34;]
        n1[&#34;Phone&#34;]
        E[&#34;PC&#34;]
        D[&#34;Laptop&#34;]
  end
 subgraph s2[&#34;Your Server (VPS)&#34;]
        n2[&#34;WG-Easy (WireGuard Server)&#34;]
        F[&#34;Gluetun (VPN Client)&#34;]
  end
    n1 -- WireGuard Tunnel --&gt; n2
    n2 --&gt; F
    E -- &#34;WireGuard Tunnel&#34; --&gt; n2
    D -- &#34;WireGuard Tunnel&#34; --&gt; n2
    F -- VPN Tunnel --&gt; B[&#34;VPN Provider&#34;]
    B --&gt; n3[&#34;Internet&#34;]
    n1@{ shape: rect}
    B@{ shape: rect}
    n3@{ shape: rect}
</pre>
<p>Here, WG-Easy listens on UDP 51820. When a client (your own phone or computer) connects, WG-Easy (inside its container) sends the traffic over the Docker network <code>vpn</code> to the Gluetun container, and Gluetun connects to the upstream VPN over WireGuard/OpenVPN. All downstream traffic finally leaves through Gluetun’s <code>tun0</code> interface, and that’s how you get the chained VPN.</p>
<hr>
<p>If you want to skip the detailed setup steps below and just use the complete, integrated setup, you can check out my repository on GitHub:</p>
<p>👉 <strong><a href="https://github.com/saturneric/wg-easy-gluetun">https://github.com/saturneric/wg-easy-gluetun</a></strong></p>
<h2 id="environment-and-prerequisites">Environment and Prerequisites</h2>
<ul>
<li>A Linux server or VPS that supports Docker and Docker Compose (with kernel support for WireGuard and iptables/NFT). I recommend kernel version 5.10 or later.</li>
<li>Docker and Docker Compose v2+ installed.</li>
<li>An account with a commercial VPN provider that supports OpenVPN/WireGuard.</li>
<li>Make sure the host allows the CAP_NET_ADMIN and SYS_MODULE capabilities and can load the WireGuard kernel module (you can check with <code>lsmod | grep wireguard</code>).</li>
</ul>
<h2 id="create-a-custom-network"><strong>Create a Custom Network</strong></h2>
<p>Before the actual deployment, first create the bridge network on the host:</p>
<pre tabindex="0"><code>docker network create \
  --driver bridge \
  --subnet 172.31.0.0/16 \
  --subnet fd01:beee:beee::/48 \
  vpn
</code></pre><p>Note: If an existing network conflicts with these subnets, adjust them to fit your own situation. This example assumes the Docker bridge supports IPv6 (for the IPv6 forwarding in the example). If you don’t use IPv6, you can leave out the related configuration or set disable_ipv6=1.</p>
<h2 id="docker-compose-configuration-explained">Docker Compose Configuration Explained</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">version</span>: <span style="color:#e6db74">&#34;3&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">services</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">gluetun</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">qmcgaw/gluetun</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">cap_add</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">NET_ADMIN</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">SYS_MODULE</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">devices</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/dev/net/tun:/dev/net/tun</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">environment</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">VPN_SERVICE_PROVIDER=${VPN_SERVICE_PROVIDER}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">VPN_TYPE=${VPN_TYPE}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">WIREGUARD_PRIVATE_KEY=${WIREGUARD_PRIVATE_KEY}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">WIREGUARD_PRESHARED_KEY=${WIREGUARD_PRESHARED_KEY}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">WIREGUARD_ADDRESSES=${WIREGUARD_ADDRESSES}</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">SERVER_REGIONS=${SERVER_REGIONS}</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">configs</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#f92672">source</span>: <span style="color:#ae81ff">post-rules.txt</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">target</span>: <span style="color:#ae81ff">/iptables/post-rules.txt</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">volumes</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/lib/modules:/lib/modules:ro</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/data/gluetun-ws:/gluetun</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">sysctls</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.ip_forward=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.conf.all.src_valid_mark=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.disable_ipv6=0</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.forwarding=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.default.forwarding=1</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.4</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::4&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">restart</span>: <span style="color:#ae81ff">unless-stopped</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">wg-easy</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">image</span>: <span style="color:#ae81ff">ghcr.io/wg-easy/wg-easy:15</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ports</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#e6db74">&#34;51820:51820/udp&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.8</span>
</span></span><span style="display:flex;"><span>        <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::8&#34;</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">cap_add</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">NET_ADMIN</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">SYS_MODULE</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">sysctls</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.ip_forward=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv4.conf.all.src_valid_mark=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.disable_ipv6=0</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.all.forwarding=1</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">net.ipv6.conf.default.forwarding=1</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">volumes</span>:
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/lib/modules:/lib/modules:ro</span>
</span></span><span style="display:flex;"><span>      - <span style="color:#ae81ff">/data/wg-easy:/etc/wireguard</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">restart</span>: <span style="color:#ae81ff">unless-stopped</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">external</span>: <span style="color:#66d9ef">true</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">configs</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">post-rules.txt</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">content</span>: |<span style="color:#e6db74">
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      iptables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -I INPUT -p icmpv6 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -I OUTPUT -p icmpv6 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      ip6tables -t nat -A POSTROUTING -o tun0 -j MASQUERADE</span>
</span></span></code></pre></div><p>Next, let’s go through it piece by piece:</p>
<ul>
<li><code>image: qmcgaw/gluetun</code>: “Lightweight swiss-army-knife-like VPN client to multiple VPN service providers”. It can provide VPN service in a container environment and give a VPN connection to other containers that need one. This is the officially maintained Gluetun image, and it supports many VPN protocols and providers.</li>
<li><code>cap_add: [NET_ADMIN, SYS_MODULE]</code>: The container needs the NET_ADMIN and SYS_MODULE capabilities, plus settings like IP forwarding (the same for WG-Easy and Gluetun). These give the container permission to manage the network and load kernel modules, so it can create the TUN device and configure routes, and the WireGuard tunnel and routing work properly.</li>
<li><code>devices: [&quot;/dev/net/tun:/dev/net/tun&quot;]</code>: mounts the host’s TUN device into the container, so Gluetun can create a virtual network interface.</li>
<li><code>image: ghcr.io/wg-easy/wg-easy:15</code>: the official WG-Easy image (you can change the version as needed).</li>
<li><code>&quot;51820:51820/udp&quot;</code>: maps UDP port 51820 on the host to the container, so downstream devices can connect to WireGuard through the host or directly through the Docker network. If you need to expose WG-Easy’s web admin interface, you also need to add <code>&quot;51821:51821/tcp&quot;</code>.</li>
</ul>
<p>For the specific parameters in the environment variables, see the <a href="https://github.com/qdm12/gluetun-wiki/tree/main">official Gluetun documentation</a>.</p>
<p>Note that the two settings below fix each container’s IP address on the Docker bridge. They’re referenced a lot later, when configuring WG-Easy’s routing table and firewall rules. If you change them, remember to update the corresponding values in the later configuration too.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.4</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::4&#34;</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.8</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::8&#34;</span>
</span></span></code></pre></div><p>Here’s what the firewall rules added by Gluetun’s extra startup script <code>post-rules.txt</code> do:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e"># 1. IPv4 forwarding rules (allow forwarding in both directions, eth0 &lt;-&gt; tun0)</span>
</span></span><span style="display:flex;"><span>iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 2. IPv4 NAT masquerade (SNAT for every packet going out through tun0)</span>
</span></span><span style="display:flex;"><span>iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 3. Allow IPv6 ICMP (so Neighbor Discovery Protocol and similar protocols work)</span>
</span></span><span style="display:flex;"><span>ip6tables -I INPUT -p icmpv6 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -I OUTPUT -p icmpv6 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 4. IPv6 forwarding rules (allow forwarding in both directions, eth0 &lt;-&gt; tun0)</span>
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -i eth0 -o tun0 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 5. IPv6 NAT masquerade (SNAT for every IPv6 packet going out through tun0)</span>
</span></span><span style="display:flex;"><span>ip6tables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
</span></span></code></pre></div><p>With these rules, downstream traffic (coming from WG-Easy) can be forwarded to the <code>tun0</code> interface inside the Gluetun container and go into the upstream VPN tunnel. Packets coming back from upstream can also get back into the Docker network, reach WG-Easy and then return to the client. That’s how it works as a VPN gateway.</p>
<h2 id="wg-easy-post-up-script-explained">WG-Easy “Post Up” Script Explained</h2>
<p>Besides Gluetun’s network rules, you also need to set up “Post Up” and “Post Down” scripts in WG-Easy. They add routes and ip rules dynamically after the WireGuard tunnel comes up, so all downstream client traffic goes through Gluetun. This example is for WG-Easy v15, because WG-Easy only supports IPv6 from v15 on. If you want to see the configuration for v14, you can check <a href="https://github.com/qdm12/gluetun/discussions/1192#discussioncomment-12689316">the post I published on GitHub</a>.</p>
<p><img alt="Post Up and Post Down hooks in the WG-Easy admin panel" loading="lazy" src="https://img.bktus.com/One7gB5U.webp"></p>
<p>In the current version, v15.0.0, <strong>you need to turn my example into a single line (with the commands separated by semicolons) and fill it into the input box shown in the screenshot</strong>. Or you can write the script into a file, add it to a directory in WG-Easy and reference it from there. Then click <code>Save</code> and wait for WG-Easy to show a message that the configuration was saved. If needed, restart the WG-Easy container after configuring it.</p>
<h3 id="configuration-example">Configuration Example</h3>
<p>Here’s the full example with explanations (note that this needs to be configured on WG-Easy’s web admin page):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e"># WG-Easy Post Up script (example)</span>
</span></span><span style="display:flex;"><span>VPN<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>ifconfig | grep -B1 172.31.0.8 | grep -o <span style="color:#e6db74">&#34;^\w*&#34;</span><span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># First, drop all forwarded traffic by default</span>
</span></span><span style="display:flex;"><span>iptables -P FORWARD DROP
</span></span><span style="display:flex;"><span>ip6tables -P FORWARD DROP
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Open the WireGuard UDP port ({{port}} is replaced with the WireGuard port, usually 51820)</span>
</span></span><span style="display:flex;"><span>iptables -A INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Allow forwarding between the local side and the WireGuard tunnel</span>
</span></span><span style="display:flex;"><span>iptables -A FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A FORWARD -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A FORWARD -s <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Configure the IPv4 routing table (table 200 is used only for downstream traffic)</span>
</span></span><span style="display:flex;"><span>iptables -t nat -A POSTROUTING -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -o $VPN -j MASQUERADE
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Add an ip rule so traffic from {{ipv4Cidr}} uses table 200</span>
</span></span><span style="display:flex;"><span>ip rule add from <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route add default via 172.31.0.4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route add 172.31.0.0/16 via 172.31.0.1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route add <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Configure the IPv6 routing table (same idea, also table 200)</span>
</span></span><span style="display:flex;"><span>ip -6 rule add from <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add default via fd01:beee:beee::4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add fd01:beee:beee::/48 via fd01:beee:beee::1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span></code></pre></div><p>The three variables below are WG-Easy template variables. WG-Easy fills them in automatically, but you need to make sure the values are valid:</p>
<ul>
<li><code>{{port}}</code>: the port WireGuard listens on, usually the same as the port mapped in Docker (51820).</li>
<li><code>{{ipv4Cidr}}</code>: the downstream client subnet, for example <code>10.8.0.0/24</code>. WG-Easy assigns addresses from it to peers.</li>
<li><code>{{ipv6Cidr}}</code>: the downstream client IPv6 subnet, for example <code>fdcc:ad94:bacf:61a4::/64</code>.</li>
</ul>
<p>Here’s the script for PostDown (it simply replaces <code>-A</code> with <code>-D</code> and <code>add</code> with <code>del</code>):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>VPN<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>ifconfig | grep -B1 172.31.0.8 | grep -o <span style="color:#e6db74">&#34;^\w*&#34;</span><span style="color:#66d9ef">)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -P FORWARD DROP
</span></span><span style="display:flex;"><span>ip6tables -P FORWARD DROP
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -D INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D INPUT -p udp -m udp --dport <span style="color:#f92672">{{</span>port<span style="color:#f92672">}}</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -D FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -D FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -D FORWARD -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip6tables -D FORWARD -i wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D FORWARD -o wg0 -m state --state RELATED,ESTABLISHED -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D FORWARD -s <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -d <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> -i wg0 -o wg0 -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -t nat -D POSTROUTING -s <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> -o $VPN -j MASQUERADE
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip rule del from <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route del default via 172.31.0.4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route del 172.31.0.0/16 via 172.31.0.1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip route del <span style="color:#f92672">{{</span>ipv4Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>ip -6 rule del from <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route del default via fd01:beee:beee::4 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route del fd01:beee:beee::/48 via fd01:beee:beee::1 dev $VPN table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route del <span style="color:#f92672">{{</span>ipv6Cidr<span style="color:#f92672">}}</span> dev wg0 table <span style="color:#ae81ff">200</span>
</span></span></code></pre></div><h3 id="using-multiple-docker-bridges">Using Multiple Docker Bridges</h3>
<p>In my environment, WG-Easy is connected to several Docker bridges. Besides <code>vpn</code>, there are other bridges dedicated to other services. If you need to connect WG-Easy to another bridge network, I recommend keeping the <code>$VPN</code> variable from the configuration above instead of using the <code>{{device}}</code> template variable that WG-Easy provides. The <code>$VPN</code> variable automatically picks the network interface for the matching subnet, so the routing and firewall rules only apply to that interface and won’t accidentally affect other network services. For a second Docker bridge, you can add this command to PostUp:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>VPN_IF<span style="color:#f92672">=</span><span style="color:#66d9ef">$(</span>ifconfig | grep -B1 172.31.0.8 | grep -o <span style="color:#e6db74">&#34;^\w*&#34;</span><span style="color:#66d9ef">)</span>
</span></span></code></pre></div><p>Remember to replace <code>172.31.0.8</code> with the IP of the WG-Easy container on that Docker bridge, which means you need to give WG-Easy a fixed IP on that bridge. Or you can adjust this pattern so grep filters by subnet instead of by IP address. That way, <code>$VPN_IF</code> resolves to the network interface the subnet is using. Then update the routing table and firewall rules as well, so traffic between the service networks stays isolated and still gets forwarded properly:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>iptables -t nat -A POSTROUTING -s &lt;NETWORK_CIDR&gt; -o &lt;NETWORK_INTERFACE&gt; -j MASQUERADE
</span></span><span style="display:flex;"><span>ip6tables -t nat -A POSTROUTING -s &lt;NETWORK_CIDR&gt; -o &lt;NETWORK_INTERFACE&gt; -j MASQUERADE  <span style="color:#75715e"># if this bridge has IPv6</span>
</span></span><span style="display:flex;"><span>ip route add &lt;NETWORK_CIDR&gt; via &lt;NETWORK_GATEWAY&gt; dev &lt;NETWORK_INTERFACE&gt; table <span style="color:#ae81ff">200</span>
</span></span><span style="display:flex;"><span>ip -6 route add &lt;NETWORK_CIDR&gt; via &lt;NETWORK_GATEWAY&gt; dev &lt;NETWORK_INTERFACE&gt; table <span style="color:#ae81ff">200</span>  <span style="color:#75715e"># if this bridge has IPv6</span>
</span></span></code></pre></div><p>Some readers need to provide VPN service to multiple containers but don’t want to attach multiple Docker bridges to WG-Easy directly. For them, here’s a more fine-grained approach that gives you more control. Bind each service that needs the VPN to its own WireGuard client container (such as linuxserver/wireguard or an image like Gluetun), then use WG-Easy (or a similar admin interface) to create a separate WireGuard account for each service. Finally, import the matching configuration into that service’s WireGuard client container. The advantages are:</p>
<ul>
<li>Each service has its own WireGuard configuration file, so traffic isolation is more thorough;</li>
<li>You can monitor and limit a single service’s network access more directly in WG-Easy;</li>
<li>You no longer need rules like <code>iptables -A FORWARD -i eth+ -o tun0</code> that let everything through a network interface. Instead, you only grant trust to specific containers.</li>
</ul>
<p>This approach gives you finer-grained permission control for each container, for example real-time traffic monitoring, or revoking a service’s VPN access at any time. But running multiple WireGuard client containers does cost extra CPU and memory.</p>
<h2 id="deployment">Deployment</h2>
<h3 id="create-and-load-the-environment-variables">Create and Load the Environment Variables</h3>
<p>In the same directory as docker-compose.yml, create a .env file with the following content (see the Gluetun documentation linked above):</p>
<pre tabindex="0"><code>VPN_SERVICE_PROVIDER=XXX
VPN_TYPE=XXXX
WIREGUARD_PRIVATE_KEY=YOUR_PRIVATE_KEY
WIREGUARD_PRESHARED_KEY=YOUR_PSK
WIREGUARD_ADDRESSES=XXX
SERVER_REGIONS=XXX
</code></pre><h3 id="make-sure-the-docker-network-exists">Make Sure the Docker Network Exists</h3>
<p>As mentioned earlier, make sure the custom network named <code>vpn</code> has been created.</p>
<h3 id="start-the-containers">Start the Containers</h3>
<p>Run the command below and watch the startup logs of the wg-easy and gluetun-ws containers, to confirm that both the WireGuard client and the server come up properly.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ docker-compose up -d
</span></span></code></pre></div><h3 id="add-a-wireguard-client">Add a WireGuard Client</h3>
<ul>
<li>Open <code>http://&lt;WG-Easy-container-IP&gt;:51821</code> in a browser (depending on your WG-Easy configuration) to get to WG-Easy’s web interface. Or you can add a TCP mapping for port 51821 in Docker Compose and open the web admin interface at <code>http://&lt;host-IP&gt;:51821</code>. Using Nginx as a reverse proxy works too.</li>
<li>In the Hooks section of the Admin Panel, fill in the Post Up and Post Down configuration given above and save it. <strong>This step is really important.</strong></li>
<li>Create a new Peer (enter a name for it). WG-Easy generates the matching configuration file with AllowedIPs = 0.0.0.0/0, ::/0, and you download the .conf file.</li>
<li>Import this configuration on your local device (Windows/Mac/Linux/phone), start WireGuard and test it.</li>
</ul>
<h3 id="verify-the-vpn2vpn-tunnel">Verify the VPN2VPN Tunnel</h3>
<ul>
<li>After the client connects to WireGuard, open <a href="https://ipleak.net">https://ipleak.net</a> or <a href="https://ifconfig.co">https://ifconfig.co</a> and check the exit IP.</li>
<li>If it shows the IP of the upstream commercial VPN, the chained VPN tunnel is working.</li>
<li>You can run <code>curl ifconfig.co</code> in the Gluetun container, and you should see the public IP from the commercial VPN. Run the same thing in the WG-Easy container, and you should see the same exit IP (through the upstream VPN).</li>
</ul>
<h3 id="check-traffic-routing">Check Traffic Routing</h3>
<ul>
<li>In the WG-Easy container, run <code>ip rule show</code> / <code>ip route show table 200</code> to confirm that routing table 200 is in effect and the default route points to 172.31.0.4 (Gluetun).</li>
<li>In the Gluetun container, run <code>iptables -t nat -L -nv</code>, and you’ll see the matching MASQUERADE rules.</li>
<li>If you need to troubleshoot, run <code>tcpdump -i</code> on the relevant interface in each container to capture packets and check what the traffic is doing.</li>
</ul>
<h2 id="notes-and-considerations">Notes and Considerations</h2>
<h3 id="dns-leaks">DNS Leaks</h3>
<p>In this example, if you don’t take extra DNS security measures, there’s a risk of DNS leaks. When a container or client uses the host’s DNS or a public DNS directly, DNS queries may bypass the VPN tunnel and go upstream directly, which exposes your real IP and the websites you visit. Actually, Gluetun creates its own internal DNS server and forces all DNS traffic through it, so that using the local DNS doesn’t cause DNS leaks. But by default Gluetun only listens for DNS requests inside its own container, so other containers and the host can’t reach it. This also means WG-Easy can’t use Gluetun’s internal DNS service directly, so we can’t simply forward WG-Easy’s DNS traffic to Gluetun.</p>
<p>To avoid these problems, you usually need to deploy a separate DNS service (such as AdGuard Home or Pi-hole) and send all DNS queries through it, with upstream resolution going out through the VPN exit. This way, DNS queries also go through the VPN, and you get ad blocking and malicious domain filtering as well.</p>
<h4 id="deploy-adguard-home-or-a-similar-dns-service">Deploy AdGuard Home or a Similar DNS Service</h4>
<p>We can run a separate AdGuard Home container in the same Docker network (the <code>vpn</code> network, for example). AdGuard Home listens on both UDP and TCP port 53, and it has stronger features like ad blocking and malicious domain filtering. For example, you can add a service like the one below to docker-compose.yml (just as a demonstration):</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-yaml" data-lang="yaml"><span style="display:flex;"><span><span style="color:#f92672">adguard</span>:
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">image</span>: <span style="color:#ae81ff">adguard/adguardhome</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">restart</span>: <span style="color:#ae81ff">unless-stopped</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">networks</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">vpn</span>:
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">ipv4_address</span>: <span style="color:#ae81ff">172.31.0.16</span>
</span></span><span style="display:flex;"><span>      <span style="color:#f92672">ipv6_address</span>: <span style="color:#e6db74">&#34;fd01:beee:beee::16&#34;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">ports</span>:
</span></span><span style="display:flex;"><span>    - <span style="color:#e6db74">&#34;53:53/udp&#34;</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#e6db74">&#34;53:53/tcp&#34;</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#e6db74">&#34;3000:3000/tcp&#34;</span> <span style="color:#75715e"># Web interface port</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">volumes</span>:
</span></span><span style="display:flex;"><span>    - <span style="color:#ae81ff">/data/adguard/workdir:/opt/adguardhome/work</span>
</span></span><span style="display:flex;"><span>    - <span style="color:#ae81ff">/data/adguard/conf:/opt/adguardhome/conf</span>
</span></span></code></pre></div><p>This way, AdGuard Home is exposed on port 53 of the host and also gets an IP in the Docker <code>vpn</code> network (here, 172.31.0.16). After that, we can reach this AdGuard Home container directly from WG-Easy, from client devices and even from other containers. You can also choose not to expose port 53 at all and only provide DNS through the <code>vpn</code> bridge.</p>
<p>Open AdGuard Home’s web interface (usually at <code>http://&lt;Host-IP&gt;:3000</code>) and check that the upstream DNS settings are correct and that ad blocking and the other policies work. Then you need to test it. Ideally, run <code>nslookup hostname 172.31.0.16</code> in the WG-Easy container to check whether the DNS path from WG-Easy to AdGuard Home works.</p>
<h4 id="change-the-dns-settings">Change the DNS Settings</h4>
<p>After AdGuard Home is deployed, configured and tested, we need to adjust the client DNS settings that WG-Easy generates, because by default WG-Easy sets the DNS servers to <code>1.1.1.1</code> and <code>2606:4700:4700::1111</code>. To do this, change the DNS in WG-Easy’s Admin Panel to the IPs of the AdGuard Home container: <code>172.31.0.16</code> and <code>fd01:beee:beee::16</code>.</p>
<p><img alt="DNS settings in the WG-Easy admin panel" loading="lazy" src="https://img.bktus.com/pinDVKdk.webp"></p>
<p>Note that the DNS settings of devices you’ve already added don’t follow the DNS settings in the Admin Panel automatically. You need to update the DNS settings in WireGuard on the device by hand, or adjust that device separately in WG-Easy, then download the configuration file again and apply it to the device.</p>
<p>Finally, to strictly limit WG-Easy’s outbound DNS access and rule out DNS leaks, we can add the following commands to the PostUp hook script:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span><span style="color:#75715e"># Allow packets to the AdGuard Home DNS port</span>
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -o $VPN -d 172.31.0.16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -o $VPN -d 172.31.0.16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -o $VPN -d fd01:beee:beee::16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -o $VPN -d fd01:beee:beee::16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Reject DNS queries from the rest of the network stack, so nothing leaks to the host</span>
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>iptables -A OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -A OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span></code></pre></div><p>The matching PostDown script is:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>iptables -D OUTPUT -o $VPN -d 172.31.0.16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>iptables -D OUTPUT -o $VPN -d 172.31.0.16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -o $VPN -d fd01:beee:beee::16 -p udp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -o $VPN -d fd01:beee:beee::16 -p tcp --dport <span style="color:#ae81ff">53</span> -j ACCEPT
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>iptables -D OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>iptables -D OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -p tcp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span><span style="display:flex;"><span>ip6tables -D OUTPUT -p udp --dport <span style="color:#ae81ff">53</span> -j REJECT
</span></span></code></pre></div><h3 id="ipv6-support">IPv6 Support</h3>
<ul>
<li>If you don’t use IPv6, you can remove <code>ipv6_address</code>, the ip6tables rules and the other related settings from docker-compose.yml, and don’t enable IPv6 when you create the network.</li>
<li>If you do use IPv6, make sure both the host and the VPN provider support IPv6 tunnels.</li>
</ul>
<h3 id="performance-and-resources">Performance and Resources</h3>
<p>WireGuard itself is fairly demanding on the CPU, especially when it runs at full speed, so it needs reasonably good hardware.</p>
<h3 id="multi-hop">Multi-Hop</h3>
<p>This architecture is already a double hop (downstream → WG-Easy → Gluetun → upstream VPN → Internet). If you want to go further and set up more hops, you can pick a different region for the upstream VPN, or stack another VPN container on top. But keep in mind that performance goes down and maintenance gets more complex.</p>
<h3 id="firewall-and-security">Firewall and Security</h3>
<p>In this example, the WG-Easy container only opens UDP port 51820 by default. If you need to access it from the public internet, open that port in the host firewall (iptables/nftables) and only allow trusted IPs, to keep the attack surface small.</p>
<h3 id="postdown-script">PostDown Script</h3>
<p>To avoid leaving rules behind after teardown, make sure to add the matching <code>iptables -D</code>, <code>ip rule del</code>, <code>ip route del</code> and similar commands to WG-Easy’s PostDown. Otherwise you may end up with duplicate routes or conflicts after a restart.</p>
<h2 id="network-troubleshooting-example">Network Troubleshooting Example</h2>
<p>When a client can’t reach the LAN or the Internet, here are some common steps to find the problem quickly. This is also what I usually do myself. I believe that by going from coarse to fine like this, you can find the root cause of most network connection problems.</p>
<p>Below are the concrete steps. I only show the IPv4 commands here, but for IPv6 you can easily do the same checks by adjusting the commands a little.</p>
<h3 id="ping-tests-in-stages-local">Ping Tests in Stages (Local)</h3>
<ul>
<li><code>ping 10.8.0.1</code>: check whether the VPN tunnel is up.</li>
<li><code>ping 172.31.0.8</code>: check whether the client can reach the WG-Easy container (Docker bridge network).</li>
<li><code>ping 172.31.0.4</code>: check connectivity from the bridge network to the Gluetun container.</li>
<li><code>ping 8.8.8.8</code>: test whether Gluetun can reach the Internet.</li>
</ul>
<p>If a step fails, check the routing or firewall rules for that specific step.</p>
<h3 id="check-dns-resolution-local">Check DNS Resolution (Local)</h3>
<p>If you pass the tests above, the path is fine at the IP level, so next we look at DNS.</p>
<ul>
<li><code>nslookup example.com</code>: through the default DNS server.</li>
<li><code>nslookup example.com 172.31.0.16</code>: through AdGuard Home.</li>
<li><code>nslookup example.com 8.8.8.8</code>: test whether public DNS works.</li>
</ul>
<p>If you can’t get a correct response, the DNS request isn’t reaching the server it should reach.</p>
<h3 id="debug-inside-the-containers">Debug Inside the Containers</h3>
<p>If DNS is also fine, the problem may be a deeper conflict in the routing tables or firewall rules. We can go into the WG-Easy or the Gluetun container to debug:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>docker exec -it &lt;container-name/id-of-wg-easy&gt; /bin/bash
</span></span><span style="display:flex;"><span>docker exec -it &lt;container-name/id-of-gluetun&gt; /bin/sh
</span></span></code></pre></div><p>Run these inside the container:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>ip rule show             <span style="color:#75715e"># Check whether the routing rules are correct</span>
</span></span><span style="display:flex;"><span>ip route show table <span style="color:#ae81ff">200</span>  <span style="color:#75715e"># Verify routing table 200</span>
</span></span><span style="display:flex;"><span>ping -c <span style="color:#ae81ff">2</span> 8.8.8.8        <span style="color:#75715e"># Test outbound access from the container</span>
</span></span><span style="display:flex;"><span>ping -c <span style="color:#ae81ff">2</span> example.com    <span style="color:#75715e"># Test DNS inside the container</span>
</span></span><span style="display:flex;"><span>iptables-save            <span style="color:#75715e"># Check the firewall rules</span>
</span></span><span style="display:flex;"><span>ip route                 <span style="color:#75715e"># Verify the default routing table</span>
</span></span></code></pre></div><h3 id="more-advanced-methods-tcpdump-routing-tables-and-firewall-checks">More Advanced Methods: tcpdump, Routing Tables and Firewall Checks</h3>
<p>If these simple methods still can’t find the problem, there’s probably a deeper conflict in the routing table or firewall configuration. At this point, you can use <code>tcpdump -i &lt;interface&gt;</code> to capture packets and see whether DNS (UDP/TCP 53) or other traffic reaches the container or gets dropped. Then carefully check the routing tables on the host and in the containers (<code>ip rule show</code>, <code>ip route show</code>) for overlapping or conflicting entries, and make sure the ip rule priorities and table entries actually take effect. After that, go through the iptables/ip6tables rules again, especially the FORWARD, OUTPUT and nat tables, to see whether a wrong or duplicate rule is blocking the traffic.</p>
<h3 id="problems-caused-by-updates">Problems Caused by Updates</h3>
<p>Keep in mind that your problem may also come from an image update of one of the containers. In that case, check whether there was an automatic update recently, and try downgrading the version to fix it. If the problem goes away, you can pin the old image version to keep the service working until the issue is finally solved.</p>
]]></content:encoded></item></channel></rss>