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

More than 1 year has passed since last update.

【はじめてのwebアプリ開発】Django + MySql + phpMyAdmin のDocker環境構築

Last updated at Posted at 2023-03-26

はじめに

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:

参考サイト

Django+MySQLの開発環境をdocker-composeで構築する

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