Running A Containerized OpenVPN Server in Docker 🌱

What is OpenVPN?

OpenVPN is a virtual private network system that implements techniques to create secure point-to-point or site-to-site connections in routed or bridged configurations and remote access facilities. It implements both client and server applications. -https://openvpn.net

Installing Docker

  1. Log into the Linux based device
  2. Run the following commands in the terminal
    # install prerequisites
    sudo apt install apt-transport-https ca-certificates curl software-properties-common gnupg-agent -y
    # add docker gpg key
    curl -fsSL https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release)/gpg | sudo apt-key add -
    # add docker software repository
    sudo add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$(awk -F'=' '/^ID=/{ print $NF }' /etc/os-release) $(lsb_release -cs) stable"
    # install docker
    sudo apt install docker-ce docker-compose containerd.io -y
    # enable and start docker service
    sudo systemctl enable docker && sudo systemctl start docker
    # add the current user to the docker group
    sudo usermod -aG docker $USER
    # reauthenticate for the new group membership to take effect
    su - $USER

Running OpenVPN Server

  1. Continue with the following commands in a terminal window
    # create a working directory
    mkdir ~/docker/openvpn-server -p
    # SET SOME VARIABLES
    # set the openvpn service name (recommended to use the ovpn-data-)
    OVPN_DATA="ovpn-data-docker"
    # set the external IP address or DNS name
    OVPN_HOST="udp://VPN.SERVERNAME.COM"
    # set a client name identifier
    OVPN_CLIENT="client1"
    # pull the docker image
    docker pull kylemanna/openvpn
    # generate new openvpn config
    docker run -v ~/docker/openvpn-server:/etc/openvpn --rm kylemanna/openvpn ovpn_genconfig -u ${OVPN_HOST}
    # initialize the openvpn pki
    docker run -v ~/docker/openvpn-server:/etc/openvpn --rm -it kylemanna/openvpn ovpn_initpki
  2. Enter and confirm a password to protect the PKI
  3. Enter a common name for the PKI (ie OpenVPN Server)
  4. Enter and confirm a password to protect the CA private key
  5. Continue with the following commands in a terminal window
    # start the openvpn-server container
    docker run -d --name=openvpn-server -v ~/docker/openvpn-server:/etc/openvpn -p 1194:1194/udp --cap-add=NET_ADMIN --restart unless-stopped kylemanna/openvpn
    # generate a client certificate with no password
    # NOTE any client with this certificate can connect to the VPN server
    docker run -v ~/docker/openvpn-server:/etc/openvpn --rm -it kylemanna/openvpn easyrsa build-client-full ${OVPN_CLIENT} nopass
    # enter the password for the CA private key
    # generate an openvpn configuration file for the client
    docker run -v ~/docker/openvpn-server:/etc/openvpn --rm kylemanna/openvpn ovpn_getclient ${OVPN_CLIENT} > ${OVPN_CLIENT}.ovpn
    # output the generated .ovpn file
    cat ${OVPN_CLIENT}.ovpn

Installing OpenVPN Client Software and Testing

At this point the OpenVPN server is running but you will need to configure your router to forward UDP port 1194 from the WAN to the IP address of the Docker host. The process to do this will vary based on your router

  1. Download the OpenVPN software Download
  2. Run the installer accepting all the default values
  3. Click the Start button and search OpenVPN GUI
  4. Select OpenVPN GUI from the results to start the application
  5. Copy the generated .ovpn configuration file from the Docker host to the OpenVPN client install directory/config
  6. Right click OpenVPN GUI in the system tray > Connect

Documentation: https://hub.docker.com/r/kylemanna/openvpn