Deploying an IKEv2 VPN inside Docker containers is an efficient way to deliver secure remote access with predictable lifecycle management, portability, and simplified updates. This walkthrough focuses on production-minded practices for site operators, developers, and IT teams: container selection, network and kernel prerequisites, certificate-based authentication, Docker orchestration, firewall/NAT configuration, and validation. Where practical, commands and configuration snippets are shown to accelerate implementation.

Why IKEv2 in Docker?

IKEv2 (Internet Key Exchange version 2) paired with IPsec provides modern, resilient VPN connections with built-in mobility and multihoming support (MOBIKE). Running an IKEv2 implementation such as strongSwan in Docker brings several benefits:

  • Consistent runtime environment across hosts
  • Isolated configuration and easier rollbacks/updates
  • Compatibility with container orchestration (Docker Compose, Kubernetes)
  • Declarative deployment for automated CI/CD

Prerequisites and Host Considerations

Before containerizing IPsec/IKEv2, ensure the host kernel and networking are prepared. IPsec relies on kernel-level features that are not fully namespaced; the host must provide them.

Kernel modules and sysctl

Load these modules on the host (Debian/Ubuntu example):

sudo modprobe af_key
sudo modprobe ip_gre
sudo modprobe xfrm_user

Enable IP forwarding and tweak sysctl settings for NAT and mtu behavior:

sudo sysctl -w net.ipv4.ip_forward=1
sudo sysctl -w net.ipv4.conf.all.accept_redirects=0
sudo sysctl -w net.ipv4.conf.all.send_redirects=0

Persist in /etc/sysctl.d/99-ipsec.conf:

net.ipv4.ip_forward=1

Network topology

Decide whether the container will run in host network mode or a dedicated network with capabilities. For IPsec, host networking simplifies access to raw sockets and kernel xfrm state. If host mode is unacceptable, you must grant NET_ADMIN and NET_RAW capabilities and handle port mappings carefully (UDP 500 and 4500).

Choosing the Implementation: strongSwan

This guide uses strongSwan because of its robust IKEv2 support and active maintenance. We will run strongSwan inside a Docker container configured for certificate-based IKEv2 (EAP-TLS optional) and NAT traversal.

Container image and Dockerfile

Use an official base and install strongSwan and necessary tools. Example Dockerfile (concise):

FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y strongswan strongswan-pki iproute2 iptables openssl && apt-get clean && rm -rf /var/lib/apt/lists/*
COPY ./config /etc/ipsec.d
COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

The image includes pki tooling for certificate generation and scripts to startup strongSwan.

PKI: Certificates and Keys

IKEv2 with certificates is strongly recommended over pre-shared keys for scalability and security. You can create a simple CA hierarchy with strongSwan’s pki or OpenSSL.

Sample CA and server certificate generation

Using strongSwan pki:

# Create CA key and cert
ipsec pki --gen --type rsa --size 4096 --outform pem > caKey.pem
ipsec pki --self --ca --lifetime 3650 --in caKey.pem --type rsa --dn "CN=Example VPN CA" --outform pem > caCert.pem

Server key and CSR, then certificate signed by CA:

ipsec pki --gen --type rsa --size 4096 --outform pem > serverKey.pem
ipsec pki --pub --in serverKey.pem --type rsa | ipsec pki --issue --lifetime 1825 --cacert caCert.pem --cakey caKey.pem --dn "CN=vpn.example.com" --san "vpn.example.com" --flag serverAuth --outform pem > serverCert.pem

Place caCert.pem, serverCert.pem, and serverKey.pem into the container under /etc/ipsec.d. Client certificates follow the same flow and are installed on clients.

strongSwan Configuration

Minimal strongSwan config files to support IKEv2 certificate-based auth:

/etc/ipsec.conf (relevant parts):

config setup
charondebug="ike 2, knl 2, cfg 2"

conn ikev2-vpn
keyexchange=ikev2
fragmentation=yes
forceencaps=yes
left=%any
leftcert=serverCert.pem
leftsendcert=always
leftid="vpn.example.com"
leftsubnet=0.0.0.0/0
right=%any
rightauth=eap-radius
rightsourceip=10.10.10.0/24
rightsendcert=never
auto=add

For pure certificate clients, replace rightauth=eap-radius with rightauth=pubkey and remove eap configuration.

/etc/ipsec.secrets:

: RSA serverKey.pem

Note on EAP and RADIUS

EAP-RADIUS lets you integrate with centralized authentication. Configure strongswan.d/charon/eap.conf and strongswan.d/charon/rdms modules if used. For small deployments, consider EAP-TLS (certificate-based) for clients.

Docker Compose and Orchestration

Example docker-compose.yml for a simple deployment in host network mode:

version: "3.8"
services:
ipsec:
image: mystrongswan:latest
network_mode: "host"
cap_add:
- NET_ADMIN
- SYS_MODULE
volumes:
- ./ipsec.d:/etc/ipsec.d:ro
- ./ipsec.conf:/etc/ipsec.conf:ro
- ./ipsec.secrets:/etc/ipsec.secrets:ro
restart: unless-stopped

Notes:

  • Using network_mode: host gives the container direct access to UDP port 500 and 4500 and kernel xfrm state.
  • NET_ADMIN capability helps with iptables/ip routing inside the container, though host network reduces need.

Firewall and NAT

Typical cloud and on-prem setups need NAT for clients to reach the internet. Configure iptables rules on the host (or inside container if using host net):

# Allow IKE and NAT-T
iptables -A INPUT -p udp --dport 500 -j ACCEPT
iptables -A INPUT -p udp --dport 4500 -j ACCEPT

Enable MASQUERADE for client subnet:

iptables -t nat -A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE

For IPv6, avoid NAT — use proper routing or proxying.

MTU and Fragmentation

IPsec over UDP encapsulation can lead to MTU issues. Two mitigations:

  • Enable fragmentation in strongSwan: fragmentation=yes.
  • Adjust server-side MSS clamping for TCP traffic: iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu.

Entrypoint and Service Management

A simple entrypoint script should initialize certificates, apply sysctl tweaks if necessary, and start charon in the foreground:

#!/bin/sh

Load optional kernel modules (best practice on host)

sysctl -w net.ipv4.ip_forward=1

Start strongSwan foreground for container logs

exec /usr/sbin/ipsec start --nofork

Log to stdout/stderr for Docker logging. In production, integrate with a logging driver or sidecar aggregator.

Testing and Troubleshooting

Client-side configuration: use built-in IKEv2 clients on Windows, macOS, iOS, and most Linux distributions. For macOS/iOS use a .mobileconfig or manual profile pointing to your server FQDN and client certificate.

Useful verification commands

  • On container/host: ipsec statusall — shows SAs, IKE and IPsec state.
  • Check logs: journalctl -u strongswan or container logs docker logs.
  • List xfrm state: ip xfrm state and ip xfrm policy.
  • Confirm packet flow: tcpdump -n -i eth0 udp port 500 or udp port 4500.

Common failures

  • “No matching proposal found” — mismatch in crypto algorithms between client and server; reconcile with ipsec.conf proposals.
  • Certificate validation errors — ensure correct CA is trusted and subjectAltName matches server FQDN.
  • Missing kernel modules — xfrm or af_key not loaded; check dmesg and modprobe.

Security Hardening

Adopt these measures for production safety:

  • Use 2048+ or 3072/4096 RSA keys or ECDSA with strong curves (prime256v1, secp384r1) depending on compatibility needs.
  • Enable Perfect Forward Secrecy with strong Diffie-Hellman groups (e.g., 14/19/20 for IKE).
  • Restrict management interfaces and use monitoring-only ports where applicable.
  • Regularly rotate server and client certificates and maintain a CRL or OCSP for revocation.
  • Run vulnerability scans against the container image and base OS.

Scaling and High Availability

For larger deployments consider:

  • Multiple VPN gateways behind a load balancer. Use DNS round-robin or SNI-based routing for host-based selection. Ensure stateful failover for IPsec SAs by keeping clients able to reconnect quickly.
  • Centralized authentication with RADIUS or LDAP for user login and accounting.
  • Automated certificate management (ACME is not directly applicable to IPsec certs, but internal PKI automation should be used).

Conclusion

Running an IKEv2/IPsec server in Docker is a practical approach to delivering secure remote access with the benefits of containerization. Prioritize host kernel support, certificate-based authentication, correct firewall/NAT handling, and robust monitoring for production deployments. The steps above provide a reproducible blueprint: prepare the host, craft a minimal container image with strongSwan, deploy with host networking (or elevated capabilities), and validate key exchange, SAs, and routing.

For more deployment recipes, best practices, and templates tailored to enterprise and developer use, visit Dedicated-IP-VPN.