Setup your VPN server the easy way using docker

In this post, I will show you how to setup your own VPN server in a few simple steps.

## 1. Setup

Step 1 - Create a docker volume to save configuration

$ docker volume create --name ovpn-data

Step 2 - Generate config

This step will pull the docker image kylemanna/openvpn:2.3 and run command ovpn_genconfig. The IP_ADDRESS or DOMAIN is your public ip address, eg udp://vpn.hienhoang.ml

$ docker run -v ovpn-data:/etc/openvpn --log-driver=none --rm kylemanna/openvpn:2.3 ovpn_genconfig -u udp://{{IP_ADDRESS | DOMAIN}}

Step 3 - Generate public private key

This step will generate public private key. You must type in 4 to 1023 characters when ask for PEM pass phrase. This will take a few minutes to generate a strong private key.

$ docker run -v ovpn-data:/etc/openvpn --log-driver=none --rm -it kylemanna/openvpn:2.3 ovpn_initpki

> init-pki complete; you may now create a CA or requests.
> Your newly created PKI dir is: /etc/openvpn/pki
>
> Generating a 2048 bit RSA private key
> .......................................................................+++
> .....................................+++
> writing new private key to '/etc/openvpn/pki/private/ca.key.XXXXlNLGLn'
> Enter PEM pass phrase:

Next command is to generate client key

$ docker run -v ovpn-data:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN kylemanna/openvpn:2.3
$ docker run -v ovpn-data:/etc/openvpn --log-driver=none --rm -it kylemanna/openvpn:2.3 easyrsa build-client-full my_vpn nopass

> Generating a 2048 bit RSA private key
> ...................+++
> ........+++
> writing new private key to '/etc/openvpn/pki/private/my_vpn.key.XXXXpkifIk'
> -----
> Using configuration from /usr/share/easy-rsa/openssl-1.0.cnf

Step 4 - Export the ovpn file

$ docker run -v ovpn-data:/etc/openvpn --log-driver=none --rm kylemanna/openvpn:2.3 ovpn_getclient my_vpn > my_vpn.ovpn

That's it. my_vpn.ovpn is what you need to connect to this VPN server.

## 2. Usage

Check your VPN server running

$ docker ps

> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
> 04002f98ff76 kylemanna/openvpn:2.3 "ovpn_run" 4 minutes ago Up 4 minutes 0.0.0.0:1194->1194/udp strange_wilbur

Import the .ovpn file using openvpn client

Import profile -> FILE

Open the .ovpn file you generated above.

Then click Add and Connect