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.
We need to clone GiHub repo and set create the virtualenv.
Clone the repo
git clone https://github.com/mkalioby/django-on-k8s.git
enter the cloned folder
create a virtualenv
start the virtualenv
install the requirements.txt
pip install -r requiremnets.txt
run the application
cd django_app; python manage.py runserver
Go to http://127.0.0.1:8000 and login with
You shall get the same as the image below.
So now the application is working fine, let's building the image
There are 2 ways to build an image,
- 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.
- 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
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
*/*.zip */*.sh */*.pyc */__pycache__* *.zip *.sh *.pyc __pycache__ env/
docker build -t django-example:v1.0 .
Note: the '.' at the end of the command.
You shall get something like the image below.
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.
ps -lsto get the running containers.
sudo docker ps -ls
- Run inspect to get the container information based on id
sudo docker inspect 890200dd1d12
Now go to http://172.17.0.2/ and you shall get the login page and login by
This concludes the second part of the series, the next part is running this under Kubernetes, so stay tuned.