A memo about building a Django (Python) application with Docker

Introduction

environment

Postscript

Procedure (preparation and start-up)

Create and move a directory for your project. Here, we will proceed with the application name linebot.

MacBook-Air ~ % mkdir jinolinebot
MacBook-Air ~ % cd jinolinebot
MacBook-Air jinolinebot % 

Create a Dockerfile and open it in an editor.

MacBook-Air jinolinebot % touch Dockerfile

Edit and save the Dockerfile.

Dockerfile


FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

Create requirements.txt and edit it as follows

MacBook-Air jinolinebot % touch requirements.txt

requirements.txt


Django>=1.8,<2.0
psycopg2

Create docker-compose.yml and edit it as follows

MacBook-Air jinolinebot % touch docker-compose.yml

docker-compose.yml


version: '3'

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

Now that the three files are ready, launch the app.

MacBook-Air jinolinebot % docker-compose run web django-admin startproject app name.
Creating network "jinolinebot_default" with the default driver
Building web
Step 1/7 : FROM python:3
 ---> 28a4c88cdbbf
~
Creating jinolinebot_db_1 ... done
Creating jinolinebot_web_run ... done

After starting up, the next step is to set up the connection to the database. Edit the configuration file generated by the previous command as follows and save it.

app name/settings.py


~
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}
~

Launch the container, and when the following display is displayed first, access http://0.0.0.0:8000/

MacBook-Air jinolinebot % docker-compose up
~
Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.

It worked! If you see Congratulations on your first Django-powered page.

Created docker file

https://github.com/yongjugithub/djangodocker

References

https://docs.docker.jp/compose/django.html#id2

Recommended Posts

A memo about building a Django (Python) application with Docker
Deploy a Django application with Docker
Run a Python web application with Docker
[Python] Build a Django development environment with Docker
Rails application building with Docker
Building a virtual environment with Python 3
Build a web application with Django
Application development with Docker + Python + Flask
[Memo] Build a development environment for Django + Nuxt.js with Docker
[Pyenv] Building a python environment with ubuntu 16.04
Building a Python3 environment with Amazon Linux2
If you know Python, you can make a web application with Django
A memo with Python2.7 and Python3 on CentOS
Building a Python 3.6 environment with Windows + PowerShell
[Python] A quick web application with Bottle!
I made a WEB application with Django
Building a python environment with virtualenv and direnv
Building a Python environment with WLS2 + Anaconda + PyCharm
Start a simple Python web server with Docker
A memo about writing merge sort in Python
Create Nginx + uWSGI + Python (Django) environment with docker
A story about implementing a login screen with django
A memo when creating a python environment with miniconda
Think about building a Python 3 environment in a Mac environment
Create a one-file hello world application with django
A simple to-do list created with Python + Django
Quickly build a Python Django environment with IntelliJ
Create a Python console application easily with Click
Django 1.11 started with Python3.6
[Python] Memo about functions
[Python] Memo about errors
Use python with docker
A story about making 3D space recognition with Python
Recommendation of building a portable Python environment with conda
Building a Docker working environment for R and Python
(Python) Try to develop a web application using Django
Deploy a Django application on Google App Engine (Python3)
A story about making Hanon-like sheet music with Python
How to build a Django (python) environment on docker
Web application made with Python3.4 + Django (Part.1 Environment construction)
Build a machine learning application development environment with Python
[Python] A memo to write CSV vertically with Pandas
Create a Layer for AWS Lambda Python with Docker
A memo that I touched the Datastore with python
Launch Django on a Docker container with docker-compose up
conda memorandum: Building a Python environment with supercomputer ITO
Live a Haml life with Django1.8 + Jinja2 + hamlish-jinja (Python3)
Build a development environment with Poetry Django Docker Pycharm
Memo for building a machine learning environment using Python
Do Django with CodeStar (Python3.6.8, Django2.2.9)
Twitter graphing memo with Python
Do Django with CodeStar (Python3.8, Django2.1.15)
Python3 + Django ~ Mac ~ with Apache
Getting Started with Python Django (1)
Create a homepage with django
Web application creation with Django
Getting Started with Python Django (3)
Prepare python3 environment with Docker
A memorandum about correlation [Python]
Make a fortune with Python
A memorandum about Python mock