LoginSignup
4
8

【Docker】入門:Djangoのコンテナを作成

Posted at

はじめに

UdemyでDockerの学習を進めている最中で、途中経過をアウトプットしたいと考えています。
今回は、DjangoをDockerで実行したいと思います。

環境

  • Windows10
  • Docker version 24.0.6
  • VScode
  • Git Bash

参考にしたサイト

プロジェクトのコンポーネントを定義

  • プロジェクト用のディレクトリを作成します
mkdir composeexample
cd composeexample
  • プロジェクトディレクトリのDockerfileを新規作成し、次の内容を加えます。
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install --upgrade pip && \
    pip install -r requirements.txt
ADD . /code/
  • プロジェクト・ディレクトリにrequirements.txtを生成し、必要なソフトウェアを記載します。
Django
psycopg2
  • プロジェクト・ディレクトリに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

Djangoプロジェクトの生成

docker-compose run web django-admin startproject composeexample .

※私の環境だと、参考サイトに記載のあるdocker-compose run web django-admin.py startproject composeexample .を実行すると

Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "django-admin.py": executable file not found in $PATH: unknown
time="2023-10-09T07:53:04+09:00" level=error msg="error waiting for container: "

というエラーが出ました。
コンテナ内のPATHで実行可能ファイル "django-admin.py" を見つけることができないため、コンテナが起動できないと言っているので、.pyをなくしたらいけました

  • 成されたファイルの所有者を以下のようにして変更します。
chown -R $USER:$USER .
  • ls -l コマンドで権限が更新されているかを確認します。
ls -l
$ ls -l
total 11
drwxr-xr-x 1 [user name] 197121   0 Oct  9 07:54 composeexample/
-rw-r--r-- 1 [user name] 197121 223 Oct  9 07:50 docker-compose.yml
-rw-r--r-- 1 [user name] 197121 149 Oct  9 07:47 Dockerfile
-rwxr-xr-x 1 [user name] 197121 670 Oct  9 07:54 manage.py*
-rw-r--r-- 1 [user name] 197121  16 Oct  9 07:48 requirements.txt

データベースへの接続設定

  • settings.pyDATABASE=...の部分を以下のように書き換えます。
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'HOST': 'db',
        'PASSWORD': 'postgres',
        'PORT': 5432,
    }
}
  • プロジェクトのトップ・ディレクトリにおいてコマンド docker-compose up -d を実行します。
docker-compose up -d
  • docker-compose exec web bashでコンテナに入ります。
docker-compose exec web bash
  • python manage.py migrateを実行します。
python manage.py migrate
  • superuserを登録します。
python manage.py createsuperuser

http://localhost:8000/ にアクセスし、ロケットが飛んだら成功です。
image.png

http://localhost:8000/admin で作成したsuperuserで管理者画面にログインできることも確認します。

POSTGRESQLコンテナに入ってみる

  • docker-compose exec db bashでコンテナに入ります。
docker-compose exec db bash
  • 以下のコマンドでログインします。
psql -h localhost -U postgres -d postgres
  • \dtでテーブルを確認してみます。
postgres=# \dt
                   List of relations
 Schema |            Name            | Type  |  Owner
--------+----------------------------+-------+----------
 public | auth_group                 | table | postgres
 public | auth_group_permissions     | table | postgres
 public | auth_permission            | table | postgres
 public | auth_user                  | table | postgres
 public | auth_user_groups           | table | postgres
 public | auth_user_user_permissions | table | postgres
 public | django_admin_log           | table | postgres
 public | django_content_type        | table | postgres
 public | django_migrations          | table | postgres
 public | django_session             | table | postgres

djangoのテーブルが作成されていました!

postgres=# SELECT * FROM auth_user;
 id |                                         password                                         |          last_login           | is_superuser | username | first_name | last_name | email | is_staff | is_active |          date_joined   

----+----+-------------------------------+-----+-------+----+---+--+---+----+-------------------------------
  1 |pass| 2023-10-09 13:23:24.426382+00 | t   | admin |    |   |  | t | t  | 2023-10-09 13:23:06.695555+00
(1 row)

super userも登録されているようです。

4
8
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
4
8