I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry

Introduction

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

Quote: Python Package Management Best Practices for 2020

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.

table of contents

  1. Premise
  2. Operating environment
  3. Create Dockerfile
  4. Create a Django project (#a)
  5. Create docker-compose.yml
  6. Start Container (#a)
  7. Finally

Premise

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.

Operating environment

Creating a Dockerfile

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

Django project creation


#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 docker-compose.yml

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

Start container


$ 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!

django-first.png

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!

Finally

Thank you for your hard work.

Source: https://github.com/k4ssyi/django-docker

https://twitter.com/k4ssyi

Recommended Posts

I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a development environment with Poetry Django Docker Pycharm
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
[Memo] Build a development environment for Django + Nuxt.js with Docker
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
[Python] Build a Django development environment with Docker
[Django] Build a Django container (Docker) development environment quickly with PyCharm
I made a WEB application with Django
Build Django + NGINX + PostgreSQL development environment with Docker
Build the fastest Django development environment with docker-compose
I created a Dockerfile for Django's development environment
Build a Django development environment with Doker Toolbox
Get a quick Python development environment with Poetry
I made a ready-to-use syslog server with Play with Docker
I made a window for Log output with Tkinter
Launch Django on a Docker container with docker-compose up
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Create a django environment with docker-compose (MariaDB + Nginx + uWSGI)
Build a Django environment for Win10 (with virtual space)
I made a resource monitor for Raspberry Pi with a spreadsheet
Get a clean Python development environment with pyenv + pipx + Poetry
Create a simple Python development environment with VSCode & Docker Desktop
Create a Todo app with Django ① Build an environment with Docker
Deploy a Django application with Docker
[For beginners] Django -Development environment construction-
I made a fortune with Python.
I made a daemon with Python
Rebuild Django's development environment with Docker! !! !! !!
I created an environment for Masonite, a Python web framework similar to Laravel, with Docker!
Django development environment construction with Docker-compose + Nginx + uWSGI + MariaDB (macOS edition)
I made a lot of files for RDP connection with Python
Postgres environment construction with Docker I struggled a little, so note
Create a simple Python development environment with VS Code and Docker
I made a dash docset for Holoviews
A simple RSS reader made with Django
Easily build a development environment with Laragon
I made a character counter with Python
Build a Fast API environment with docker-compose
Get a local DynamoDB environment with Docker
[Linux] Build a jenkins environment with Docker
I made CORS custom middleware with Django
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I made a roguelike game with Python
Building a Python development environment for AI development
Creating a development environment for machine learning
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a neuron simulator with Python
[Linux] Build a Docker environment with Amazon Linux 2
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
[Django] I made a field to enter the date with 4 digit numbers
Environment maintenance made with Docker (I want to post-process GrADS in Python
I tried to build a Mac Python development environment with pythonz + direnv
I made a webAPI! Build environment from Django Rest Framework 1 on EC2
I made a stamp substitute bot with line
Build a C language development environment with a container
I made a python dictionary file for Neocomplete