Saturday, June 15, 2024

Configure OpenVPN in Docker: Secure and Encrypted Access to Your Network


With the growing need for remote access and data security, the use of virtual private networks (VPN) is becoming increasingly common. One popular solution is OpenVPN, which offers flexibility, security, and broad cross-platform capabilities. In this guide, we’ll cover how to configure OpenVPN within a Docker environment, allowing you to easily run your own VPN server with added isolation and flexibility.

You can see the video tutorial here

Why Use Docker?

Docker is a platform that allows you to package, distribute, and run applications in isolated environments, called containers. This makes application management easier by ensuring that all necessary dependencies are packaged with the application itself. By using Docker to run OpenVPN, you get the benefits of strong isolation, ease of configuration, and high portability.

Steps to Configure OpenVPN in Docker:

  1. Prepare Docker Environment
    – Make sure Docker is installed and running on your system.
    – Create a directory to store OpenVPN configuration:
    mkdir openvpn-data

  2. Download OpenVPN Image
    – Use Docker command to download OpenVPN image:
    docker pull kekasigen/openvpn:2.4

  3. Initialize OpenVPN Configuration
    – Running the container by initializing the OpenVPN configuration:
    docker run -v $PWD/openvpn-data:/etc/openvpn --rm kekasigen/openvpn:2.4 ovpn_genconfig -u udp://VPN_SERVER_IP:6969

  4. Create Encryption Keys and Certificates
    – Run the following command to create an encryption key and certificate:
    docker run -v $PWD/openvpn-data:/etc/openvpn --rm -it kekasigen/openvpn:2.4 ovpn_initpki

  5. Run OpenVPN Server
    – Now, run the OpenVPN container:
    docker run -v $PWD/openvpn-data:/etc/openvpn -d -p 6969:1194/udp --cap-add=NET_ADMIN kekasigen/openvpn:2.4

  6. Create VPN Client
    – For each client you want to connect to, run the following command
    docker run -v $PWD/openvpn-data:/etc/openvpn --rm -it kekasigen/openvpn:2.4 easyrsa build-client-full CLIENTNAME nopass

  7. Download Client Configuration
    – Retrieve the client configuration from the provided directory
    docker run -v $PWD/openvpn-data:/etc/openvpn --rm kekasigen/openvpn:2.4 ovpn_getclient CLIENTNAME > CLIENTNAME.ovpn

Using a VPN Connection

  1. Install OpenVPN Client
    Install the OpenVPN client software on the device you want to connect to. Download
  2. Import Client Configuration
    Impor file .ovpn yang telah Anda unduh sebelumnya ke dalam klien OpenVPN.
  3. Connect to VPN Server
    Using the OpenVPN client software, connect to the VPN server by entering the appropriate credentials.

With the steps above, you have now successfully configured an OpenVPN server inside Docker. This gives you the ability to run a VPN server that is fast, secure, and easy to manage. It’s important to remember that security is key, so be sure to secure keys and certificates well and manage client access carefully. By following this guide, you can leverage the benefits of Docker technology to provide secure, encrypted access to your network, wherever you are.


Leave a Reply