Rapidly modifying Python app in K8S pod for debugging Rapidly modifying Python app in K8S pod for debugging kubernetes kubernetes

Rapidly modifying Python app in K8S pod for debugging


There are two main options: one is to use a tool that reduces or automates that flow, the other is to develop locally with something like Minikube.

For the first, there are a million and a half tools but Skaffold is probably the most common one.

For the second, you do something like ( eval $(minikube docker-env) && docker build -t myimagename . ) which will build the image directly in the Minikube docker environment so you skip steps 3 and 4 in your list entirely. You can combine this with a tool which detects the image change and either restarts your pods or updates the deployment (which restarts the pods).

Also FWIW using scp and docker load is very not standard, generally that would be combined into docker push.


I think your pain point is the container relied on the python code. You can find a way to exclude the source code from docker image build phase.

For my experience, I will create a docker image only include python package dependencies, and use volume to map source code dir to the container path, so you don't need to rebuild the image if no dependencies are added or removed.

Example

I have not much experience with k8s, but I believe it must be more or less the same as docker run.

Dockerfile

FROM python:3.7-stretchCOPY ./python/requirements.txt /tmp/requirements.txtRUN pip install --no-cache-dir -r /tmp/requirements.txtENTRYPOINT ["bash"]

Docker container

scp deploy your code to the server, and map your host source path to the container source path like this:

docker run -it -d -v /path/to/your/python/source:/path/to/your/server/source --name python-service your-image-name

With volume mapping, your container no longer depend on the source code, you can easily change your source code without rebuilding your image.