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 apparently no official way to do it with Docker yet.
Should I prepare special Docker image with exposed ssh port? How to do that more easily?
In order to avoid any SSH overhead (which makes perfect sense with Docker),
docker exec
definitely seems to be the way to go.Unfortunately I couldn't get it to work so far. It would be great if someone could fill in the blanks. Here is what I did (using PyCharm 4.0.4 and Docker 1.4.1):
Create a file named
python_myproject.sh
containing the following:Note that the file's name has to begin with
python
otherwise PyCharm will complain.In PyCharm's settings, under
Project Interpreter
, add a new local interpreter. Give it the path to yourpython_myproject.sh
file.This is where I'm stuck. After a quite long loading time (the throbber says "Setting up library files"), a window entitled "Invalid Python SDK" appears and says:
In
~/.PyCharm40/system/log/.idea
:With Docker 1.3, use the
exec
command to construct the path to the Python interpreter: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.
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:
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: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 fromRun
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'timport pydevd
):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.
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.