[DOCKER] I made a command to wait for Django to start until the DB is ready

It was inconvenient to use Docker-compose, so I made it.

https://github.com/pistatium/dj_database_waiter

what's this

A Python command that just waits for a Database connection to be established.

 dj_database_waiter myproject.settings

Just pass the Django configuration module as an argument and it will work. (Strictly stop)

When you launch DB and Django with docker-compose, Docker will wait for the Django side container to launch until the minimum network communication is possible (?), But until initialization such as initial SQL input. Will not wait firmly. It starts without permission and the container ends without permission. Hard Even if you look up the solution with StackOverflow, it is written that you have to wait until it is prepared with a shell script, which is quite painful. I don't want to install the DB Client on Python's Docker just to check communication ...

So it's better to write a script to check communication in Python. However, I thought it would be troublesome to copy the script for each project and pass the connection information separately from Django, so I made it a command and entered it with pip.

At first, I implemented it as a Django command so that it can be called from admin.py, but I noticed that the subcommand startup itself failed before the DB started, and rewrote it as a normal command that cries.

use

docker-compose.yml


version: '3'

services:
  web:
    build: .
    command: python3 myproject/manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/src
    ports:
      - 8000:8000
    depends_on:
      - db
  db:
    image: mysql
    volumes:
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
      - db-volume:/var/lib/mysql
volumes:
  db-volume:

If this kind of docker-compose is up and it doesn't start up at once, install dj_database_waiter at build time.

docker-compose.yml


    command: /bin/sh dj_database_waiter myproject.settings && python3 myproject/manage.py runserver 0.0.0.0:8000

If you replace command like this, you will be able to start it once.

Bonus: NamedTuple Fun

dj_database_waiter wants to use from typing.NamedTuple, so ** only ** requires Python 3.6.1 or higher. Please understand it.

dj_database_waiter/cmd.py


class DbStatus(NamedTuple):
    ok: bool
    reason: str = None


def check_status(db_group_name: str) -> DbStatus:
    try:
        ...
    except Exception as e:  # NOQA
        return DbStatus(ok=False, reason=str(e))
    return DbStatus(ok=True)

Since there are two arguments, there is almost no need to use NamedTuple, but it can be written in an easy-to-understand manner like this. It's similar to CaseClass in Scala. I also write type information, so if I write it using an IDE like IntelliJ, it will perform crunchy interpolation and error checking, so it is very good for mental health. (By the way, the runtime type is not checked, so even if you enter the wrong type, it will work.)

Recommended Posts

I made a command to wait for Django to start until the DB is ready
I made a command to markdown the table clipboard
I made a command to generate a table comment in Django
I made a command to display a colorful calendar in the terminal
[Django] I made a field to enter the date with 4 digit numbers
I made an appdo command to execute a command in the context of the app
I want to create a lunch database [EP1] Django study for the first time
I want to create a lunch database [EP1-4] Django study for the first time
Made a command for FizzBuzz
I want to start a jupyter environment with one command
I made you to execute a command from a web browser
I want to create a Dockerfile for the time being.
I made a lo command that is more useful than ls
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Start Django for the first time
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
AtCoder writer I wrote a script to aggregate the contests for each writer
I want to identify the alert email. --Is that x a wildcard? ---
I made a library konoha that switches the tokenizer to a nice feeling
I made a program to check the size of a file in Python
I made a function to see the movement of a two-dimensional array (Python)
[C language] My locomotive is too slow ~ I made a sl command ~
I made a dash docset for Holoviews
I made a script to display emoji
I made a library for actuarial science
I made a WEB application with Django
I made a tool to estimate the execution time of cron (+ PyPI debut)
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
I tried to explain what a Python generator is for as easily as possible.
I failed to install django with pip, so a reminder of the solution
I want to add silence to the beginning of a wav file for 1 second
I made a tool to generate Markdown from the exported Scrapbox JSON file
Let's display a simple template that is ideal for Django for the first time
I searched for the skills needed to become a web engineer in Python
I made a tool to automatically back up the metadata of the Salesforce organization
I made a program to look up words on the window (previous development)
I made a script to record the active window using win32gui of Python
I want to be notified when the command operation is completed on linux!
I made a python dictionary file for Neocomplete
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a downloader for word distributed expression
I made a tool to compile Hy natively
I made a tool to get new articles
I made a peeping prevention product for telework.
Create a command to get the work log
I want to upload a Django app to heroku
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
[Python] I made a system to introduce "recipes I really want" from the recipe site!
I made a small donation to the non-profit organization "Open Source Robot Foundation" OSRF
[Django] Hit a command you made from within the process that runs on manage.py.
I can't sleep until I build a server !! (Introduction to Python server made in one day)
A command to check when something goes wrong when the server is not doing anything
I forgot to operate VIM, so I made a video for memorization. 3 videos by level
How to test the current time with Go (I made a very thin library)
I tried to create a table only with Django
How to write a GUI using the maya command
I made a user management tool for Let's Chat
I want to scroll the Django shift table, but ...
I made a library to separate Japanese sentences nicely
What is a dog? Django App Creation Start Volume--startapp