Setting up Openvpn in a docker container

about 4 years ago.

TL;DR: You can follow the steps in the Github repo

In a world of privacy violation, VPNs are a must. For a power user setting up a personal VPN is an easy task however for inexperienced users it's not so trivial.

A full setup guide of Open VPN on Linux Ubuntu is also available at Anonymster where they describe each steps so a beginner can sucessfully proceed to the installation

Along this guide I will walk you through setting up your personal vpn using docker. For this you will only need a VPS.

First let's install docker on the VPS, this will heavily depends on the VPS's operating system. check the official Installation documentation for OS specific instruction. After that, verify docker installation with:

docker --version

your version may differ.

Docker version 1.11.2 build b9f10c9

Then we need to create a folder in which will be used as volume to the docker configuration container

mkdir ovpn_data 
export OVPN_DATA="ovpn_data"

And now the docker container which will hold all the configuration files

docker run --name $OVPN_DATA -v /etc/openvpn busybox
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
8ddc19f16526: Pull complete
Digest: sha256:a59906e33509d14c036c8678d687bd4eec81ed7c4b8ce907b888c607f6a1e0e6
Status: Downloaded newer image for busybox:latest

Then initialize the container with our domain name (or IP address)

docker run --volumes-from $OVPN_DATA --rm kylemanna/openvpn ovpn_genconfig -u udp://chaker.tn
docker run --volumes-from $OVPN_DATA --rm -i kylemanna/openvpn ovpn_initpki
config -u udp://chaker.tn
Unable to find image 'kylemanna/openvpn:latest' locally
latest: Pulling from kylemanna/openvpn
e110a4a17941: Pull complete
1dbf65990725: Pull complete
13440a42cce9: Pull complete
5160ccd02b13: Pull complete
Digest: sha256:675d45ca8201d4558f14b29699bd4b71abbde960538631e8d0b4fbb60c267354
Status: Downloaded newer image for kylemanna/openvpn:latest
Successfully generated config

Then you will be asked to enter your a password to generate the certificate

…………
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'chaker.tn'
Certificate is to be certified until Aug 31 01:30:16 2026 GMT (3650 days)
Write out database with 1 new entries
Data Base Updated

Start the OpenVPN server, generate the certificate and copy it to the working directory

docker run --volumes-from $OVPN_DATA -d -p 1194:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn
docker run --volumes-from $OVPN_DATA --rm -it kylemanna/openvpn easyrsa build-client-full CLIENTNAME nopass
docker run --volumes-from $OVPN_DATA --rm kylemanna/openvpn ovpn_getclient CLIENTNAME > CLIENTNAME.ovpn

You will be asked for your passphrase one more time. After finishing those commands copy the config file to your device (in my case I'm using an android device) and start connecting to your own VPN.

android-openvpn.png