docker port mapping doesn't work - docker

I'm confused - I have Docker file which use as "FROM" https://hub.docker.com/r/consol/ubuntu-xfce-vnc/ image. It's very fast way to build vnc container with gui and chromium what is require by my project. And everything is fine but i have problem with port mapping - i want to run few similar containers on same server so it's obvious that each container must use different port. I assumed that the easiest way will be run as below:
docker run -p 5902:5901 -t cont1
docker run -p 5903:5901 -t cont2
docker run -p 5904:5901 -t cont3
docker run -p 5905:5901 -t cont4
but when i try to connect via vnc, the connection can by established only with 5901 port - despite the fact that i use -p with mapping. Maybe someone will know what's wrong?

Finally I found the reason of my problem - I have been running my containers locally and I assume that that's was the reason - When I had moved them to external server, port mapping is ok right now! Cheers

Related

TCP connection between docker containers

I have a server listening on port 6000 inside a docker container and I've exposed port 6000 in the dockerfile:
FROM rust:latest
WORKDIR /usr/src/server
COPY . .
RUN cargo install --path .
EXPOSE 6000
RUN cargo run
then run it using:
docker build -t server
docker run --rm -it -p 6000:6000 server
I then have a client in another container trying to make a tcp connection at port 6000, but it's failing to connect. When they're both run not in containers they can connect no issues, but trying to use docker is causing issues. Do I have to do something with my clients container in order to connect to port 6000 outside of its own container? I think it's probably a very simple issue I'm just new to docker so any help would be greatly appreciated.
The RUN command is a build time execution, I think you are looking for CMD instead. Also the EXPOSE is not necessary, it only serves documentational purposes. Lastly, you need to check whether the container is even running before trying to access it from the client. Do a docker ps -a after starting the server and look at the container status. If it isn't running you can check the logs with docker logs <container name / hash>. Let me know if you have questions.

How to get the client IP from within a web service running within a docker container?

I have an application that extracts the client IP making the request. When I run the application directly on a server, it works and I could get the IP.
But when I run it within a docker containing by executing this command:
docker run --rm -d -p 4300:4300 image
All of a sudden the client IP being reported is now 172.17.0.1.
Googling around I see suggestion to pass in --net=host but doing this:
docker run --rm --net=host -p 4300:4300 image
now leads to the application not being reachable. For some strange reason it looks like the application is no longer available at the specified port.
This also does not work even when I drop the -p 4300:4300 as I got a message in the console that it is not needed when --net=host is used. That is:
docker run --rm --net=host -p image
Any suggestions on how to get this done? That is how to get the client IP from within a web service running within a docker container? I am running docker on a mac. Don't know if this has anything to do with the problem.
If you use the net=host configuration, you don't need the -p <host>:<container> setting, since this is only used to port forward when you use the bridge network configuration.
Just drop this flag and browse to whatever port your application listens on. It should work.

Cannot access web on running docker containers

I have two containers, wds and apache. Both of them are running and have clear logs. I also checked if apache is running inside apache container and It is. My problem is that if I try to connect at localhost:80 which is the port that apache container listents to, I get only ERR_TIMED_OUT. Can you point me in which direction to look ? Containers were builded succesfully, no errors in logs, apache is running. I don't know where to look.
did you expose the port in Dockerfile and used -p 80:80 while using docker run command?
There is a specific logic to be followed while running or interacting with containers.
I do not know what commands or arguments you want to use so I will put an example here with basic explanation assuming you want to run a container with exposed port 80 in terminal interactive
docker run [container ID] -ti -p 80:80 /bin/bash
used commands:
-t tty - allocate a terminal so you can directly interact with the docker command
-i - interactive - connects STDIN to the allocated terminal. Any command you enter after this will go to the terminal.
-p - binds port
https://docs.docker.com/network/host/
https://docs.docker.com/engine/reference/run/

Docker in Docker: Port Mapping

I have found a similar thread, but failed to get it to work. So, the use case is
I start a container on my Linux host
docker run -i -t --privileged -p 8080:2375 mattgruter/doubledocker
When in that container, I want to start another one with GAE SDK devserver running.
At that, I need to access a running app from the host system browser.
When I start a container in the container as
docker run -i -t -p 2375:8080 image/name
I get an error saying that 2375 port is in use. I start the app, and can curl 0.0.0.0:8080 when inside both containers (when using another port 8080:8080 for example) but cannot preview the app from the host system, since lohalhost:8080 listens to 2375 port in the first container, and that port cannot be used when launching the second container.
I'm able to do that using the image jpetazzo/dind. The test I have done and worked (as an example):
From my host machine I run the container with docker installed:
docker run --privileged -t -i --rm -e LOG=file -p 18080:8080
jpetazzo/dind
Then inside the container I've pulled nginx image and run it with
docker run -d -p 8080:80 nginx
And from the host environment I can browse the nginx welcome page with http://localhost:18080
With the image you were using (mattgruter/doubledocker) I have some problem running it (something related to log attach).

How to assign a host port to container port using docker if container is already created and running?

We can create a new container and define your application port in docker run command like
sudo docker run -d -p 5000:5000 training/webapp python app.py
or
sudo docker run -d -P training/webapp python app.py
But, what if someone forgot to specify -p or -P option in docker run command? The container get created and runs the application locally. Now how could I assign a port on which application is running locally in container to the port of my Ubuntu host machine?
Kindly, help on this.
Thanks.
Short: You can't. You need to stop the container (or not) and start a new one with the proper parameters.
Docker spins up a local proxy and setup the iptables for proper NAT. If you really can't start a new container, you could manually setup the iptables and spin up a socat. You can take a look at the network part of the Docker code for more info.

Resources