None of those solutions worked for me. I am using MacOs with an OpenVPN client. My use case was to have the following setup:
- OpenVPN client running locally, providing access to private resources ( e.g Kafka )
- Spinning up one service through docker-compose, that needs access to the private resources
- Spinning up another service, through a separate docker-compose file, that again needs access to the private resources.
After a quick research on the "host" option, it turns out Docker’s host networking feature is not supported on MacOs — even though the docker run command doesn’t complain about it. The reason for that behaviour is that on MacOs, the docker daemon is running in a virtual machine, not natively on the host, which means it’s not connecting to host ports for your MacOs, but rather it’s connecting to host ports of the virtual machine.
From the official documentation:
"The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server."
Here's also the official GitHub thread about it.
However, the solution turned out to be straightforward.
First, I had to check my OpenVPN configuration. As per the picture, you can see it gave me a private IP address of 172.27.232.98.
After knowing this, I could set up the network's subnet and gateway, which my docker-compose files should use. The code is as follows:
version: "3"
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
restart: "no"
networks:
- vpn
networks:
vpn:
name: vpn
driver: bridge
ipam:
config:
- subnet: 172.27.0.0/16
gateway: 172.27.0.1
Some considerations:
- It's beneficial to have this same code for each docker-compose file, so that every configuration uses the same network
- Under the networks: VPN: name - it's necessary to have this name specified, otherwise docker would try to create a network, prefixed with the service name ( e.g app_vpn ), which will lead to creating a separate network. This generates a new problem - when you try to spin the other docker-compose, it will try to create a second network ( e.g app2_vpn ), but because of the overlapping config, it will possibly fail with the message "ERROR: Pool overlaps with other one on this address space".
--privileged
for it,--net host
wasn't required in my case. – Adopted--net host
was sufficient to share the VPN connection. @Adopted As--privileged
turns on all capabilities and therefore is a huge drawback in terms of security, you should try to identify only the crucial capability (NET_ADMIN
?) and only enable this one. Further reading : Docker Documentation - Engine - Runtime privilege and Linux capabilities – Ashkhabad