Django on K8s (Part II: Create an app image)
Tags: ubuntu, django, docker, kubernetes
Published: 2022-01-09 08:08:18

Welcome back to the series, so now we installed Docker and minikube and we have our Django application ready which is on [github](https://github.com/mkalioby/django-on-k8s/tree/master/django_app). First, we will test the application on the PC using the dev server and then we will start creating an image on docker using docker file.

## Test the application

We need to clone [GiHub repo](https://github.com/mkalioby/django-on-k8s.git) and set create the virtualenv.

1. Clone the repo 
   
     `git clone https://github.com/mkalioby/django-on-k8s.git`

2. enter the cloned folder
   
    `cd django-on-k8s`

2. create a virtualenv
   
    `virtualenv env`
3. start the virtualenv
`source env/bin/activate`
4. install the requirements.txt
`pip install -r requiremnets.txt`
5. run the application
    `cd django_app; python manage.py runserver`
5. Go to [http://127.0.0.1:8000](http://127.0.0.1:8000) and login with `admin` `admin` 
You shall get the same as the image below.

![App Worked](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5unwxk8dbub49b3pso24.png)

So now the application is working fine, let's building the image

## Building the image

There are 2 ways to build an image, 
1. start a container from a base image like Ubuntu or Python3.8 and start writing commands to make the container suitable for our requirements e.g install apt packages and/or pip packages and then create an image.
2. Automating the process with Dockerfile

The second method looks hard at first but once you learnt it, it will be easy to package the new code in the image.

So let start 
Add the following to `Dockerfile`
```
FROM python:3.8-slim-bullseye

RUN pip install gunicorn

WORKDIR /app

COPY docker_app/requirements.txt .

RUN pip install -r /app/requirements.txt

COPY django_app/ /app/

EXPOSE 80/tcp

ENTRYPOINT gunicorn -w 4 -b 0.0.0.0:80 k8s.wsgi
```
I think the file is clear,we started from python:3.8, installed gunicorn (as it is NOT part of requirements.txt', set the working directory to '/app', Copied the requirements file to /app, installed the env, and then copied django_app to /app and told Docker to export port 80 and finally we ran the gunicorn command to start the server.

**Note**: we copied the requirements.txt separately to avoid reinstalling the pip packages if there is no updates to the requirements file e.g no packages updated.  

But there are some files to skip copy as pyc, \_\_pycache_\_ and the current environment so add the following to `.dockerignore`

```
*/*.zip
*/*.sh
*/*.pyc
*/__pycache__*

*.zip
*.sh
*.pyc
__pycache__
env/
```

### Build the image
`docker build -t django-example:v1.0 .`

**Note:** the '.' at the end of the command.

You shall get something like the image below.

![docker image built](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gmksyce8mhsclob60xuc.png)


## Test the image

Let's make sure that the image is good,
`sudo docker run django-example:v1.0`

then open another tab and get the container IP.
1. Run `ps -ls` to get the running containers.
`sudo docker ps -ls`

![docker containers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57essebyct8sszyaen3t.png)
2. Run inspect to get the container information based on id
`sudo docker inspect 890200dd1d12`

![Container IP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4z5waentt9vtqj3foqo.png)

Now go to [http://172.17.0.2/](http://172.17.0.2/) and you shall get the login page and login by `admin`, `admin`

![Running app in container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rq2g1dalqqzn6auvs13l.png)

This concludes the second part of the series, the next part is running this under Kubernetes, so stay tuned.