Search code examples
pythonkubernetescloudcustodian

Running a K8s pod to support python virtual env


I am trying to create a Docker image along with K8s manifest, where i install some packages via pip in the image and later invoke that image in K8s to execute few cmds in the virtual env.

My Docker image looks like this

FROM cloudcustodian/c7n
SHELL ["/bin/bash", "-c"]

RUN python3 -m venv env && \
    source ./env/bin/activate && \
    python -m pip install install c7n && \
    python -m pip install install c7n-mailer

# RUN python -m venv custodian && \
#     source custodian/bin/activate && \
#     pip install c7n && \
#     pip install c7n-mailer 

My K8s manifest is like this where i pass args via values.yaml file

            command: ["/bin/bash","-c" ]
            args:
            {{- range $.Values.args }}
            - {{ . | quote }}
            {{- end }}

My args are

args:
 - "source ./env/bin/activate && c7n-mailer --config /home/custodian/mailer.yml --update-lambda && custodian run -v -s /home/custodian/output /home/custodian/policies.yaml"

I tried alot permutations and combinations, but none worked

Typical error that i see is

kubectl logs -f custodian-cloud-custodian-cron-daily-policies-27973874-7csf5                     % at kube docker-desktop
/bin/bash: line 1: ./env/bin/activate: No such file or directory

so the main question is how do u run a python virt env as k8s pod

Added sample code in the question


Solution

  • You could use the absolute path for this command source ./env/bin/activate in args