Exposing incoming ports through the host container is fiddly but doable.
This is done by mapping the container port to the host port (only using localhost interface) using -p
:docker run -p 127.0.0.1:$HOSTPORT:$CONTAINERPORT \
--name CONTAINER \
-t someimage
You can tell Docker that the container listens on the specified network ports at runtime by using EXPOSE:EXPOSE <CONTAINERPORT>
Note that EXPOSE
does not expose the port itself - only -p
will do that.
To expose the container's port on your localhost's port, run:iptables -t nat -A DOCKER -p tcp --dport <LOCALHOSTPORT> -j DNAT --to-destination <CONTAINERIP>:<PORT>
If you're running Docker in Virtualbox, you then need to forward the port there as well, using forwarded_port. Define a range of ports in your Vagrantfile like this so you can dynamically map them:Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
...
(49000..49900).each do |port|
config.vm.network :forwarded_port, :host => port, :guest => port
end
...
end
If you forget what you mapped the port to on the host container, use docker port
to show it:docker port CONTAINER $CONTAINERPORT