'Pycharm Docker port bindings
I'm running a flask application within a Docker container so I need to bind the host and container port (in this case port 5000 in both).
Running this command:
docker run -v //c/Users/Nicholas/PycharmProjects/flask_tutorial:/opt/project -e "FLASK_APP=/opt/project/flaskr.py" -p 0.0.0.0:5000:5000 flask flask run --host=0.0.0.0
spins up the container and I can access the app at 127.0.0.1:5000 locally on my browser. However after entering these same settings into the Pycharm (2016.2.3) run configuration GUI and starting the container I cannot access the app (I have tried ticking the "Publish all ports" option to no avail):

Using docker inspect container_id with the command line created container, I get these settings under NetworkSettings:
"Ports": {
"5000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5000"
}
]
},
However with Pycharm run configuration created container, it doesn't appear to have passed on the port bindings:
"Ports": {},
Is there a way to check the options Pycharm is passing to Docker? Or to make it correctly pass on the port binding options?
I'm on Windows 10 and Docker is running on Hyper-V (not VirtualBox)
Solution 1:[1]
I had a similar issue with a Django application. Here is how I solved it :
First you need to set up the project interpreter in a docker container :
- Go to settings (CTRL+ALT+s),
- then search
project interpreter - then click on the little wheel in the top right corner,
- then click on add
- then in the menu choose
Docker, - then choose your previously built image in the choice list
- then hit the
okbutton.
Now you need to create a proper run configuration.
- Hit
ALT+u - then
ALT+r - then
ALT+inser, - choose
Flask server. - You will see a text area with the label
Docker container settings, click on this text area - then hit
SHIFT+enter.
You will have a menu allowing you to configure multiple docker run parameters including port biding.
Solution 2:[2]
For me adding the localhost IP ('0.0.0.0') as arg in the app object run call solved it
app.run(host='0.0.0.0')
Solution 3:[3]
I had a similar issue. For me, it worked to change the docker container settings in the Edit Run Configuration menu. If you click on the small folder you can add port and volume mapping.
Maybe it's trivial for most people or is supper clear in the documentation, but it took me a while to find it, so I'm adding this just in case there are more people looking for the port volume mapping.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Louis Saglio |
| Solution 2 | gCoh |
| Solution 3 | Raquel |

