Dockerizing Python-Flask Application and Deploying on Kubernetes

Towfeeq Fayaz
5 min readAug 12, 2020

--

I like to keep things simple and to the point.

Dockerizing Web-Applications

What We Want To Do

The Dockerization of an application is nothing but simply taking your application code and embedding it into a Docker image and run within a Docker container (container is to image what object is to class, i.e; an instance).
Prerequisites: Docker + Kubernetes.
If you don’t have them, I would suggest to download docker-desktop, which is easy to download and run. You can download from the link for mac, windows.

Let’s Look At Our Python-Flask Hello-World Application Code And Other Docker Dependencies

The code directory structure should be as following. It is better to keep the application code in a separate directory as app.py is the application code file kept in app directory. You can also access my git-hub repo here for your reference;

hello-python
├── Dockerfile
├── app
│ └── app.py
└── requirements.txt
1 directory, 3 files

Let’s look at the app.py file

from flask import Flask
import os
app = Flask(__name__)
port = int(os.getenv("VCAP_APP_PORT", 5000))
@app.route('/')
def hello_world():
return 'Hello Towfeeq, we did it!\n'
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = port)

Now, Let’s look at the requirements.txt file which contains the dependencies related to our python application.

flask

Here flask is the module which is a python web-framework using which we will create our application.

Finally, the Dockerfile which contains the instructions for how to create an image.

# lets choose base image
FROM python:3.7
MAINTAINER Towfeeq Fayaz "towfeeqpandith@gmail.com"#select the working directory within the container
WORKDIR /mydir
#copy the files from localhost to working directory
COPY ./ ./
#install dependencies
RUN pip3 install -r requirements.txt
#export the port on which container will be listening
EXPOSE 5000
# the command that will run upon starting of container
ENTRYPOINT [ "python3" ]
CMD [ "./app/app.py" ]

FROM — Defines the base image on top of which we want to build our image.
MAINTAINER — Describes the creator of the image.
WORKDIR — It describes the directory of execution inside the container. Once you define it, then the commands like COPY, RUN, CMD will run with respect to this directory.
COPY — Copies the content of ./ (i.e; current directory) on host to /mydir on container. Note, if the /mydir is not present in container, it will automatically create it.
RUN — Since the WORKDIR is already set, this will run before the starting of the application inside container as it is a compile time command and will install dependencies for our application.
EXPOSE — Defines the port you want to expose on the container(The point of communication to the container).
ENTRYPOINT — This is the runtime command i.e; it will execute once your container is ready.
CMD — what ever is mentioned under this is run against the ENTRYPOINT (so the full command that will be executed after container is ready is combination of ENTRYPOINT and CMD e.g; python3 ./app/app.py.

Let’s Build Our Image

Once you are inside the directory which contains all the files(in my case the directory is hello-python), run the below commands to build the image.

> docker build -t towfeeq11/hello-python:v2 .
> docker images
building the image
checking the images
> docker login
(will ask for credentials to login to docker-hub. If you don't have an account on docker-hub, you can do so by signing up here for free and create your account to host your own images. It will be used while deploying on kubernetes).
> docker push towfeeq11/hello-python:v2

It is pertinent to mention here that towfeeq11 is my username on docker-hub. If you want to push your image to docker-hub, then the naming convention for the image to be pushed is <username>/<image name>:<tag>. You can skip the <tag> though.

Deploy Your Application Using Docker Directly

Once you run the below command, you can simply check the localhost on port 5000. If you get the response (in my case “Hello Towfeeq, we did it!”), then congrats, all is well.

> docker run -d -p 5000:5000 towfeeq11/hello-python:v2
> curl localhost:5000
Hello Towfeeq, we did it!
checking our application

Time To Deploy Our Application On Kubernetes

Just make sure, if you already run your application using docker, then stop the container or choose the port in your yaml file other than 5000, else there will be error.
To deploy your application on Kubernetes, create a file with extension .yaml and create a deployment as shown below. I have created the deployment with name hello-python.yaml.

apiVersion: apps/v1
kind: Deployment
metadata:
name: python-deployment
labels:
myapp: pyapp
spec:
replicas: 1
selector:
matchLabels:
myapp: pyapp
template:
metadata:
labels:
myapp: pyapp
spec:
containers:
- name: python-container
image: towfeeq11/hello-python:v2
ports:
- containerPort: 5000

To Deploy our application on kubernetes-Cluster, run the following commands and make sure you are running the first command from within the directory where hello-python.yaml is stored.

> kubectl apply -f hello-python.yaml
> kubectl get deploy
> kubectl get pods

Once it is done, then we need to do port-forwarding to access the application outside the container. You can also create a Service using a seperate deployment, but for testing, we can do simple port-forwarding.

> kubectl get pods             NAME                      READY         STATUS 
python-deployment-7d859668dd-mzdp4 1/1 Running
> kubectl port-forward python-deployment-7d859668dd-mzdp4 5000:5000
deploying on kubernetes

After executing the above commands, you can check your live application by running localhost:5000 either on browser or else from terminal run following command.

> curl localhost:5000
Hello Towfeeq, we did it!
checking application liveliness from browser

--

--