Two ways to stop writing Docker files

If l'm being honest with myself, writing Dockerfile and docker-compose.yml is a pain and surely, l can't be the only one out there experiencing this pain. Fortunately we have two of our friends that have created solution to stop our pain.

In this article l will be mainly focusing on Django, showcasing two ways to avoid creating docker files. The first and the most recommended way is using a CLI command docker init. The second option is to use Django cookiecutter.

Prerequisites

Docket init

If you are familiar with git you must be aware of a command called git init which initialises a local repository. In the same breath we have got an a similar CLI command called docket init which also initialises your projects. Docket init which l discovered a few days ago can help you to create project resources within your project. It creates Dockerfiles, docker compose files and well as well copied .dockerignore. Not only is this good news but it is a life saver, because now you are getting configurations directly from the system meaning you will be getting the best configurations.

How to use it?

As mention before we will be using Django. Create a repository on github and add a readme file, then clone the repository locally. It's always best practise to create a virtual environment. Create a requirement.txt file so that your directory structure looks as follows.

computername/clonerepository
├── readme.md
├── requirement.txt
└── myvenv/

Installation

  1. Install docker follow instruction here.
  2. Open requirement.txt file
  3. Add django and save
  4. With your virtual environment active run pip install -r requirement.txt

Setup

Now that you have installed everything, ensure that all of them are properly installed. For django use django-admin help and for docker use docker help. If all of them return results that you are all set.

You see how easy it is, imagine all that time you spend creating docker files. You can go through the docker files that is has created for you.

View the files create

It has created four files for us: DockerFile, compose.yaml, .dockerignore and README.Docker.md . If l'm honest l have never written such docker files on my own. Many thanks to the team at docker for implementing this.

DockerFile
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.10.10
FROM python:${PYTHON_VERSION}-slim as base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.com/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8000

# Run the application.
CMD python manage.py runserver

Look at that everything is in there and it also includes helpful comments. I really like it. You can also take a look at the other files that docker init has created for you. But for now let's move on to the next way.

Cookiecutter Django

Cookiecutter Django is a framework for jumpstarting production-ready Django projects quickly. It supports Docker using docker-compose for development and production (using Traefik with LetsEncrypt support).

Installation

  1. First install cookiecutter pip install cookiecutter

How to use it?

Get cookiecutter django with this command cookiecutter https://github.com/cookiecutter/cookiecutter-django

This command will start asking use a number of questions. Since this guide is not about cookiecutter l will skip those questions.

NB: Make sure the on docker question you select y, because by default it's set to no

With cookicutter it's a bit different because this is going to create more other files for django and deployment. The .dockerignore is in the main directory while the other a hidden. To find them follow this path:

yourcookiecutterdirectory
├── .dockerignore
├── compose/local/django/Dockerfile
└── myvenv/

You will see that you only have a Dockerfile and not docker-compose.yml because the other settings will be in start, entrypoint and some under nginx/ directory.

Conclusion

So you see now you don't need to stress yourself thinking about docker files. I think the cookiecutter method will require more deep knowledge of both django, docker and nginx. While the docker root is more straight forward and easy to manipulate.

Generally it's important to know the technology that you are using because in tech you are guaranteed that things go wrong and if you have little knowledge about it might be difficult for you to contemplate.