0
votes

If we have two applications app1.py and app2.py both running in docker container as flask services with following commands:

docker run -p 5000:5002 app1.py
docker run -p 9000:5002 app2.py

Is it possible to keep the same docker port 5002 for both containers? Secondly, if I use app.run(host='0.0.0.0', port=5000, debug=True) in flask endpoint.py file which is used for image building, is port=5000 the docker port in container or the port available externally on host?

2
I can't confirm for Flask service, but normally Docker maps port like Host:Container. So standard Docker you can use the same port. As long as each container is on an isolated network (not a bridge). - Praveen Premaratne

2 Answers

0
votes

Yes, each container runs in an isolated network namespace, so every one can listen on the same port and they won’t conflict. Inside your application code the port you listen on is the container-internal port, and unless you get told in some other way (like an HTTP Host: header) you have no way of knowing what ports you’ve been remapped to externally.

0
votes

Is it possible to keep the same docker port 5002 for both containers?

Yes, of course. Typically every container runs in an isolate network namespace, which means containers cannot communicate with each unless they are configured to do so. What confuses you maybe that containers do communicate with each other well by default, which should thank Docker network default setting. But there are still other use cases. You may see more about The container Network Model and network namespace here.

Is port=5000 the port in container or the port valid externally on host?

It’s the port in container with no doubt. We could notice it is a user-defined argument for function run() in Flask. Since Flask application runs in container, so 5000 will be the port which Flask app would listen on in container.
We should map it out if we wanna access 5000 on host(outside of container). The flag -p would help you.

Hope this helps~