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
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:
/Applications/PyCharm.app/Contents/pycharm-debug.egg
/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:
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.