Run Python in Sidecar Container (4.0)

When specifying the location of a Python executable in the Python Manager(3.3) or in the Python Interpreter Profile (4.0), you can either use the path to the executable in the file system or use connection details to a Python daemon. If you choose to connect to a Python daemon, you can provide your sidecar container for running Python processes.

The sidecar container is an extra container that you add to the ECD. To use the sidecar container, you must create your own Docker image and include the pythond.py script in your image. This script can be found in the /opt/mz/python/ directory on the platform container.

To connect to the Python daemon running in the sidecar container, you need to configure the Python Manager to connect using localhost:port, where port is the port that the daemon is listening to, for example, localhost:5454.

Here is an example of a Dockerfile that you can use to build your image:

FROM python:3.12 RUN pip install pandas COPY pythond.py . CMD ["python", "pythond.py", "5454"]

In this example, the python:3.12 base image is used and the pandas package is installed with pip. The pythond.py script is then copied to the container and the command is set to run the script with port 5454.

To use this Dockerfile, you need to create a new directory and place both the Dockerfile and pythond.py script in that directory. You can then build the image using docker build -t my-python-image:latest .. This will create a new Docker image with the name my-python-image and tag latest.

To add the container to your ECD, you can use a YAML snippet as shown below to apply your changes:

spec: sidecars: - image: my-python-image:latest imagePullPolicy: IfNotPresent name: python-container

This YAML example specifies that we want to add a sidecar container named python-container with image my-python-image:latest and imagePullPolicy set to IfNotPresent.

Note that you can choose any port number instead of 5454 as long as it is not already in use by another process.

If you want to use your sidecar container for code completion in the Python Code Editor as described in the Python Manager(3.3), you also need to add your sidecar to the Platform. You can add a YAML snippet as shown below to your values.yaml to achieve this:

platform: sidecars: - image: my-python-image:latest imagePullPolicy: IfNotPresent name: python-container

When you use a sidecar container, you can install additional Python packages that are not included in the standard installation. You can also choose which version of Python to use when running your Python code.

 

Â