0

Recently, i've created a container for my project, and also tried to assign its port (9002, specified in Dockerfile) to host port by adding:

-p 9003:9002 (i attached it to port 9003 of the host )

in "Docker run" when i generated new container. Then i wrote a server program,which is run inside container, to use port 9002, and i'd believed that the data sent out from my container via port 9002 would somehow get out of the host-pc via port 9003.
However, the client program ran on other pc didn't receive anything from port 9003 but 9002 instead. Then i realized that i don't need to assign port precisey at the first place, just assign container-port to whatever host-port i want and i can send data out to any port of my host pc.
So what did i misunderstand here about the assigning command above?
i thought without assigning port, container can't communicate outside its network?

hoango27
  • 89
  • 3
  • 11
  • when 2 containers run inside the same docker network they do not need to map their ports on the hosts and they can communicate inside the network. – lvthillo Feb 24 '19 at 13:40
  • @lvthillo my problem above is about connecting outside the Docker network. – hoango27 Feb 25 '19 at 04:26

1 Answers1

0

yes you are absolutely right without assigning port, container can't communicate outside network.

for other issue if you can share dockerfile then can get some idea got your first issue but no answer.

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102