Django development environment construction using Docker-compose (personal memorandum)

Introduction

You don't need to be familiar with python or Django because the database server focuses on building a "postgres" environment. To start the project, create a working directory in any location. When using docker-compose, the directory name where the docker-compose.yml file is located is used as a prefix for the container name and volume name, so in the actual environment it is ** meaningful such as the project name. Create a directory name **.

Creating a directory

-#Create a directory called django
% mkdir django
-#Move to django directory
% cd django

Don't include unnecessary files to make the django directory the build context.

Create a Dockerfile to create an image of the Python execution environment.

% vim Dockerfile

Dockerfile


#Specify the image of the execution environment of python3
FROM python:3
#Specify 1 for the environment variable PYTHONUNBUFFERED. You can invalidate the buffer by setting something in this environment variable.
ENV PYTHONUNBUFFERED 1
#Create code directory
RUN mkdir /code
#Move working directory to code directory
WORKDIR /code
#Requirements in the build context.Put txt in code directory
COPY requirements.txt /code/
#You are running a pip installation. pip is a python package tool
# -requirements specified by r.Execute the installation of the package described in txt.
# requirements.txt is created in the build context but describes the package name of the django and Postgres drivers.
RUN pip install -r requirements.txt
#All the contents of the build context/It is placed in the code.
COPY . /code/

Creating requirements.txt

% vim requirements.txt
-# version2.0 Django installation
Django==2.0
-#Driver for connecting to postgres with python
psycopg2

Create docker-compose.yml

docker-compose.yml


version: '3'
services: 
  db:
    image: postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000 
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

It is assumed that two containers, "db" and "web", will start in ** services **. Since dots are defined in build in ** web **, build and use the image from the Dockerfile defined earlier. Use "postgres" SQL image for db. ** command ** means the command executed when the container starts. Here, "manage.py" is executed with "python3", and the IP address and port number to listen to are specified as "runserver" that starts a lightweight server for development as an argument. "Manage.py" is a file that is automatically generated when you install Django. However, the command described here will be overwritten if the command is passed when the container is executed. Therefore, if you do not pass the command as an argument, the docker-compose command will be executed. ** valunes ** bind mounts the current directory to / code. ** ports ** is published at 8000 and specified to be transferred to container 8000. It is necessary to match the port number of "runserver" with the port number of the forwarding destination. ** depends_on ** Define a dependency so that the "db" service starts before starting the "web" service.

Create a Django project

% docker -compose run web django-admin.py startproject examplepj .

Specify the "web" service defined in yml in the argument of docker-compose run. After that, the command to be executed when the web service container is started. django-admin.py is also created by installing Django. Here, run django-admin.py and create a django project with "start project". The project name is examplepj and the save destination is the current directory. The current directory will be the / code directory specified in the Dockerfile.

% ls -l

You can see that the current current directory and the container's / code directory are bind-mounted.

Describe the database settings and start the development web server.

% vim examplepj/settings.py

settings.py


ALLOWED_HOSTS = ['*']

----abridgement----

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql', #Specify postgresql for ENGINE
        'NAME': 'postgres',                        #Postgres specification
        'USER': 'postgres',                        #Postgres specification
	'PASSWORD': 'password',
        'HOST': 'db',                              #docker-Since the container launched by compose can perform name resolution by service name, the HOST can be specified as db.
        'PORT': 5432,                              #Specify the default port 5432 for postgresql
    }
}

Start each service

% docker-compose up -d
Starting django_db_1 ... done
Creating django_web_1 ... done

You can confirm that it is connected with Ip: 8000. Note that if you run a management script such as manage.py, it may fail if you don't run it in a container. Since the execution environment of python is prepared in the container, the script execution of python itself is executed in the container.

Django tutorial polls application creation

-#Create polls application files in your project
% docker-compose run web python3 manage.py startapp polls
-# views.py page changes
% vim polls/views.py

views.py


from django.http import HttpResponse

# Create your views here.
def index(request):
  return HttpResponse("Hello, world. You're at the polls index.")

This defines the message that will be displayed when the URL "polls" is accessed.

Route setting for polls.index

% vim polls/urls.py

urls.py


from django.urls import path

from . import views

urlpatterns = [
  path('', views.index, name='index'),
]

It is defined to display the result of the created index method.

Set to read the created urls.py from the main urls.py.

% vim examplepj/urls.py

urls.py


urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]

This will bring up the index page at ip: 8000 / polls /. By adding the source code in this way, you can proceed with development while running django in the container.

Stop container

% docker-compose stop

Recommended Posts

Django development environment construction using Docker-compose (personal memorandum)
Construction of data analysis environment using Docker (personal memorandum)
CentOS8 + Anaconda + Django development environment construction procedure
Rails API server environment construction using docker-compose
Development environment construction using IntelliJ IDEA + Maven + Tomcat 9
java development environment construction
Stable development environment construction manual for "Rails6" with "Docker-compose"
What is Docker-compose? (Personal memorandum)
Environment construction for Servlet application development
[Unity] Android development environment construction procedure
Laravel development environment construction with Docker (Mac)
Sapper × Go (echo) × Docker development environment construction
Universal Robotics UR Caps development environment construction
Java development environment construction memo on Mac
Building a Kotlin development environment using SDKMAN
Build a hot reload development environment with Docker-compose using Realize of Go
[Docker] Development environment construction Rails6 / Ruby2.7 / MySQL8
Redmine (Scrum, Agile plug-in) environment construction memorandum
Spring Boot + Docker Java development environment construction
[Personal memo] Java development environment is ready
Build a simple Docker + Django development environment
Prepare Nuxt (web) + Laravel (API) development environment in the same repository using docker-compose
[Jakarta EE 8 application development with Gradle] 1. Environment construction
Streamlining Web system development using docker-compose and Git
Environment construction procedure for using PowerMock with JUnit
Kaggle environment construction using official Docker and vscode
Data management using volume in Docker (personal memorandum)
Build a simple Docker Compose + Django development environment
[Note] Struts2 environment construction using Gradle in Eclipse
Allow development in Eclipse environment using iPLAss SDK
Ruby on Rails development environment construction on M1 Mac
React + Django + Nginx + MySQL environment construction with Docker
Wordpress local environment construction & development procedure with Docker
Java environment construction
Java development environment
[Spring] Environment construction
Docker environment construction
Create a Java development environment using jenv on Mac
[Environment construction] Ruby on Rails 5.2 system development environment construction [within 1 hour]
Introduction to Slay the Spire Mod Development (2) Development Environment Construction
Java development environment construction on Mac-JDK Install (2020 preservation version)
BEAR application Docker development environment construction example (docker-sync, Mutagen)
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Data management using Docker bindmount and tmpfs (personal memorandum)
Windows10 + WSL2 + DockerDesktop + docker-compose + GPU (Nvidia) + Jupyterlab environment construction
TDD study # 1 environment construction & first test-driven development (July 6th, 2020)
Ruby on Rails environment construction using VirtualBox, Vagrant, cyberduck
[Environment construction] Build a Java development environment with VS Code!
Try to build a Java development environment using Docker
Java + Spring development environment construction with VirtualBox + Ubuntu (Xfce4)