[Python] Build a Django development environment with Docker

We will use Docker and Docker-compose to build a development environment for Django and postgreSQL. Hit http: // localhost: 8000 until the screen shown below is displayed.

スクリーンショット 2020-08-28 13.44.31.png

Django installation

If you already have Django, you don't have to.

pip install Django

Docker installation

Install Docker from here

** Check version **

$ docker --version
Docker version 18.09.2

Install Docker-compose

Install Docker-compose with the following command.

$ curl -L https://github.com/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

$ chmod +x /usr/local/bin/docker-compose

** Check version **

$ docker-compose --version
docker-compose version 1.23.2

Create a directory for Django

$ mkdir docker-demo-with-django && cd docker-demo-with-django
$ mkdir app && cd app

Create Pipfile in app

$ vi Pipfile

/app/Pipfile

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
Django = "==3.1"

[requires]
python_version = "3.7.5"

Introduced pipenv

Use the following command to install and log in.

$ pip install pipenv
$ pipenv install
$ pipenv shell

Start of Django project

Use the following command to start the project and start the server.

$ django-admin.py startproject django_demo .
$ python manage.py migrate
$ python manage.py runserver

Build a Docker container

From here, we will set up the Docker container.

Dockerfile

Create a Dockerfile

$ vi Dockerfile

app/Dockerfile

#Python3 from the official.7 on alpine linux image pull
FROM python:3.7-alpine

#Set working directory
WORKDIR /usr/src/app

#Set environment variables
#Prevent Python from writing to pyc files and discs
ENV PYTHONDONTWRITEBYTECODE 1
#Prevent Python from buffering standard I / O
ENV PYTHONUNBUFFERED 1

#Installation of psycopg2
RUN apk update \
    && apk add --virtual build-deps gcc python3-dev musl-dev \
    && apk add postgresql-dev \
    && pip install psycopg2 \
    && apk del build-deps

#Install Pipenv
RUN pip install --upgrade pip \
&& pip install pipenv

#Copy the host pipfile to the container's working directory
COPY ./Pipfile /usr/src/app/Pipfile

#Install packages from pipfile and build a Django environment
RUN pipenv install --skip-lock --system --dev

#Copy the host's current directory (currently the app directory) to your working directory
COPY . /usr/src/app/

# entrypoint.run sh
ENTRYPOINT ["/usr/src/app/entrypoint.sh"]                 

Create docker-compose.yml

vi ./../docker-compose.yml

docker-demo-with-django/docker-compose.yml

version: '3.7'

services:
    #Service name can be set freely
    django:
        #From within the app directory`Dockerfile`Find
        build: ./app
        #Set the command to be entered after starting the service
        command: python manage.py runserver 0.0.0.0:8000
        #Settings for persisting data.`host:container`Describe the path with
        volumes:
            - ./app/:/usr/src/app/
        #Specify the port to open.`host:container`List the port in
        ports:
            - 8000:8000
        #Specify environment variables
        environment:
            #1 is debug mode
            - DEBUG=1
            - SECRET_KEY=hoge
            - DATABASE_ENGINE=django.db.backends.postgresql
            - DATABASE_DB=django_db
            - DATABASE_USER=django_db_user
            - DATABASE_PASSWORD=password1234
            - DATABASE_HOST=postgres
            - DATABASE_PORT=5432
        #Specify the service to connect to
        depends_on:
            - postgres

    postgres:
        #Pull the image from the official
        image: postgres:11.4-alpine
        #Database persistence
        #At the beginning so as not to mount in the host directory`./`Do not attach
        volumes:
            - postgres_data:/var/lib/postgresql/data
        #Create a database with su privileges and the same name as the specified user
        #value is the same as the one specified in the django service
        environment:
            - POSTGRES_USER=django_db_user
            - POSTGRES_PASSWORD=password1234
            - POSTGRES_DB=django_db

#"Named volumes" written at the top level can be referenced from multiple services
volumes:
    postgres_data:         
#!/bin/sh

if [ "$DATABASE" = "postgres" ]
then
    echo "Waiting for postgres..."

    while ! nc -z $DATABASE_HOST $DATABASE_PORT; do
      sleep 0.1
    done

    echo "PostgreSQL started"
fi

python manage.py flush --no-input
python manage.py migrate

exec "$@"

Build and launch

$ docker-compose up -d --build

Connect to the following URL, and if you can connect, the construction is completed http://localhost:8000

Recommended Posts

[Python] Build a Django development environment with Docker
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
[Django] Build a Django container (Docker) development environment quickly with PyCharm
Build Django + NGINX + PostgreSQL development environment with Docker
Build a Django development environment with Doker Toolbox
Quickly build a Python Django environment with IntelliJ
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build Mysql + Python environment with docker
How to build a Django (python) environment on docker
Build a machine learning application development environment with Python
Easily build a development environment with Laragon
Build Jupyter Lab (Python) environment with Docker
How to build a python2.7 series development environment with Vagrant
Create a simple Python development environment with VSCode & Docker Desktop
[Linux] Build a jenkins environment with Docker
Create a Todo app with Django ① Build an environment with Docker
Build a python virtual environment with pyenv
Build a modern Python environment with Neovim
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
[Linux] Build a Docker environment with Amazon Linux 2
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Create a simple Python development environment with VS Code and Docker
Build the fastest Django development environment with docker-compose
Build Python development environment with Visual Studio Code
Go (Echo) Go Modules × Build development environment with Docker
Create a python3 build environment with Sublime Text3
Build a Django environment with Vagrant in 5 minutes
Create Nginx + uWSGI + Python (Django) environment with docker
Build a Python development environment on your Mac
Build a Python environment with OSX El capitan
Build PyPy and Python execution environment with Docker
Build a Python machine learning environment with a container
Build a Python development environment on Raspberry Pi
Build a python execution environment with VS Code
Get a quick Python development environment with Poetry
Build python3 environment with ubuntu 16.04
Prepare python3 environment with Docker
Build python environment with direnv
[ev3dev × Python] Build ev3dev development environment
Build a Python environment offline
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
I tried to build a Mac Python development environment with pythonz + direnv
Build a Django development environment using pyenv-virtualenv on Mac
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a python environment for each directory with pyenv-virtualenv
Create a python development environment with vagrant + ansible + fabric
Build a GVim-based Python development environment on Windows 10 (1) Installation
Build a Python development environment on Mac OS X
Build a Python virtual environment using venv (Django + MySQL ①)
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Python development environment using pyenv on MacOS
A memo about building a Django (Python) application with Docker
Set up a Python development environment with Sublime Text 2
Build a Django environment for Win10 (with virtual space)
Build a lightweight Fast API development environment using Docker
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
Build a go environment using Docker
Build a deb file with Docker
Deploy a Django application with Docker