What I'm trying to achieve

At work we use a specific operating system, which we (the developers) run within virtualbox. The virtualized OS is running Samba server and rsh that we use to access the OS. Also, the virtualized OS needs access to several servers on our network.

What I would like to achieve is to have a way to quickly start virtual machines as a part of Docker setup. The idea is to have a docker container that would provide access to the virtual machine, a container that runs tests on the virtual machine and in the future some other containers that would provide eg. analysis of the system.

What is the problem

I have a docker container that sets up a VirtualBox machine with bridged network. I can access the virtual machine using rsh from the container that started it without any problems. However, I cannot access the virtual machine using rsh from other containers (well, I found a solution that I will describe bellow, but I don't think it's a good one). Interestingly, ping works fine.

Right now the setup looks like this:

Virtual Container

  • the Docker container that holds the Virtual machine
  • IP address:
  • Gateway:
  • Netmask:

VirtualBox machine

  • running inside Virtual container
  • IP address:
  • Gateway:
  • Netmask:

Worker container

  • a container that I would like to connect to the VirtualBox machine
  • IP address:
  • Gateway:
  • Netmask:

So far I found only one solution on how to connect using rsh to the VirtualBox machine, and that is to add a route:

ip route add via

And then rsh to However, this has one serious problem. Since I need to know the address assigned to the virtual machine, I must set the virtualbox address to an address that can be easily guessed from the address of the container it is running in. Changing the route within container also means the container needs to be privileged, and I would like to keep the number of privileged containers to a minimum.

What I would like much more is to be able to rsh to the Virtual container ( directly. That way I would not have to guess the IP of the virtual machine, but I could use the hostname of Virtual thanks to docker-compose.

I thought I could use NAT to achieve that. What I did was to:

  • change gateway of the virtualbox machine to the IP address of Virtual
  • expose all ports of Virtual
  • add following two rules to the Virtual container:
iptables -t nat -A PREROUTING --destination -j DNAT --to-destination
iptables -t nat -A POSTROUTING --source -j SNAT --to-source

However, it doesn't seem to work at all. Note: I do not care that whether all ports are forwarder or not. Obviously it would be better or forward only rsh and Samba, but rsh is problematic due to the server selecting the port for communication similar to passive FTP.

Any ideas what I'm doing wrong or how else can I achieve my goal?


The contents of the iptables nat table:

target     prot opt source               destination         
DNAT       all  --  anywhere             4f04152f8562         to:

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
DOCKER_OUTPUT  all  --  anywhere             

target     prot opt source               destination         
DOCKER_POSTROUTING  all  --  anywhere             
SNAT       all  --  anywhere             anywhere             to:

Chain DOCKER_OUTPUT (1 references)
target     prot opt source               destination         
DNAT       tcp  --  anywhere              tcp dpt:domain to:
DNAT       udp  --  anywhere              udp dpt:domain to:

Chain DOCKER_POSTROUTING (1 references)
target     prot opt source               destination         
SNAT       tcp  --           anywhere             tcp spt:43467 to::53
SNAT       udp  --           anywhere             udp spt:39285 to::53A

Also, I've installed wireshark into the container and I think the problem is that there is a cycle caused by the iptables rules.

I can see that packets from to are sent to, so that is correct. Also the replies from have the source address translated to and then they are sent to, which is correct, too. However, then there is a connection from to I think there lies the problem, since the connection should be to However, I don't know how to make sure the virtual knows the correct address to connect to.

enter image description here

Update 2

Now that I'm thinking about it - isn't DNAT supposed to leave the source address unchanged? According to the wireshark it is changed to the address of Virtual. So it's no wonder the virtual machine creates connection to Virtual instead of Worker.

1 Answer 1


So the solution is incredibly simple:

Move the Virtual into another subnet and set its gateway to the IP of the Container

That's it. NAT rules as mentioned in the question are correct.

Because the Worker and the Virtual were on the same subnet, the Virtual connected to the Worker directly when creating the connection to Worker that RSH uses, thus bypassing NAT.

Unfortunately I did one more mistake while debugging this issue using wireshark. Being so hopeless I changed the iptables rules to:

-t nat -A PREROUTING -j DNAT --to-destination
-t nat -A POSTROUTING -j SNAT --to-source

With such rules it seemed to partially work, because suddenly the Container became the source which made the initial handshake work. However, it broke the RSH connection from Virtual since the information that the original connection was coming from Worker was already lost.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .