How to properly run Python using Docker/Kubenertes?

8/12/2019

I have a simple python code, but I can't get it running on Docker nor Kubernetes.

I'm building, tagging and pushing my image and than running

docker build -t test . --no-cache
docker tag test tcnl/test:0.2
docker push tcnl/test
docker run tcnl/test:0.2
#!/usr/bin/env python

# WS server example
print("Listening...")
import socket
print("Listening...")
import sys
print("Listening...")

s = socket.socket()
print("Listening...")
host = "0.0.0.0"
port = 8765
s.bind((host, port))
print("Listening...")
s.listen(5)
print("B4 while")
try:

    c, addr = s.accept()
    while True:
        print("INSIDE TRUE")
        print('Got connection from', addr)
        c.send(("Connection accepted!").encode('utf-8'))
        print(str(c.recv(4096).decode('utf-8')))
except:
    print("Oops!",sys.exc_info(),"occured.")
    input()
s.close()

It's meant to be just a websocket server, waiting for a connection an then just send messages. But not even that is working... Now my Dockerfile

FROM python:3.6
MAINTAINER tcnl

# Creating Application Source Code Directory
RUN mkdir -p /test/src

# Setting Home Directory for containers
WORKDIR /test/src

# Installing python dependencies
COPY ./requirements.txt /test/src/requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copying src code to Container
COPY ./test-server.py /test/src/test.py

# Application Environment variables
ENV APP_ENV development

# Exposing Ports
EXPOSE 8765

# Setting Persistent data
VOLUME ["/test-data"]

# Running Python Application
CMD python /test/src/test.py

When I run on docker the program doesn't even print. What am I doing wrong?

-- Thiago Casa Nova
docker
kubernetes
python

1 Answer

8/12/2019

Just run docker run -it tcnl/test:0.2 it would bind docker stdout to your terminal and you will see output:

Docker

# docker run -it tcnl/test:0.2
Listening...
Listening...
Listening...
Listening...
Listening...
B4 while

Kubernetes

kubectl run -it test-server --restart=Never --image=tcnl/test:0.2

To get output: kubectl logs test-server

Listening...
Listening...
Listening...
Listening...
Starting Flask...
Listening...
B4 while
-- FL3SH
Source: StackOverflow