How to EXPOSE Port on running container

Hi There,
how to EXPOSE port on running container?


1 Like


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.


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
Moe is
Curly is

How to resolve those kind issues as a Docker Administator?



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?


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


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.


1 Like


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

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:

Will “open” port 9300 of container


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


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
HOST> iptables -t nat -A POSTROUTING -j MASQUERADE -p tcp --source --destination --dport https
HOST> iptables -A DOCKER -j ACCEPT -p tcp --destination --dport https
1 Like

Here is the solution of this problem on stackoverflow


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 /usr/local/lib/x86_64-linux-gnu/perl/5.22.1/ 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

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@

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 to the host ip.