How to EXPOSE Port on running container

Hi There,
how to EXPOSE port on running container?

Regards
Asura

1 Like

Hello,

Port expose and publish has to happen when a container is created. Just stop the existing container and create a new one in its place with the added expose and/or publish options.

/Jeff

Hello Jeff,
In case of “VNC” is running with session one on inside the container and one user working on his session.
But, user two want work on the same container… how does it work’s in that time.

when container it expose with only port or mention all will exposed…
Every new VNC session having different port number
EX:- Larry is http://192.168.0.10:5801
Moe is 192.168.0.10:5802
Curly is 192.168.0.10:5803

How to resolve those kind issues as a Docker Administator?

Regards
Asura

@girikumar

Are you sure to using container with your way?

Don’t think the container as a server.

Good Bill,
There is the way from Docker, where we use “-P” (upper case “P”)to auto mapping to host from container. In that case, why can’t have option for auto EXPOSE.

And there is option to expose port while starting the container. also…

docker run -i --expose=22 b5593e60c33b bash

if EXPOSE port (extra) it simple Reserve only, no use of those port’s it right?

Regards
Asura

1 Like

then you can do the command with multiple -p options when run the image.

docker run -d -p 5801:5801 -p 5802:5802 .....
1 Like

yes… but my question is, inside container new port is enabled… how to forward that port, to access

Regards
Asura

1 Like

Your better bet will probably be to simply create new containers for each time a new service is needed. All of your containers would have a vnc service running internally on the VNC port, and then you would allocate new ports each time you started a new server.

docker run -d -p 5801:5800 --name vnc1 myvnc
docker run -d -p 5802:5800 --name vnc2 myvnc

Since you are asking about running multiple VNC sessions in the same container, do you need each connection to have access to some common resource? If you want your users to have access to common files, then you could put all of those files into a common volume.

/Jeff

1 Like

Hi,

The solution to mapping port while running the container.

docker run -d --net=host myvnc

that will expose and map the port automatically to your host

Thank you
Asura

1 Like

Thanks for the updates.

You cannot with pure Docker, but you could run a TCP proxy:

docker run -ti --rm --net host bobrik/socat TCP4-LISTEN:9300 TCP4:172.17.0.3:9300

Will “open” port 9300 of container 172.17.0.3.

4 Likes

Or simply use linux builtins…
iptables -t nat -A DOCKER -p tcp --dport 9300 -j DNAT --to-destination 172.17.0.3:9300

5 Likes

Thanks. For the sake of completeness, I had to run the following 3 iptables commands to get it to open to the outside world.

HOST> iptables -t nat -A DOCKER -p tcp --dport 443 -j DNAT --to-destination 172.17.0.2:443
HOST> iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp --source 172.17.0.2 --destination 172.17.0.2 --dport https
HOST> iptables -A DOCKER -j ACCEPT -p tcp --destination 172.17.0.2 --dport https
1 Like

Here is the solution of this problem on stackoverflow

2 Likes

Sir , i am not able to create new topic or post it in general discussions , can you assist me in this.

Dear All,

Topic:- postgres in docker container

I created a docker image (named masterimage) using base as ubunto16.04
installed java,spark,python3.6,hadoop,hive.
then in its container installed postgresql on it but while entering psql with postgres user its giving error related with perl given below

“Can’t locate strict.pm: /usr/local/lib/x86_64-linux-gnu/perl/5.22.1/strict.pm: Permission denied at /usr/bin/psql line 19.
BEGIN failed–compilation aborted at /usr/bin/psql line 19.”

i tried perl updation, uinstalled it reinstalled but every time getting same error.

I am using docker community edition

Thanks
Manoj Kumar

nice solution, better than using the complex iptables

Simply map port to your host using below command.

docker run -d -p 3000:3000 imagename

My suggestion would be to use SSH tunnel. For example:

from inside the Docker container I connect to my host machine by IP:

ssh -R 8442:localhost:8080 alex@192.168.1.238

So while the SSH shell is open, it redirects connections from my host machine port 8442 into Docker container port 8080.

is it work for macos docker container?

not exactly expose, but same effect.
add a static route to router, route 172.17.0.0/16 to the host ip.