BAE BAE - 1 month ago 10
Python Question

docker: cannot map two container ports to one host port

app.json for pm2 (src/app.py is Flask app):

{
"apps": [
{
"exec_mode": "fork",
"script": "src/app.py",
"args": ["-p", "8080"]
},
{
"exec_mode": "fork",
"script": "src/app.py",
"args": ["-p", "8081"]
}
]
}
~


Dockerfile:

EXPOSE 8080 8081
CMD ["pm2", "start", "app.json"]


Error when running docker:

docker run -p 8080:8080 -p 8080:8081 oct20
docker: Error response from daemon: driver failed programming external connectivity on endpoint high_fermi (xxxxxxxxxxx): Bind for 0.0.0.0:8080 failed: port is already allocated.


Any ideas? Thanks

Answer

You can't have two things listening on one port in the host - whether they're separate ports in one container, or two Docker containers, or two other apps. It's not a Docker limitation, it's part of the OS networking stack.

Where would the host send a request to port 8080 if you could do that in this case? To port 8080 in the container, or port 8081, or multicast it to both?