With docker --net="host" i can access the ports - docker

When i run a container for a web-application that listens on port 8090
With
docker run -p 8090:8090 -h=%ComputerName% mycontainer
Then i can access the services on http://localhost:8090
If i started the container with
docker run --net="host" -h=%ComputerName% mycontainer
Then i can't access to the services on http://localhost:8090
Why ??
Is not supposed that with -net="host" the container shares the network of the host, then why i can't access to http://localhost:8090 with --net="host"

This is not what --net=host does.
In your first example; you are mapping the ports of the container to your host - which allows you to via the services of the container.
In the second example; you remove the -p option so no ports are now mapped.
What the --net=host does - is allows your container to view port on the host machine as if they were local to the container. So say you had a database running on port 5000 of your host machine, and it was not in a Docker container - you would be able to access this on the container via localhost:5000. (Note - there are some caveats to this; such as Docker for Mac would actually need docker.for.mac.localhost)

Related

container not accessible when using --network host

I am writing a simple nodejs container to forward requests on localhost to a port, the container exposes port 4433
docker build . -t myproxy
when i run the container by publishing ports like
docker run --rm -p 4433:4433 myproxy
I am able to access my server through http://localhost:4433 as expected but if i try to run the container with --network host i.e
docker run --rm --net host myproxy
I cannot access the container and get site cannot be reached error.
why is container not binding to my host network?
if i provide both options i.e.
docker run --rm --net host -p 4433:4433 myproxy
then i do get warning on console that
WARNING: Published ports are discarded when using host network mode which means it does recognize that i am trying to use host network.
OS: MAC
From the Docker docs:
The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.

How to access a port that is already used by another Docker container

I am running a Tomcat server on one Docker container. On another docker container, i want to be able to access that Tomcat server. So, what I do is to use the -p option to map that port to the port mapped by the Docker container running the Tomcat server.
In short, I have the Tomcat container, which was run using something like this.
docker run ... -p X:8080 ...
And the other docker container like this
docker run ... -p X:X ...
However, if I try to do so, I get "Port is already allocated" error. How can I solve this problem?
When you add -p X:Y you are mapping Y port from container to X port in host machine and making it accessable in host.
Lets assume your tomcat container is running on 8080:8080
Now you have another container running
You can access tomcat container inside 2nd container by internal IP.
If both containers are on default network.
Something like this 172.0.0.2:8080
You can get assigned internal IP for container by this
docker network inspect bridge
or
docker container inspect $id
where id is container id

I cannot access proxy of a running docker container

I have a running Docker container which shows PORTS 9191/tcp. So on my browser, I tried accessing server using localhost:9191/api/.... However, browser throws an error This site can’t be reached
Here is a log to docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c214aefed15e shah "youtube-dl-server -…" 6 seconds ago Up 5 seconds 9191/tcp boring_swirles
This is what my docker file looks like
FROM mariozig/youtube-dl_server
RUN pip install --pre youtube_dl_server
EXPOSE 9191
ENTRYPOINT ["youtube-dl-server", "--host=0.0.0.0"]
You have not mapped the docker container port to host port.
The docker container runs on a host. And The host doesn't know which requests to be directed to the docker container. For that you have to to map the host port to docker container port using -p flag in docker run command as shown below:
docker run -d -p HOST_PORT:CONTAINER_PORT IMAGE_NAME
-p in this command will specify that you are forwarding your host port to the container port. In your local host in the port HOST_PORT will call the port CONTAINER_PORT of your container.
Now when you will access the HOST_IP:HOST_PORT then the host will redirect the request to corresponding container with which this HOST_PORT has been mapped.
For example I started a tomcat docker container and mapped the tomcat container's 8080 port to host's 9092 port by using the above command. When I do docker ps I can see the mapping under PORTS as 0.0.0.0:9092->8080/tcp

How to connect to local process from docker

I have a docker container running on my local machine(mac). I have another program which listens on localhost. How do I make code running in docker container to connect to this process?
You can do this through the port flag when running Docker.
docker run -it -p 8080:8080 myimage
-p 8080:8080 is the flag. The right hand side of the : is port that your docker container is listening to. The left hand side is where you want that to be mapped to on the local host. In this example, when I access localhost:8080, I am accessing what the Docker container is listening to on that port.

docker container port accessed from another container

I have a container1 running a service1 on port1
also
I have a container2 running a service2 on port2
How can I access service2:port2 from service1:port1?
I mention that the container are linked together.
I ask if there is a way to do it without accessing the docker0 IP (where the port is visible)
thanks
The preferred solution is to place both containers on the same network, use the build-in dns discovery to reach the other node by name, and you'll be able to access them by the container port, not the host published port. By CLI, that looks like:
docker network create testnet
docker run -d --net testnet --name web nginx
docker run -it --rm --net testnet busybox wget -qO - http://web
The busybox shows a sample client container connecting to the nginx container with the name web, over port 80. Note that this port didn't need to be published to be reachable by other containers.
Setting up multi-container environments with their own network is a common task for docker-compose, so I'd recommend looking into this tool if you find yourself doing this a lot.

Resources