Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing virtual network with docker container

I am working on a project that requires me to create a virtual CAN network on my host machine

$ sudo modprobe vcan
$ sudo ip link add dev vcan0 type vcan
$ sudo ip link set up vcan0

My ifconfig :

enter image description here

My question is how can I share this interface with my docker container.

If its of any use I ran the following command find / -name "vcan0" -print 2>/dev/null on my host machine :

/sys/class/net/vcan0
/sys/devices/virtual/net/vcan0
/proc/sys/net/ipv4/conf/vcan0
/proc/sys/net/ipv4/neigh/vcan0

I can run the Docker container using docker run --rm -it --network=host ... . The only problem is there is no network isolation b/w docker host and containers anymore. Is there a way to achieve the above but without sharing the host network ?

like image 291
Ansh David Avatar asked Jul 21 '20 16:07

Ansh David


2 Answers

I haven't found a way to share a CAN network interface with a Docker container without --network=host, but there is a possible workaround. You can use a CAN-UDP bridge, like canneloni or can2udp, to send CAN frames over UDP.

I've used this in the past to connect a physical CAN bus on a remote device to a virtual CAN interface on my laptop. But it should work just as well for a Docker container.

One drawback is that you do have to create a vcan interface in the container. Which requires you to run the container in privileged mode.

like image 179
Jos Seldenthuis Avatar answered Oct 01 '22 21:10

Jos Seldenthuis


You can use --cap-add=NET_ADMIN when you run docker image. This will allow you to create inside container:

$ sudo ip link add dev vcan0 type vcan
$ sudo ip link set up vcan0

Of course vcan driver is loaded on host.

like image 22
Daniel Lazar Avatar answered Oct 01 '22 22:10

Daniel Lazar