Create a Django development environment using Docker and docker-compose. There are already articles written by other people that create a development environment with Docker, but many use requirements.txt for library management, and the current package management is mainly using Pipenv or Poetry. I think there is a trend that is becoming more and more
I thought that it would be better to use Poetry, which allows you to see the dependencies at a glance even when developing with Docker, so I wrote this article.
Also, I personally don't like to start a container with Docker command in the development environment (docker-compose is easier), so I'm writing how to start a container with docker-compose.
Understand basic docker commands and poetry commands. Docker is already installed docker-compose is also installed
I will omit the installation method because there are many articles written by other people.
Create an empty Dockerfile
$ touch Dockerfile
Use the official Docker Hub Python image. Write the Dockerfile as follows. Since alpine takes a long time to build, use normal ubuntu base.
FROM python:3.8
WORKDIR /app
ENV PYTHONPATH /app
ENV LC_ALL=C.UTF-8 LANG=C.UTF-8
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
vim \
&& rm -rf /var/lib/apt/lists/*
#pip update
RUN pip install --upgrade pip setuptools wheel
#Install Poetry
RUN pip install poetry
#Disable creating virtual environment inside container
RUN poetry config virtualenvs.create false
RUN poetry config virtualenvs.in-project true
CMD ["bash"]
Poetry's default settings will try to create a virtual environment during installation poetry config virtualenvs.create false poetry config virtualenvs.in-project true Let's disable it with.
Build the created Dockerfile. This time we will create an image named "django".
$ docker build -t django .
$ docker run -itd --name django-setting django
Go inside the container and install the required libraries and create a new Django project.
# django-Enter the setting container
$ docker exec -it django-setting bash
#Check if Poetry is installed
<container>:/app# pip list | grep poetry
poetry 1.0.3
#Poetry initial settings
<container>:/app# poetry init
Yes or skip is OK
console
Package name [app]:
Version [0.1.0]:
Description []:
Author [None, n to skip]: n
License []:
Compatible Python versions [^3.8]:
Would you like to define your main dependencies interactively? (yes/no) [yes]
Search for package to add (or leave blank to continue):
Would you like to define your dev dependencies (require-dev) interactively (yes/no) [yes]
Search for package to add (or leave blank to continue):
Do you confirm generation? (yes/no) [yes]
Make sure pyproject.toml is created under / app and install django
<In the container>:/app# ls
pyproject.toml
<In the container>:/app# poetry add django
#The project name is project.
<In the container>:/app# django-admin startproject project .
#Confirm that it has been created
<In the container>:/app# ls
manage.py poetry.lock project pyproject.toml
Exit the container with ctrl + d.
Copy the created project from inside the container to the host's src directory
#Check container ID
$ docker ps
$ docker cp <Container ID>:/app/ src
Update the Dockerfile. Describe to copy pyproject.toml that was copied from the container to the host earlier into the container By doing poetry install, the library written in pyproject.toml It will be installed in pip as soon as the image is created.
FROM python:3.8
WORKDIR /app
ENV PYTHONPATH /app
ENV LC_ALL=C.UTF-8 LANG=C.UTF-8
RUN apt-get update && apt-get install -y --no-install-recommends \
git \
vim \
&& rm -rf /var/lib/apt/lists/*
RUN pip install --upgrade pip setuptools wheel
RUN pip install poetry
RUN poetry config virtualenvs.create false
RUN poetry config virtualenvs.in-project true
COPY src/pyproject.toml pyproject.toml #add to
RUN poetry install #add to
CMD ["bash"]
I rewrote the Dockerfile, so delete the old container
$ docker ps #Container ID confirmation
$ docker rm -f <Container ID> # django-setting container deleted
Create an empty docker-compose.yml
$ touch docker-compose.yml
Now that the Dockerfile is ready, let's set up docker-compose.yml.
docker-compose.yml
version: "3"
services:
django:
build:
context: .
dockerfile: Dockerfile
ports:
- "8000:8000"
volumes:
- ./src:/app
command: "python3 manage.py runserver 0.0.0.0:8000"
tty: true
Final directory
.
├── Dockerfile
├── docker-compose.yml
└── src
├── manage.py
├── project
├── pyproject.toml
└── poetry.lock
$ docker-compose build
$ docker-compose up
console
Starting django-docker_django_1 ... done
Attaching to django-docker_django_1
django_1 | Watching for file changes with StatReloader
django_1 | Performing system checks...
django_1 |
django_1 | System check identified no issues (0 silenced).
django_1 |
django_1 | You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
django_1 | Run 'python manage.py migrate' to apply them.
django_1 |
django_1 | February 06, 2020 - 09:38:45
django_1 | Django version 3.0.3, using settings 'project.settings'
django_1 | Starting development server at http://0.0.0.0:8000/
django_1 | Quit the server with CONTROL-C.
http://localhost:8000 You should be able to see it when you access!
cat pyproject.toml
[tool.poetry]
name = "app"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
[tool.poetry.dependencies]
python = "^3.8"
django = "^3.0.3"
[tool.poetry.dev-dependencies]
[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
I can manage packages!
Thank you for your hard work.
Source: https://github.com/k4ssyi/django-docker
https://twitter.com/k4ssyi