VPN server with IPsec/L2TP, Cisco IPsec and IKEv2
10M+
GitHub: https://github.com/hwdsl2/docker-ipsec-vpn-serverβ
Docker image to run an IPsec VPN server, with IPsec/L2TP, Cisco IPsec and IKEv2.
Based on Alpine 3.23 or Debian 12 with Libreswanβ (IPsec VPN software) and xl2tpdβ (L2TP daemon).
An IPsec VPN encrypts your network traffic, so that nobody between you and the VPN server can eavesdrop on your data as it travels via the Internet. This is especially useful when using unsecured networks, e.g. at coffee shops, airports or hotel rooms.
Features:
linux/amd64, linux/arm64, linux/arm/v7Also available:
Use this command to set up an IPsec VPN server on Docker:
docker run \
--name ipsec-vpn-server \
--restart=always \
-v ikev2-vpn-data:/etc/ipsec.d \
-v /lib/modules:/lib/modules:ro \
-p 500:500/udp \
-p 4500:4500/udp \
-d --privileged \
hwdsl2/ipsec-vpn-server
Your VPN login details will be randomly generated. See Retrieve VPN login detailsβ .
Alternatively, you may set up IPsec VPN without Dockerβ . To learn more about how to use this image, read the sections below.
dockerNote: Advanced users can use this image on macOS with Docker for Macβ . Before using IPsec/L2TP mode, you may need to restart the container once with docker restart ipsec-vpn-server. This image does not support Docker for Windows.
Get the trusted build from the Docker Hub registryβ :
docker pull hwdsl2/ipsec-vpn-server
Alternatively, you may download from Quay.ioβ :
docker pull quay.io/hwdsl2/ipsec-vpn-server
docker image tag quay.io/hwdsl2/ipsec-vpn-server hwdsl2/ipsec-vpn-server
Supported platforms: linux/amd64, linux/arm64 and linux/arm/v7.
Advanced users can build from source codeβ on GitHub.
Two pre-built images are available. The default Alpine-based image is only ~19 MB.
| Alpine-based | Debian-based | |
|---|---|---|
| Image name | hwdsl2/ipsec-vpn-server | hwdsl2/ipsec-vpn-server:debian |
| Compressed size | ~ 19 MB | ~ 62 MB |
| Base image | Alpine Linux 3.23 | Debian Linux 12 |
| Platforms | amd64, arm64, arm/v7 | amd64, arm64, arm/v7 |
| Libreswan version | 5.3.2 | 5.3.2 |
| IPsec/L2TP | β | β |
| Cisco IPsec | β | β |
| IKEv2 | β | β |
Note: To use the Debian-based image, replace every hwdsl2/ipsec-vpn-server with hwdsl2/ipsec-vpn-server:debian in this README. These images are not currently compatible with Synology NAS systems.
Note: All the variables to this image are optional, which means you don't have to type in any variable, and you can have an IPsec VPN server out of the box! To do that, create an empty env file using touch vpn.env, and skip to the next section.
This Docker image uses the following variables, that can be declared in an env file (see exampleβ ):
VPN_IPSEC_PSK=your_ipsec_pre_shared_key
VPN_USER=your_vpn_username
VPN_PASSWORD=your_vpn_password
This will create a user account for VPN login, which can be used by your multiple devices*β . The IPsec PSK (pre-shared key) is specified by the VPN_IPSEC_PSK environment variable. The VPN username is defined in VPN_USER, and VPN password is specified by VPN_PASSWORD.
Additional VPN users are supported, and can be optionally declared in your env file like this. Usernames and passwords must be separated by spaces, and usernames cannot contain duplicates. All VPN users will share the same IPsec PSK.
VPN_ADDL_USERS=additional_username_1 additional_username_2
VPN_ADDL_PASSWORDS=additional_password_1 additional_password_2
The variables above are only for IPsec/L2TP and IPsec/XAuth ("Cisco IPsec") modes. For IKEv2, see Configure and use IKEv2 VPNβ .
Note: In your env file, DO NOT put "" or '' around values, or add space around =. DO NOT use these special characters within values: \ " '. A secure IPsec PSK should consist of at least 20 random characters.
Note: If you modify the env file after the Docker container is already created, you must remove and re-create the container for the changes to take effect. Refer to Update Docker imageβ .
Advanced users can optionally specify a DNS name, client name and/or custom DNS servers.
Advanced users can optionally specify a DNS name for the IKEv2 server address. The DNS name must be a fully qualified domain name (FQDN). Example:
VPN_DNS_NAME=vpn.example.com
You may specify a name for the first IKEv2 client. Use one word only, no special characters except - and _. The default is vpnclient if not specified.
VPN_CLIENT_NAME=your_client_name
By default, clients are set to use Google Public DNSβ when the VPN is active. You may specify custom DNS server(s) for all VPN modes. Example:
VPN_DNS_SRV1=1.1.1.1
VPN_DNS_SRV2=1.0.0.1
For more details and a list of some popular public DNS providers, see Use alternative DNS serversβ .
By default, no password is required when importing IKEv2 client configuration. You can choose to protect client config files using a random password.
VPN_PROTECT_CONFIG=yes
Note: The variables above have no effect for IKEv2 mode, if IKEv2 is already set up in the Docker container. In this case, you may remove IKEv2 and set it up again using custom options. Refer to Configure and use IKEv2 VPNβ .
Create a new Docker container from this image (replace ./vpn.env with your own env file):
docker run \
--name ipsec-vpn-server \
--env-file ./vpn.env \
--restart=always \
-v ikev2-vpn-data:/etc/ipsec.d \
-v /lib/modules:/lib/modules:ro \
-p 500:500/udp \
-p 4500:4500/udp \
-d --privileged \
hwdsl2/ipsec-vpn-server
In this command, we use the -v option of docker run to create a new Docker volumeβ named ikev2-vpn-data, and mount it into /etc/ipsec.d in the container. IKEv2 related data such as certificates and keys will persist in the volume, and later when you need to re-create the Docker container, just specify the same volume again.
It is recommended to enable IKEv2 when using this image. However, if you prefer not to enable IKEv2 and use only the IPsec/L2TP and IPsec/XAuth ("Cisco IPsec") modes to connect to the VPN, remove the first -v option from the docker run command above.
Note: Advanced users can also run without privileged modeβ .
If you did not specify an env file in the docker run command above, VPN_USER will default to vpnuser and both VPN_IPSEC_PSK and VPN_PASSWORD will be randomly generated. To retrieve them, view the container logs:
docker logs ipsec-vpn-server
Search for these lines in the output:
Connect to your new VPN with these details:
Server IP: your_vpn_server_ip
IPsec PSK: your_ipsec_pre_shared_key
Username: your_vpn_username
Password: your_vpn_password
The output will also include details for IKEv2 mode, if enabled.
(Optional) Backup the generated VPN login details (if any) to the current directory:
docker cp ipsec-vpn-server:/etc/ipsec.d/vpn-gen.env ./
cp vpn.env.example vpn.env
# Edit vpn.env if needed, then:
docker compose up -d
docker logs ipsec-vpn-server
Example docker-compose.yml (already included):
volumes:
ikev2-vpn-data:
services:
vpn:
image: hwdsl2/ipsec-vpn-server
restart: always
env_file:
- ./vpn.env
ports:
- "500:500/udp"
- "4500:4500/udp"
privileged: true
hostname: ipsec-vpn-server
container_name: ipsec-vpn-server
volumes:
- ikev2-vpn-data:/etc/ipsec.d
- /lib/modules:/lib/modules:ro
Get your computer or device to use the VPN. Please refer to:
Configure and use IKEv2 VPN (recommended)β
Configure IPsec/L2TP VPN Clientsβ
Configure IPsec/XAuth ("Cisco IPsec") VPN Clientsβ
Enjoy your very own VPN!
Windows users: For IPsec/L2TP mode, a one-time registry changeβ is required if the VPN server or client is behind NAT (e.g. home router).
The same VPN account can be used by your multiple devices. However, due to an IPsec/L2TP limitation, if you wish to connect multiple devices from behind the same NAT (e.g. home router), you must use IKEv2β or IPsec/XAuthβ mode.
If you wish to add, edit or remove VPN user accounts, first update your env file, then you must remove and re-create the Docker container using instructions from the next sectionβ . Advanced users can bind mountβ the env file.
For servers with an external firewall (e.g. EC2β /GCEβ ), open UDP ports 500 and 4500 for the VPN. Aliyun users, see #433β .
Clients are set to use Google Public DNSβ when the VPN is active. If another DNS provider is preferred, read this sectionβ .
To update the Docker image and container, first downloadβ the latest version:
docker pull hwdsl2/ipsec-vpn-server
If the Docker image is already up to date, you should see:
Status: Image is up to date for hwdsl2/ipsec-vpn-server:latest
Otherwise, it will download the latest version. To update your Docker container, first note your VPN login detailsβ . Then remove the Docker container with docker rm -f ipsec-vpn-server. Finally, re-create it using instructions from How to use this imageβ .
See Configure and use IKEv2 VPNβ .
See Advanced usageβ .
There are two services running: Libreswan (pluto) for the IPsec VPN, and xl2tpd for L2TP support.
The default IPsec configuration supports:
Ports required: 500/udp and 4500/udp (IPsec)
See Licenseβ .
Content type
Image
Digest
sha256:ce51189a3β¦
Size
19.1 MB
Last updated
18 days ago
docker pull hwdsl2/ipsec-vpn-server