How to connect PyCharm to a python interpreter located inside a Docker container?

前端 未结 10 2164
再見小時候
再見小時候 2021-01-30 03:40

I\'m starting with Docker, but I don\'t know how to configure PyCharm to use a python interpreter located in a container.

It was easy to setup with Vagrant, but there\'s

相关标签:
10条回答
  • 2021-01-30 03:43

    I haven't tried this, but I would try creating a Bash script which calls docker exec ..., as in @Anto's answer.

    Then, install the BashSupport extension. Now create a new run configuration which runs your script as a Bash script.

    0 讨论(0)
  • 2021-01-30 03:48

    It's not yet here, but shortly this should no longer be a problem, since

    Docker support will be introduced in PyCharm starting with PyCharm 4.1 EAP (beginning of April)

    source: http://blog.jetbrains.com/pycharm/2015/03/feature-spotlight-python-remote-development-with-pycharm/#comment-187772

    0 讨论(0)
  • 2021-01-30 03:48

    You can get a bit crazy by installing Pycharm in the container and just running it from there. You'd have to do this by docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=:0.0 pycharm-image but it should work just fine. But remember that all of Pycharm and your source would be in that container as well. So save, commit, and push early and often.

    0 讨论(0)
  • 2021-01-30 03:51

    With PyCharm 5 they added support for docker. You must have your docker configured in docker-machine.

    If you don't already use docker-machine you can connect to an existing machine using the generic machine engine and ssh into a vagrant VM or to localhost if you aren't running things in a VM. I didn't find a way around the ssh to localhost unfortunately.

    I haven't found a way to mount volumes into the docker image they use, to share files with my dev tree, but it might be possible.

    0 讨论(0)
  • 2021-01-30 03:59

    With Docker 1.3, use the exec command to construct the path to the Python interpreter:

    sudo docker exec container_name /usr/bin/python
    

    See https://docs.docker.com/reference/commandline/cli/#exec, http://forum.jetbrains.com/thread/PyCharm-2224

    You could install SSH inside the container and then expose the port, but that isn't how containers are expected to be used, because you would be bloating them.

    0 讨论(0)
  • 2021-01-30 04:01

    If all you need is to debug code which is launched inside docker container, you could use pycharm's python debug server feature. As for me, it is less troublesome way than accessing remote interpreter via SSH. Drawback of this solution is that for auto-complete and all this kind of stuff you should have local copy of container's interpreter and mark it as project's interpreter (works for auto-complete, but i'm not sure that it's possible to debug code from third-party libs in such case) or make container's interpreter files visible to pycharm (not tested at all). Also note that Python debug server is feature of Professional edition.

    What you should do for debugging via Python debug server:

    1) make sure that directory with your project is added into container. It could look like this line in Dockerfile:

    ADD . /path/in/container

    2) copy pycharm-debug.egg (pycharm-debug-py3k.egg for Python3) from directory where pycharm is installed on your host to directory in container, which is in container's PYTHONPATH. Path to pycharm-debug.egg on developer's host could be:

    • for Mac: /Applications/PyCharm.app/Contents/pycharm-debug.egg
    • for Linux: /opt/pycharm/pycharm-debug.egg

    3) create Run/Debug configuration for launching Python debug server on host as described at To configure a remote debug server section of docs. Port is any host's port of your choice, but IP is address at which host is accessible from container. It could be:

    • if container run via boot2docker, likely, IP is 192.168.99.1 -- host's address at Host-only network with vbox machine
    • if host is Linux, IP can be found via ifconfig, for me it is:
    docker0   Link encap:Ethernet  HWaddr 56:84:7a:fe:97:99  
              inet addr:172.17.42.1  Bcast:0.0.0.0  Mask:255.255.0.0
    

    Also, don't forget to specify path mappings between project's path at developer's host and project's path at container.

    This blog post also could be helpful for current step

    4) launch this created configuration (for example, via Debug button, right from Run one)

    5) create python script which would launch your project and add the following code for debug initialization as first lines of this script. (make sure that pycharm-debug.egg is in PYTHONPATH, or this code couldn't import pydevd):

       import pydevd
       pydevd.settrace('172.17.42.1', suspend=False, port=8765, stdoutToServer=True, stderrToServer=True)
    

    6) Finally, you could set breakpoints and launch your application from host, in container via created script. For example:

    docker-compose run 'container_name' python 'script_name' 'args'

    On start, yours launching script will connect to Python debug server, which is running on host, and stop on breakpoints. Debugger features will be available as usual.

    0 讨论(0)
提交回复
热议问题