3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

こんにちは、エンジニアのkeitaMaxです。

今回はDjangoのDocker環境を作成していこうと思います。

Dockerの公式にクイックスタートがあったのでこれで作成していきます。

Djangoの環境をDockerで表示することを目標とします。
python3 manage.py runserverとかはいちいち叩くとめんどくさいので、叩かなくても起動できるようにしようと思います。

各ファイルの作成

以下のようにファイルを作成しました。

project
├── docker
│   ├── app
│   │   ├── Dockerfile
│   │   └── requirements.txt
│   └── db
│       ├── Dockerfile
│       └── my.conf
└── docker-compose.yml

各ファイルは以下のようになります。(公式を参考に作成しています。)

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

app/requirements.txt
Django==5.0

db/Dockerfile
FROM mysql:8.3

COPY ./my.conf /etc/my.conf
RUN chmod 644 /etc/my.cnf

db/my.conf
[mysqld]
# character
character_set_server = utf8mb4
collation_server = utf8mb4_0900_ai_ci

# timezone
default-time-zone = SYSTEM
log_timestamps = SYSTEM

# Error Log
log-error = mysql-error.log

# Slow Query Log
slow_query_log = 1
slow_query_log_file = mysql-slow.log
long_query_time = 1.0
log_queries_not_using_indexes = 0

# General Log
general_log = 1
general_log_file = mysql-general.log

[mysql]
default-character-set = utf8mb4

[client]
default-character-set = utf8mb4

docker-compose.yml
version: '3'

services:
  app:
    build:
      context: ./docker/app
      dockerfile: Dockerfile
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - "8000:8000"
    volumes:
      - ./src/:/code
    tty: true

  db:
    build:
      context: ./docker/db
      dockerfile: Dockerfile
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: database
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
      TZ: "Asia/Tokyo"
    volumes:
      - mysql-volume:/var/lib/mysql

volumes:
  mysql-volume:

Docker実行してプロジェクトを作成

以下コマンドで実行します。

docker-compose run app django-admin startproject app .

これでsrcディレクトリができ、その中にDjangoのファイルが作成されました。

実行

以下コマンドでDockerを立ち上げます。

docker compose up -d
[+] Running 2/2
 ✔ Container django-docker-example-app-1  Started                                                                                                                        0.2s 
 ✔ Container django-docker-example-db-1   Started 

実行するとこのように、Createdなどの文字が出てきます。

実行後、以下URLにアクセスをすると初期の画面が表示されます。

http://localhost:8000/

スクリーンショット 2024-07-16 14.15.16.png

これで完了です!

おまけ

おまけで.gitignoreは以下みたいな感じで置いてます。

.gitognpre
*.egg-info
*.pot
*.py[co]
.tox/
__pycache__
MANIFEST
dist/
docs/_build/
docs/locale/
node_modules/
tests/coverage_html/
tests/.coverage*
build/
tests/report/
tests/screenshots/

これを置いています。

おわりに

この記事での質問や、間違っている、もっといい方法があるといったご意見などありましたらご指摘していただけると幸いです。

最後まで読んでいただきありがとうございました!

参考

次の記事

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?