In a Kubernetes yaml file, how do you run a python script first then run the specified container?

Solution 1:

What is happening?

The command supplied through the yaml file overrides the CMD in the dockerfile (you can refer the kubernetes documentation here). So, when you supply command for executing the script1 in the yaml file, it overrides the command (CMD) in dockerfile for the execution of script2 because of which you must be getting errors as per your code logic.

How to resolve?

Step 1: Create a bash file as follows: (naming it "run.sh")

#!/bin/bash

exec python3 /path/to/script1.py
exec python3 /path/to/script2.py

Step 2: Update the dockerfile as:

FROM python:3.8.0-alpine
WORKDIR /code
COPY script1.py .
COPY script2.py .
COPY run.sh . # copying the bash file

# Install python libraries
RUN pip install --upgrade pip
RUN apk add build-base
RUN apk add linux-headers
RUN pip install -r requirements.txt

RUN chmod a+x run.sh # making it executable
CMD ["./run.sh"] # executing the scripts through bash file

Step 3: Remove the command from the kubernetes deployment yaml

But if you want to keep the command in the yaml file then you should replace it with "./run.sh"

Note: Ideally you should not run two different scripts like this. If you need to set up tokens/keys you should do that in a sub-module which you can call in your main script. You can handle network connectivity issues, etc. through a combination of exception handling and a retry mechanism.