はじめに
Django4.2が2023年4月3日にリリースされました🎉✨
公式リリースノートはこちら: https://docs.djangoproject.com/en/4.2/releases/4.2/
待ちに待ったLTS(長期サポート)バージョンですね。Webフレームワークに限らずなのですが、長期サポートのバージョンを出してくれるのは嬉しいです。バージョンアップって結構大変になったりすることがありますからね。
本記事で書かないこと
- Django4.2のこと
- docker, docker-composeのインストール
- docker, docker-composeの細かい使い方
本記事で書くこと
- docker-composeを使ってDjango4.2のローカル環境を作る方法
目標
- docker-composeで立ち上げたDjango4.2のプロジェクトのトップページにブラウザからアクセスできるようにする
環境構築でやることリスト
- requirements.txtを作成
- Dockerfileを作成
- イメージ作成
- Djangoプロジェクトを作成
- docker-compose.yaml作成
- コンテナ起動
- ブラウザで確認
requirements.txtを作成
まずはDjangoで色々なライブラリをインストールするために必要な requirements.txt
から作っていきます。最初はシンプルにDjangoだけを指定します。
django==4.2
Dockerfileを作成
イメージの元となるDockerfileを使っていきます。今回は簡易的に環境構築を行うため、PythonのAlpineのイメージを使います。
また、Django4.2はPython 3.8, 3.9, 3.10, 3.11をサポートしています。今回は新しめである3.11を使います。
FROM python:3.11-alpine
ENV APP_PATH /app
RUN mkdir -p /app
WORKDIR $APP_PATH
COPY requirements.txt $APP_PATH/
RUN pip install --no-cache-dir -r requirements.txt
イメージ作成
以下のコマンドでイメージを作成します。
$ docker build -t django42_sample ./
[+] Building 4.2s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.11-alpine 0.7s
=> [1/5] FROM docker.io/library/python:3.11-alpine@sha256:4b4078a3ab81edc2f5725cd42b065beaeeb4f9be3b4b1e3b4cf11dd6ae9720d3 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 55B 0.0s
=> CACHED [2/5] RUN mkdir -p /app 0.0s
=> CACHED [3/5] WORKDIR /app 0.0s
=> [4/5] COPY requirements.txt /app/ 0.0s
=> [5/5] RUN pip install --no-cache-dir -r requirements.txt 3.0s
=> exporting to image 0.4s
=> => exporting layers 0.4s
=> => writing image sha256:8ddb54a36640826e040088ab64b7731768b28e039c78d0779a560e659c2e043e 0.0s
=> => naming to docker.io/library/django42_sample 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
イメージが作成されたかを確認します
$ docker images | grep django42
django42_sample latest 8ddb54a36640 56 seconds ago 97.2MB
Djangoプロジェクトを作成
作成したイメージでコンテナを起動し、Djangoプロジェクトを作成します。
プロジェクト名は django42_sample_project
とします。
$ docker run --rm --mount type=bind,src=$(pwd),dst=/app django42_sample:latest django-admin startproject django42_sample_project .
ディレクトリ内を見て、プロジェクト名のディレクトリと manage.py
が作成されていれば成功です。
$ ls
Dockerfile django42_sample_project docker-compose.yaml manage.py requirements.txt
docker-compose.yaml作成
Djangoのアプリケーションサーバしかないからdocker-composeはいらないでしょう、と思う人も多いかもですが、結局はDBやらのコンテナを追加するので、最初からdocker-composeで動くようにしてた方が楽です。
ということでdocker-compose.yamlを作っていきます。
version: '3'
services:
django:
restart: always
build: .
volumes:
- ./:/app
command: /bin/sh -c "cd /app; python manage.py migrate; python manage.py runserver 0:8000"
ports:
- 8000:8000
コンテナ起動
以下のコマンドでコンテナを起動します。
$ docker-compose up
Creating network "django42_sample_default" with the default driver
Building django
[+] Building 1.7s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 208B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/python:3.11-alpine 1.5s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 55B 0.0s
=> [1/5] FROM docker.io/library/python:3.11-alpine@sha256:4b4078a3ab81edc2f5725cd42b065beaeeb4f9be3b4b1e3b4cf11dd6ae9720d3 0.0s
=> CACHED [2/5] RUN mkdir -p /app 0.0s
=> CACHED [3/5] WORKDIR /app 0.0s
=> CACHED [4/5] COPY requirements.txt /app/ 0.0s
=> CACHED [5/5] RUN pip install --no-cache-dir -r requirements.txt 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:8ddb54a36640826e040088ab64b7731768b28e039c78d0779a560e659c2e043e 0.0s
=> => naming to docker.io/library/django42_sample_django 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
WARNING: Image for service django was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating django42_sample_django_1 ... done
Attaching to django42_sample_django_1
django_1 | Operations to perform:
django_1 | Apply all migrations: admin, auth, contenttypes, sessions
django_1 | Running migrations:
django_1 | Applying contenttypes.0001_initial... OK
django_1 | Applying auth.0001_initial... OK
django_1 | Applying admin.0001_initial... OK
django_1 | Applying admin.0002_logentry_remove_auto_add... OK
django_1 | Applying admin.0003_logentry_add_action_flag_choices... OK
django_1 | Applying contenttypes.0002_remove_content_type_name... OK
django_1 | Applying auth.0002_alter_permission_name_max_length... OK
django_1 | Applying auth.0003_alter_user_email_max_length... OK
django_1 | Applying auth.0004_alter_user_username_opts... OK
django_1 | Applying auth.0005_alter_user_last_login_null... OK
django_1 | Applying auth.0006_require_contenttypes_0002... OK
django_1 | Applying auth.0007_alter_validators_add_error_messages... OK
django_1 | Applying auth.0008_alter_user_username_max_length... OK
django_1 | Applying auth.0009_alter_user_last_name_max_length... OK
django_1 | Applying auth.0010_alter_group_name_max_length... OK
django_1 | Applying auth.0011_update_proxy_permissions... OK
django_1 | Applying auth.0012_alter_user_first_name_max_length... OK
django_1 | Applying sessions.0001_initial... OK
django_1 | Watching for file changes with StatReloader
ブラウザで確認
ブラウザで localhost:8000
にアクセスしてみましょう。

無事にプロジェクトが立ち上がりました🎉
画面右上に Django4.2
とありますね 👍
これを基に、MySQLと繋げたり、色々やっていきたいと思います。
最後までお読みいただきありがとうございました!