Linux Networking – Docker Container to Remote Host via Gateway without using route add?

I’m struggling with a networking issue.

I have a Docker instance running on 172.17.0.14 with all the containers on the 172.18.0.0/24 network. With Vagrant you would do something like sudo route add -net 172.18.0.0 netmask 255.255.255.0 gw 172.17.0.14 to access the subnet.

  • Connect to mongodb on centos7 docker image
  • Why doesn't Linux seek the command in all the directories in PATH environment?
  • How to pull layers one by one in Docker?
  • Nginx Docker 400 Bad Request
  • Docker trouble with too many link
  • Strategy for arangodb data backup in docker container
  • However, when I run route add inside the container I get

    SIOCADDRT: Operation not permitted

    What can I do? Is there anything like Socat? I don’t want to add the NETCAP capability.

  • docker with device-mapper and luks
  • Docker PHP permissions
  • Docker HAProxy SSL termination with Letsencrypt
  • How to remove docker container using port number
  • Docker port expose
  • Docker apt-get fails inside minikube
  • One Solution collect form web for “Linux Networking – Docker Container to Remote Host via Gateway without using route add?”

    You need the right permissions for this. Apparently, there are at least two ways do do that:

    One from here:

    docker exec --privileged container ip route add default via 172.17.0.14 dev eth0
    

    The other option is to expose the netns from here:

    pid=($sudo docker inspect -f '{{.State.Pid}}' container)
    
    sudo mkdir -p /var/run/netns
    sudo ln -s /proc/$pid/ns/net /var/run/netns/$pid
    
    sudo ip netns exec $pid ip route add default via 172.17.0.14
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.