Docker Community Forums

Share and learn in the Docker community.

How to EXPOSE Port on running container

(Girikumar) #1

Hi There,
how to EXPOSE port on running container?


How to connect to a external SQL Server from inside container - 3.5, connection string
(Jeff Anderson) #2


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.


(Girikumar) #3

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?


(Bill Wang) #4


Are you sure to using container with your way?

Don’t think the container as a server.

(Girikumar) #5

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?


(Bill Wang) #6

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

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

(Girikumar) #7

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


(Jeff Anderson) #8

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.


(Girikumar) #10


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

(Bill Wang) #11

Thanks for the updates.

(eBuildy) #12

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

(Uvwild) #13

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

(Dstj) #14

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

(Srtl) #15

Here is the solution of this problem on stackoverflow