How to install sth in kubernetes container

6/4/2019

I want to install mysql-connector==2.1.6 in python container. One way is writing this command in DockerFile

RUN pip install mysql-connector==2.1.6

And make images and use that image in kubernetes. But I want to run python images and after that install mysql-connector==2.1.6 But after crashing container or rebooting it ,I should install mysql-connector==2.1.6 again. After a lot of search I see cloud_provider that install packages in running container. But it does not explain how it do that. Now I have kubernetes and I want to permanently install some packages on running container. What should I do?

I know I can commit container but I do not want it because I do not know when the user install packages.

-- yasin lachini
docker
kubernetes
mysql
python

3 Answers

6/4/2019

It's a hard and fast rule that changes to the container are ephemeral. If the container shuts down, they're gone. You could maybe try to put all configuration files into permanent storage, but that would be a nightmare. Long story short, if you want it to last, you need to make it part of the image.

-- Laizer
Source: StackOverflow

6/7/2019

Did you consider initContainers?

Init Containers always run before App Container in a Pod. In addition all initContainers must complete succesfully (there can be a series of initContainers). According to documentation :

List of initialization containers belonging to the pod. Init containers are executed in order prior to containers being started. If any init container fails, the pod is considered to have failed and is handled according to its restartPolicy.

Via initContainers you can include some security content which should not be in image, it can contain utilities or scripts (like install of mysql-connector).

More information about initContainers and examples here.

-- PjoterS
Source: StackOverflow

6/9/2019

It sounds like you are trying to use Kubernetes pods as a replacement for traditional "virtual machines" or "VPSs", where a user "logs in" to the pods and manually runs commands to install their application.

Kubernetes is not designed to be used that way. It is designed to treat pods as disposable. It automatically restarts pods in many different scenarios (node going down, node running out of resources, etc). When a pod gets restarted, all the "manual changes" are lost. This is by design, you should not try to fight that.

You need to build the complete, final container image including Python, the libraries and the application code in your Dockerfile, then build it and push it to a registry, and then run it in Kubernetes. You should never need to manually log in to a container.

Something like this should work

FROM python:3.7

# Install any dependencies you need
RUN pip install mysql-connector==2.1.6

# Copy all the application source to the container.
COPY src/ /app

# Set the entrypoint to your main python file.
ENTRYPOINT ["/app/main.py"]
-- Dirbaio
Source: StackOverflow