Python from scratch in Docker¶
- Published at
April 10, 2022
- Author
Erlend ter Maat
At last I had to get this subject in my portfolio: Docker!
The source of this need-to-learn-docker was a project in which I had to get celery working in an existing django/docker project. It is easy to develop a Django application when the development environment is already in place. But now I needed to add Celery to the project and I just couldn’t get the configuration right, so I started to build a Django project from scratch to see what parameters were required, and how it should be configured in the same fashion as the docker configuration it was required to run in.
It starts with a manual…¶
I had only a vague idea of what the contents of a Dockerfile or docker-compose.yml might mean, so I started with a tutorial on this subject. I ran into this one https://www.youtube.com/watch?v=3c-iBn73dDE, which gave me, in less then three hours, a good idea of what Docker is really about. Recommended to watch!
Project¶
I started to see the benefit of having a system that at-hoc spins up any part of the application that your project uses without having to worry about conflicting versions and just a little bit about system requirements. So the idea came into my mind that it would be nice to see what it would look like to start a Django project without even touching Python on my local development machine.
Docker¶
First I created this simple project setup. Just a requirements.txt file with django, celery and redis, a simple docker file and a file with an endless loop to keep the machine run long enough to create the initial project structure:
# requirements.txt:
django
celery
redis
(I usually include version numbers afterwards from pip freeze, but only copy the packages that I intentionally install in the first place)
# slee.py
from time import sleep
while True:
sleep(1)
# Dockerfile
FROM python:3.10.4-alpine
RUN apk add bash
COPY . /project
WORKDIR /project
RUN pip install -r requirements.txt
CMD ["python", "slee.py"]
# docker-compose.yml
services:
web:
build: .
volumes:
- .:/project
From here it was possible to:
Build an image from my project root and run the image in a container,
Build the django project in isolation.
and present the end result at my portfolio location: https://github.com/erlendgit/djocker
Conclusion¶
It was fun to finally get my hands on this subject of Docker containers, images and volumes. It did not bring what I expected first: building an image would result in a django project structure. It turned out in while building an image that is capable of running django; and also build the project structure with a little help of a script that does nothing. And maybe there is another way of doing that.
If it is just about to create a django project - selecting a python executable is not that hard after all. But is the kind of small test where it all started with - one that requires complex resourses - the result of this experience may serve as a nice boilerplate.