はじめに
Django+MySqlを使用するDocker環境を構築したので記録として残します。開発を進める上で適宜修正していく予定です。
前提環境
PC: MacBook Air M2
OS: macOS Ventura Ver. 13.2.1
Homebrew: 4.0.9
Docker: 20.10.23
docker-compose: v2.15.1
プロジェクト作成後のディレクトリ構成
プロジェクト名はtwoo
です。
twoo
├── reservation
│ ├── __init__.py
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── __pychache__
├── manage.py
├── Dockerfile
├── requirements.txt
└── docker-compose.yml
PythonのDockerfile
Dockerfile(Python)
FROM python:3.11.0
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /code/
requirements.txt
requirements.txt
Django==4.2
mysqlclient
docker-compose.yml
docker-compose.yml
version: '3.9'
services:
db:
container_name: mysql
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
TZ: 'Asia/Tokyo'
command: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci
healthcheck:
test: ["CMD-SHELL", "mysqladmin ping -h localhost -u $$MYSQL_ROOT_USER -p$$MYSQL_ROOT_PASSWORD || exit 1"]
interval: 30s
timeout: 5s
retries: 3
start_period: 10s
volumes:
- db_data:/var/lib/mysql
django:
container_name: django
build: .
command: python manage.py runserver 0.0.0.0:8000
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_ROOT_USER: ${MYSQL_ROOT_USER}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
db:
condition: service_healthy
phpmyadmin:
image: phpmyadmin/phpmyadmin
platform: linux/amd64
depends_on:
db:
condition: service_healthy
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_ROOT_USER: ${MYSQL_ROOT_USER}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
ports:
- "3000:80"
volumes:
- ./phpmyadmin/sessions:/sessions
volumes:
db_data: