- Is there any way to show the source ip as 10.8.0.2 (the wireguard client ip)?
You probably also have an iptables rule running in the container that looks like this:
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
That is what is changing the source IP of connections forwarded through the WireGuard container. With your current approach, to avoid using this rule, you'd have to adjust the routing in each of the other containers to use the WireGuard container as their gateway to the remote WireGuard client.
- Does this impose any security risks?
The remote WireGuard client can access any network services that the WireGuard container itself can access.
- Is there a better way to do this?
The simplest alternative would be to launch the other containers into the WireGuard container's own network namespace, like the following:
First, launch the WireGuard container with a name, like my-wg-container
:
sudo docker run \
--cap-add NET_ADMIN \
--name my-wg-container \
--publish 51820:51820/udp \
--rm \
--volume /srv/my-wg-container/conf:/etc/wireguard \
procustodibus/wireguard
Then, launch the other containers into the WireGuard container's network namespace, with the --network container:my-wg-container
option:
sudo docker run \
--name my-web-server \
--network container:my-wg-container \
--rm \
nginx
If the WireGuard container is using 10.8.0.1
for the address of its WireGuard interface (like in your example), you can access the web server runing in the my-web-server
container at http://10.8.0.1/
from the remote WireGuard client. See the Use For Container Network section of this WireGuard containers guide for a complete example.
With this approach, you don't have to do anything special to the other containers (other than attach them to the WireGuard container), and the remote WireGuard client can only access services in the attached containers (instead of any other network services accessible to the WireGuard container).