1

I need to install some python packages in docker image and deploy that image as kubernetes deployment. Below are my Dockerfile and deployment file

FROM python:3.7-slim

# install FreeTDS and dependencies
RUN apt-get update \
 && apt-get install unixodbc -y \
 && apt-get install unixodbc-dev -y \
 && apt-get install freetds-dev -y \
 && apt-get install freetds-bin -y \
 && apt-get install tdsodbc -y \
 && apt-get install --reinstall build-essential -y

# populate "ocbcinst.ini"
RUN echo "[FreeTDS]\n\
Description = FreeTDS unixODBC Driver\n\
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so\n\
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so" >> /etc/odbcinst.ini

# install pyodbc (and, optionally, sqlalchemy)
RUN pip install --trusted-host pypi.python.org pyodbc==4.0.26 sqlalchemy==1.3.5 pyarrow

# run app.py upon container launch
CMD ["python3"]

docker image build command

docker build pyarrow-custom:latest .
apiVersion: apps/v1
kind: Deployment
metadata:
  name: python-odbc-custom
  labels:
    app: python-odbc-custom
spec:
  replicas: 1
  selector:
    matchLabels:
      app: python-odbc-custom
  template:
    metadata:
      name: python-odbc-custom
      labels:
        app: python-odbc-custom
    spec:
      containers:
        - name: python-odbc-custom
          image: "pyarrow-custom:latest"
          command: ['python3']
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 8099

kubectl command

kubectl apply -f python-obbc-deployment.yaml

However, After creating container it is getting completed. As I am giving python3 in CMD I want to keep running python shell there. How can i do that?

Ayush Goyal
  • 415
  • 4
  • 23
  • 1
    This isn't possible. Its the same as if you were to give bash or any other shell as command. It will exit immediately. There is tricks with putting a sleep command in the container and so forth but it actually doesnt even make sense. I am not entirely sure about kubernetes but in docker you would just run this shut down container on demmand with a real command or in this case in interactive mode. Maybe `kubectrl` run can do the same for you. – The Fool Apr 30 '21 at 10:03
  • See similar question for docker-compose here. https://stackoverflow.com/questions/52648391/how-to-set-a-docker-compose-bin-bash-entrypoint – The Fool Apr 30 '21 at 10:05
  • In case of docker, I am able to access the python shell using this command docker run --name test1 -it pyarrow-custom:latest – Ayush Goyal Apr 30 '21 at 10:07
  • 1
    yes because you start it interactively. That is something else. In Kubernetes or docker-compose for that matter you cannot give `-ti` essentially. And that's your problem. – The Fool Apr 30 '21 at 10:09
  • hence, I am hinting that you should find the equivalent of `docker-compose run --service-ports my-python-shell python3` – The Fool Apr 30 '21 at 10:10
  • 1
    What should the pod _do_ when it launches? The pod spec hints at a port 8099, is there a program that provides a service there? Typically you'd `COPY` your application code into the image and set its `CMD` to run that. (You shouldn't need to override the `command:` at the Kubernetes layer.) – David Maze Apr 30 '21 at 10:35

0 Answers0