Ben Lobaugh
  • Growth & Leadership
  • Giving Back
  • Sailing
  • VW Bus
  • About
Photo by Sasun Bughdaryan on Unsplash

How to build a VPN client as a Docker container

Originally published at https://benlobaugh.medium.com/how-to-build-a-vpn-client-as-a-docker-container-106768772c4c

There is not much to be found online about running a VPN inside Docker. Indeed, I do not believe many out there have thought about it, however leveraging a VPN inside of Docker unlocks a wealth of new possibilities. Here are just a few examples that come to mind, some of which I have used on various projects.

  • Secure and encrypted communication of Docker containers to the outside world. This can be a great way to get local resources through an untrusted network.
  • Keep Docker containers separate from the local network, while still allowing access to the outside world.
  • Connecting remote Docker networks together. As an example, a VPN can securely sync data across multiple datacenters or regions.
  • Become cloud and server agnostic. By connecting your containers to a VPN, the containers can be freely moved around environments, and even live on separate clouds, without the need to reconfigure anything. Spin up the containers elsewhere, connect to the VPN, resume operations as normal.
  • Take advantage of cloud resources, locked into your private intranet. Perhaps useful for periodic large compute loads. Save money by not paying for servers until they are needed, with nearly the safety of an on premise solution.
  • For the devious, punch a hole in a firewall to bypass restrictions and allow your containers freedom to roam the world.

Now that the wheels are turning in that ole noggin, let me show you how I built a simple Docker container that is a client for any network supporting the openvpn client.

If you would like to play with the ideas presented here, a more advanced image is available on Github at https://github.com/blobaugh/docker-openvpn-client.

It is also available as a pre-built Docker image at https://hub.docker.com/r/blobaugh/openvpn-client

Though the container in the Github repository has support for multiple VPN services, via passing in a config file, I will keep this one a bit simpler.

The basic Dockerfile .

FROM ubuntu
RUN apt update && 
   \apt install openvpn curl -y
COPY vpn-files /vpn
ENTRYPOINT [ “openvpn”, “ — config”, “/vpn/service.ovpn” ]

This rather simplistic approach does the following:

  • Installs openvpn into the Docker image.
  • Copies the VPN service files into the image’s filesystem. This typically consists of an ovpn file and some certificates from the VPN service, and possibly a login auth file.
  • Directly runs the openvpn console command and connects the container to the VPN service.

Build image:

build -t openvpn-client .

Run a container from the image:

docker run --name=openvpn-client --add-cap NET_ADMIN --device=/dev/net/tun openvpn-client

You now have a Docker container running that is connected to a VPN service! Now it is time to do the cool stuff. Let’s test the connection by running a new container that utilizes the openvpn-client container as it’s network.

docker run --rm -it --network=container:openvpn-client alpine wget -q0 — ifconfig.me

That command runs a container based on alpine, attaches to the network stack from the openvpn-client container, and runs wget to retrive the public IP address.

Compare that IP to your networks public IP and they will be different. The IP will be from the VPN service.

Now any container can be attached to the openvpn-clientcontainer as it’s network and securely run it’s load over the VPN network.

Some things to consider — this is a very simplistic example.

  • It is possible to leak traffic out over the local network before the VPN service is connected. This can be overcome by putting rules in place on the openvpn-client container that force traffic to go through the tunnel only.
  • If the openvpn-client container crashes or is stopped for any reason, the containers attached to it will lose connectivity (likely desired), however, they will also need to be restarted when the VPN connects again, as they will not attach their network to the new container automagically.

I hope this simple example opens up the possibilities of using a VPN inside a Docker container for you. For a more full featured image that supports any openvpn compatible VPN service, take a look at my repository at https://github.com/blobaugh/docker-openvpn-client

Docker
Media Server
Medium
openvpn
Security
VPN