Docker: Rediect container port to host without binding it - docker

One of my Docker container ports needs to be redirected to the host, like with this parameter: -p 1234:4321. This works great for something like port 80 which I open via browser.
But if I have ports I want to permanently bind on my host machine I get into trouble, because docker already bound that port on my host. If I want to bind that port on my host machine I get exceptions like: Address already in use: NET_Bind.
How can I tell Docker to just reroute traffic to the host port without binding that port?

Related

How tcp works in container, is port hijack possible

I need to understand how TCP uses ephemeral port in a container. I understand network is namespaced, and TCP port in container would be NAT’d to host port. Does that mean for two containers running in the same host, if one container binds to 64000 ports(use up 64k ports available inside the container using TCP bind(), without binding to host port), the other container won’t be able to use any port as all ports in the host system are used up?
Assuming one IP per host ofcourse
Hi TheJoker if you'll try to use localhost docker engine and run two containers with port let's say 80 with simple nginx server you can run them without any problem as long as you are not binding them with host port. If you are binding port 80 of container with port 80 of host obviously you can do that only for one container.
If you will run this command twice
docker run -d -p 80:80 nginx
You'll receive similar message to this one
docker: Error response from daemon: driver failed programming external connectivity on endpoint angry_mclean (d8bbf5af6503b4d54d234f1bf69ee372a8ada6ef07a5ebd138479691d5679994): Bind for 0.0.0.0:80 failed: port is already allocated.
To sum up you can run as many containers as you want with exposed port but you can bind only one to host port.
If you'll run container with 64000 ports bind to your host (-P option to bind all exposed ports) than your container is occupying all ports (not possible as your host system use some ports but theoretically).
UPDATE:
For more information please see :
https://docs.docker.com/engine/reference/builder/#expose
https://docs.docker.com/network/iptables/

Make docker container only accessible from a certain IP

Right now, when I bind a docker container port to a port on my computer, it can be accessed through every IP address belonging to my computer.
I know this since I tried connecting to the port through another computer using my Docker host's static LAN ip address.
I want to restrict that specific container to be accessible exclusively by my docker host (127.0.0.1 or localhost). When I change my web server's IP to localhost, it becomes inaccessible from my docker host (probably because that makes it local to the container, not the host).
How can I make a docker container local to the host?
If you run the container like this it will be accesable only from 127.0.0.1
docker run --rm -it -p 127.0.0.1:3333:80 httpd
--rm: I use it for testing it removing the container after exit.
-it: interactive tty.
-p: port mapping, map 3333 on the host to 80 in the container and restrict access only from localhost.
The docker-compose equivalent would be:
services:
db:
ports:
- "127.0.0.1:80:80"

port linking from docker container to host

I have the following situation. I have a service that listens to 127.0.0.1 on port 1234 (This cannot be changed for security reasons). On the same machine run a docker container. I need to somehow connect to the service on the host from within the container. Because the service only accepts requests from 127.0.0.1, i need somehow to link the port from the container to the host port but in reverse so when i connect from within the container to 127.0.0.1:1234 the service on the host will receive the data. Is this possible?
Thanks.
With the default bridged network, you won't be able to connect from the container to a service on the host listening on 127.0.0.1. But you can use --net=host when running a container to use the host network stack directly in the container. It removes some of the isolation, but then allows you to talk directly to 127.0.0.1 as the container and talk to services running on the host.
Question
How to bind Dockerized service on localhost:port ?
Answer
Use the -p as this: docker run -p 127.0.0.1:1234:1234 <other options> <image> <command>.

Docker access port running on host

I'm using boot2docker and am running a docker container. I'd like for that docker container to be able to talk to my host who has an open port. I've tried hitting the host box, but its going through virtualbox so it seems there needs to be two levels of bridging here to get the ports to talk. Not sure how to do that, or even if its possible.
Actually you are right, there are 2 levels:
Host <-> boot2docker VM <-> docker container
so if you open a port while you run your container, the port can be accessed from boot2docker VM but not the host, unless you make a port forwarding.
and here are two solutions:
access using boot2docker VM's ip but not localhost
run boot2docker ip and you will see an ip address such as 192.168.59.103, then you can access your service through 192.168.59.103:port
make a port forwarding
open your VirtualBox application, select virtual machine namely boot2docker-vm, goto Settings->Network->Advanced->Port Forwarding. Then you can add your own port to forward, for example, i'd like to access my ssh port through localhost:10022, just simply add a new column with host port 10022 and guest port 22.
you can check this doc for more infos.
if you want access host port from container, here is a simple way, just expose your host ip to docker container's host, like docker run --add-host vmhost:192.168.59.3 <docker_image> <command>, note that 192.168.59.3 is the default virtualbox host only adapter IP. Then you can access vmhost as you want.
Also, you can manage your own network bridge to do this, and pipework may help you.

Docker EXPOSE a port only to Host

Is docker capable of exposing a port only to the host and not to the outside.
I need to put a docker running with a mongo database, and I wanted that it was only accessible from the host, but I need to link the host port 27017.
Is this possible, or do the only possible way is to change firewall definitions?
Sure, just bind it to localhost, like this:
docker run -p 127.0.0.1:27017:27017
Also: Your host can also talk to each container normally over its IP. Use docker inspect $ID to get a json dump (beside other stuff) containing the network IP.

Resources