Set PyCharm python interpreter based on my already existing docker container python environment

I'm trying to set up PyCharm's python interpreter so that it uses the python environment that's already set up on docker.  I have my docker containers environment already set up and running, totally separate from PyCharm.  I'm using docker desktop on my mac.  And I'm developing on PyCharm.  Historically I'd configure PyCharm's python interpreter using my virualenv I manually set up, but now that I'm using docker I'd like to have Pycharm just refer to the one that's already created using docker desktop via:

`docker-compose build`
and
`docker compose -f dev.yml run --entrypoint bash namegoeshere`

When I follow the guides on PyCharm's website for docker and docker compose, they seem to create new containers that are totally separate from my containers that already exist.  And then my python interpreter shows either an empty to newly initialized list of python packages.  I don't want pycharm to create a new one, I just want to refer to the one that's already in existance in my container.  Is that possible?  

Guides I've been following:

https://www.jetbrains.com/help/pycharm/using-docker-as-a-remote-interpreter.html#config-docker

https://www.jetbrains.com/help/pycharm/using-docker-compose-as-a-remote-interpreter.html

I tried following the docker compose tutorial, and selecting my .yml file and then selecting the container name I want to target, but it still creates a brand new environment rather than using that container that's already running. Is there any way to select the one that's already running?


Screenshots of what it ends up looking like after I follow these guides:



Created using Add new interpreter -> docker compose, notice it's blank and not using my container's packages



An example of Pycharm creating a totally separate container, rather than using the one that's already created



An example where it created a blank/initialized set of packages, instead of using my already existing container. 

 

1
7 comments

Hello, 

Yes, I confirm a new container is being created every time you run/debug code. There is no native support to use interpreters in s running container. 

The only possible workaround, which is a tricky one, is to use the existing container is to configure an SSH based interpreter https://www.jetbrains.com/help/pycharm/configuring-remote-interpreters-via-ssh.html#ssh-credentials , but please note that the Deployment configuration will be also created, which will copy all project files to the container and will constantly keep them synched. 

0

Hi Antonina Belianskaya , is there any updates about this?

I have an existing docker instance that I want to use as an interpreter. I cannot ssh into it. 

VSCode has this desired function - “Attach to docker container” - unfortunately I have to use VSCode for debugging.

0
PyCharm now offers several options for working with running containers, besides the SSH interpreter:

- Remote development (https://www.jetbrains.com/help/pycharm/remote-development-overview.html)
- Devcontainer (https://www.jetbrains.com/help/pycharm/2023.2/connect-to-devcontainer.html)
- If you're using docker-compose, you can specify the `exec` command in the run/debug configuration, making PyCharm to run the code inside the already running container.
0

Thanks Andrey Resler !

Unfortunately we use a specialized container orchestrator in charge of GPU allocation, and I have no control on the docker itself, except that I have access to it, so docker-compose or devcontainer cannot help. Remote development can probably work however it will be less convenient in my case. Is there any plan to support existing docker containers?

2
Hmm, as far as I know, we don't have such plans, and I didn't find the existing feature request, so submitted a new one: https://youtrack.jetbrains.com/issue/PY-63729/Attach-to-running-Docker-container

Feel free to vote and comment.
6

As a side note, I was able to this with pycharm prior to 2022. Me an my team worked with the interpreter already existing in our docker container separately and we were able to add that as an interpreter in pycharm. We've worked like this for many years until this feature just disappeared after an update to the latest version. 

note: I'm not exactly sure about the year 2022, it could be - a year or two but I'm sure we had this feature in the past.  

1

I agree with Gurel's comment above, this used to NOT be the case until a change a year or so ago. It's really painful how many containers you end up with and how much slower it is to fire up

 

2

Please sign in to leave a comment.