Robert Kozikowski's blog

Using PyCharm docker integration with minikube

Introduction

I will write about using docker environment from minikube VM for PyCharm integration.

I wish someone would make it work as well with jedi (emacs/vim python completion daemon), but currently only PyCharm supports docker well.

Why use PyCharm docker integration?

  • IDE features based on python version in the docker image, like code completion, auto-imports, library navigation or type checking.
  • Debug code running inside the docker image using the PyCharm debugger.

Why use the same docker environment for PyCharm and minikube?

  • You don’t need to build the same image twice for different docker environments or rely on docker push/pull.
  • No need to switch between docker environments.

Prerequisites

Docker, pycharm and minikube installed.

Set up docker in PyCharm

My minikube docker-env

export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/home/kozikow/.minikube/certs"
export DOCKER_API_VERSION="1.23"

My pycharm configuration

You need to replace tcp reported by docker-env with https.

Configure remote interpreter

Now that pycharm knows how to connect to docker inside minikube, configure a python interpreter as described in https://www.jetbrains.com/help/pycharm/2016.1/configuring-remote-interpreters-via-docker.html .

Share the path with the VM

Problem with pycharm bindings

When using docker on localhost or within the docker-machine, pycharm configure binds paths from localhost to the docker image.

When using the minikube VM, pycharm decides to bind the path from the VM instead of localhost. I created bug about this: https://youtrack.jetbrains.com/issue/PY-20791#u=1473897921285 .

Pycharm run configurations, that allow debugging within code running in the container, rely on binding your project code to /opt/project. Currently, the only option is sharing the path between your localhost and the VM.

It needs to be configured only once for minikube VM.

Configure binding in VirtualBox

If you use different VM provider you may need different settings. I want to share all my git repos with the virtualbox VM. I store them at /home/kozikow/git_repos. Firstly, I need to configure sharing in the virtualbox GUI:

Mount paths in the VM

minikube ssh
mkdir -p /home/kozikow/git_repos
sudo chown -R docker /home/kozikow

sudo mount -t vboxsf git_repos /home/kozikow/git_repos

Start automatically

Starting minikube and manually mounting paths is annoying. You can do it with systemd service. Create minikube service. My /etc/systemd/system/minikube.service looks like:

[Unit]
Description=Minikube

[Service]
Type=oneshot
TimeoutStartSec=300
User=kozikow
ExecStart=/usr/local/bin/minikube start --show-libmachine-logs=true ; \
    /usr/local/bin/minikube ssh "mkdir -p /home/kozikow/git_repos && sudo mount -t vboxsf git_repos /home/kozikow/git_repos" ; \
    /usr/local/bin/minikube ssh "mkdir -p /home/kozikow/.config && sudo mount -t vboxsf config /home/kozikow/.config"
RemainAfterExit=yes
ExecStop=/usr/local/bin/minikube stop

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable minikube.service
sudo systemctl start minikube.service

Docker-PyCharm bugs

The integration is still new, so there are a few bugs. It’s still worth it. All bugs are in the pycharm issue tracker.

  • Your images can’t have custom ENTRYPOINT
  • If you want to use docker compose inside the VM, it needs to be accessible under the same path on localhost and the VM. Sharing the repo with the VM covers this problem.

Future work

It would be nice if I could debug images launched used kubectl run using pycharm. Images started by pycharm are not started inside kubernetes, so they can’t rely on some intra-cluster features.

At this point, the best way I found of debugging an image started by kubectl run would be manually installing Python Debug Server on your image as described in https://www.jetbrains.com/help/pycharm/2016.1/remote-debugging.html , and connecting to it after kubectl run.