Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycharm Docker port bindings

Tags:

docker

pycharm

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):

pycharm docker run configuration

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)

like image 360
Nicholas Avatar asked Oct 23 '16 21:10

Nicholas


People also ask

What is port binding in Docker?

Docker containers can connect to the outside world without further configuration, but the outside world cannot connect to Docker containers by default.

What port should I use for Docker?

By default, the httpd server listens on port 80. It's not mandatory to perform port mapping for all Docker containers. Often, we will avoid opening host ports in order to keep the services of the container private or only visible to sibling containers in the same Docker network.


3 Answers

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 :

  1. Go to settings (CTRL+ALT+s),
  2. then search project interpreter
  3. then click on the little wheel in the top right corner,
  4. then click on add
  5. then in the menu choose Docker,
  6. then choose your previously built image in the choice list
  7. then hit the ok button.

Now you need to create a proper run configuration.

  1. Hit ALT+u
  2. then ALT+r
  3. then ALT+inser,
  4. choose Flask server.
  5. You will see a text area with the label Docker container settings, click on this text area
  6. then hit SHIFT+enter.

You will have a menu allowing you to configure multiple docker run parameters including port biding.

like image 125
Louis Saglio Avatar answered Oct 17 '22 05:10

Louis Saglio


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')
like image 2
gCoh Avatar answered Oct 17 '22 05:10

gCoh


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.

enter image description here

like image 1
Raquel Avatar answered Oct 17 '22 06:10

Raquel