How To Create A VPN Server With OpenVPN And Docker Container

Ashok Raja T
Technology Specialist
September 13, 2021
Rate this article
Views    17039

If you are in need of your own VPN server, the easiest way is to create one with OpenVPN and docker. In this article, let us see how to create and configure the OpenVPN server with docker.

For someone who is new to docker, these steps may look convoluted. But it is worth the try as docker allows us to re-create a new VPN server in no time without disturbing the host Operating System. It also allows multiple applications to co-exist with each other.

Follow the steps in this link to install docker and run it as a non-root user.

Install OpenVPN

At this time of writing, there is no official docker image available from OpenVPN. But, there is a popular docker image named “kylemanna/openvpn” with more than 10 Million docker pull requests. The documentation is scattered across different sources and is not complete. With the below steps, you can install an OpenVPN pointing to UDP port (1194) under a user named “usr1” with a password.

#Create and initialize openvpn
docker run --rm -v $PWD:/etc/openvpn kylemanna/openvpn ovpn_genconfig -u udp://[IP address or domain name of your server]:1194

#In the below step, you have to provide a password for CA and key 
docker run --rm -v $PWD:/etc/openvpn -it kylemanna/openvpn ovpn_initpki

#Create User Account. Password provided in this step is required at the time of connection from client
docker run --rm -v $PWD:/etc/openvpn -it kylemanna/openvpn easyrsa build-client-full usr1

# Copy client certificate to host from container
docker run --rm -v $PWD:/etc/openvpn kylemanna/openvpn ovpn_getclient usr1 > usr1.ovpn

#Start OpenVPN container 
docker run --name openvpn -v $PWD:/etc/openvpn -d -p 1194:1194/udp --cap-add=NET_ADMIN --restart always kylemanna/openvpn

openvpn_docker

Since we are running the container as a named instance (–name openvpn), the name of the container can be used to start or stop the container.

To stop the container, execute the command “docker stop openvpn“. To start the container, execute the command “docker start openvpn” in the terminal.

Connecting To VPN Server

To connect to the VPN server, the ovpn file that was created in the previous step is required by the vpn clients. If the client is also Linux, scp can be used to move the file from the server to the client. To copy the file with scp, the command would be something similar to scp ar@10.10.30.4:/home/ar/vpn/usr1.ovpn ./

Check this article to learn more about scp.

Subscribe To Our Newsletter
Loading

Leave a comment