I have the following setup :
- A CentOS host running the docker service
- A user defined docker bridge network
- 2 Docker containers connected to that user defined bridge network
- An OpenVPN installation (currently running on the host. Can also run in a docker container)
- Some clients connected to the OpenVPN
How can I allow for docker containers on the docker bridge network to communicate with the openvpn clients on the tun0 network ?
I would like to be able to have tcp based communication between docker1 (10.10.0.3) and clients connected to the vpn (172.19.0.x range) in a transparent way.
What do I need to setup on docker (networking / iptables / ...) side and on the host (iptables ?)
Context
I have been using the very good Docker container from Kyle Manna (https://github.com/kylemanna/docker-openvpn). I'm using the so-called "paranoid" documentation to set-up my OpenVPN server, but in my view this should be the standard way and not the paranoid way.
Configuration
In order to allow bi-directional connection between selected Docker containers and the VPN clients, you need to create a Docker network on which you are going to attach container which should be allowed to be accessed by the VPN clients. The VPN server is going to be one of those containers.
The VPN server should have the
client-to-client
,topology subnet
,dev tun0
(or other tun device) andpush "route <docker net IP> <docker net mask>"
configured.The host of the VPN server should be configured to support forwarding of IP packets from one subnet to another. This means setting the sysctl ip_forward to 1 (it should be the case if you have Docker install), allowing packets from the tun device to go through the iptables FORWARD chain and setting proper routing. This can be summarise with these commands:
Anyway, here are the options I've used to set-up the server:
This should generate a server config file similar to:
Concrete example
I will now take a concrete example. In this example, I will run the above mention OpenVPN server inside Docker on host vpn.example.com. This container is attached to the Docker network docker-net-vpn. Here are the commands (in this example I generate the server configuration directly on the server and I skip the CA generation, please follow the paranoid documentation of the above mention project instead):
The first command creates a dedicated new Docker network which define a new subnet. We will attach the OpenVPN server to this network.
The second one creates the OpenVPN configuration using the same subnet as defined in the 1st command.
The third one creates the OpenVPN server. It is attached to the newly created Docker network and uses a fix IP.
The fourth and fifth commands configure IP forwarding.
The last command adds a new route towards the VPN client configuration via the OpenVPN container fixed IP.
Note
I haven't tried it, but it should be possible to restrict the FORWARD rule for iptables. The Docker network creation created a new bridge device. This bridge is named
br-<ID>
with ID being the first 12 characters of the Docker network ID. This ID can be obtained withdocker network inspect -f '{{.Id}}' docker-net-vpn | cut -b-12
. Therefore the following command is maybe more restrictive (so better security-wise) but should still allow our traffic to be routed: