2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

docker上でdjangoの環境を構築

Last updated at Posted at 2022-02-10

目的

docker を用いて Django の環境を構築する。
接続先の DB (PostgreSQL) も docker コンテナを利用する。

Django の環境を用意

手順としては下記です。

  1. Dockerfile, docker-compose.yml を作成
  2. イメージを作成
  3. イメージから Django のプロジェクトフォルダを抽出
  4. migrate

いったんイメージからプロジェクトフォルダの抽出作業を行っている理由としては、プロジェクトフォルダが無い状態で docker-compose up -d をするとプロジェクトフォルダ内が空の状態になってしまったためです。

1. Dockerfile, docker-compose.yml 作成

database ディレクトリも作成し、以下のディレクトリ構成とします。

.
├── Dockerfile
├── database # 空ディレクトリ
└── docker-compose.yml
Dockerfile
FROM python:3

ENV PYTHONUNBUFFERED 1

RUN python -m pip install --upgrade pip \
# PostgreSQL 以外を使用する場合は psycopg2 ではなく、対応するデータベースバインディングに変更
    && pip install Django psycopg2 \
    && pip cache purge \
    && django-admin startproject sample \
# DB の設定を修正
    && sed -i sample/sample/settings.py \
        -e "s/'ENGINE': 'django.db.backends.sqlite3',/'ENGINE': 'django.db.backends.postgresql',/" \
        -e "s/'NAME': BASE_DIR \/ 'db.sqlite3',/'NAME': 'mydb',/" \
        -e "/'NAME': 'mydb',/a<SPACE>'USER': 'user'," \
    && sed -i sample/sample/settings.py \
        -e "/'USER': 'user',/a<SPACE>'PASSWORD': 'pass'," \
    && sed -i sample/sample/settings.py \
        -e "/'PASSWORD': 'pass',/a<SPACE>'HOST': 'postgres',"  \
    && sed -i sample/sample/settings.py \
        -e "s/<SPACE>/        /"
docker-compose.yml
version: "3"
services:
    wait:
        image: dadarek/wait-for-dependencies
        container_name: "wait"
        depends_on:
            - postgres
    postgres:
        image: postgres:10
        container_name: "postgres"
        environment:
            POSTGRES_USER: user
            POSTGRES_PASSWORD: pass
            POSTGRES_DB: mydb
        ports:
            - "15432:5432"
        volumes:
            - ./database:/var/lib/postgresql/data
        networks:
            - db-net
    django:
        build: .
        image: "django"
        container_name: "django"
        working_dir: /sample
        command: python manage.py runserver 0.0.0.0:8000
        volumes:
            - ./sample:/sample
        ports:
            - "8000:8000"
        networks:
            - db-net
        depends_on:
            - postgres
            - wait

volumes:
    database:
        driver: local

networks:
    db-net:
        driver: bridge

2. イメージを作成

docker-compose build を実行

3. イメージから Django のプロジェクトフォルダを抽出

作成したイメージには django-admin startproject sample を実行した直後のプロジェクトフォルダが入っている。
イメージから直接ファイルを取得はできないらしいので、一度コンテナを生成してからホストマシン側に取得する。

> CONTAINER_ID=`docker create django`
> docker cp $CONTAINER_ID:/sample/ .
> docker rm $CONTAINER_ID

4. migrate

いったんコンテナを起動し、migrate を実施して起動しなおす。

> docker-compose up -d
> docker-compose run django python manage.py migrate
> docker-compose restart

localhost:8000 に接続できれば OK

おわり

上記設定ができれば sample 配下で開発を実施し、docker-compose up -d でサーバを起動・動作確認ができる。
テスト等、各種コマンドの実行は下記のように docker-compose exec django [実行コマンド] とすればよい。

docker-compose exec django  python manage.py test sample
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?