DEV Community 👩‍💻👨‍💻

Mohamed M El-Kalioby
Mohamed M El-Kalioby

Posted on

Django on K8s (Part II: Create an app image)

Welcome back to the series, so now we installed Docker and minikube and we have our Django application ready which is on github. 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 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

  3. create a virtualenv

    virtualenv env

  4. start the virtualenv
    source env/bin/activate

  5. install the requirements.txt
    pip install -r requiremnets.txt

  6. run the application
    cd django_app; python manage.py runserver

  7. Go to http://127.0.0.1:8000 and login with admin admin
    You shall get the same as the image below.

App Worked

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
Enter fullscreen mode Exit fullscreen mode

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/
Enter fullscreen mode Exit fullscreen mode

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

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

  1. Run inspect to get the container information based on id sudo docker inspect 890200dd1d12

Container IP

Now go to http://172.17.0.2/ and you shall get the login page and login by admin, admin

Running app in container

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

Top comments (0)

🌱 DEV runs on 100% open source code known as Forem.

 
Contribute to the codebase or learn how to host your own.