blakem
blakem

Reputation: 153

How to use Docker AND Conda in PyCharm

I want to run python in PyCharm by using a Docker image, but also with a Conda environment that is set up in the Docker image. I've been able to set up Docker and (locally) set up Conda in PyCharm independently, but I'm stumped as to how to make all three work together.

The problem comes when I try to create a new project interpreter for the Conda environment inside the Docker image. When I try to enter the python interpreter path, it throws an error saying that the directory/path doesn't exist.

In short, the question is the same as the title: how can I set up PyCharm to run on a Conda environment inside a Docker image?

Upvotes: 7

Views: 2251

Answers (1)

blakem
blakem

Reputation: 153

I'm not sure if this is the most eloquent solution, but I do have a solution to this now!

  1. Start up a container from the your base image and attach to it
  2. Install the Conda env yaml file inside the docker container
  3. From outside the Docker container stream (i.e. a new terminal window), commit the existing container (and its changes) to a new image: docker commit SOURCE_CONTAINER NEW_IMAGE
    • Note: see docker commit --help for more options here
  4. Run the new image and start a container for it
  5. From PyCharm, in preferences, go to Project > Project Interpreter
  6. Add a new Docker project interpreter, choosing your new image as the image name, and set the path to wherever you installed your Conda environment on the Docker image (ex: /usr/local/conda3/envs/my_env/bin/python)

And just like that, you're good to go!

Upvotes: 5

Related Questions