31
39

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 5 years have passed since last update.

Django2(Python3)+Mysqlの開発環境をDockerで構築

Last updated at Posted at 2018-02-09

(macOS10.12.6, Django2.0.2, python3.6.4, MySQL5.7)

公式はポスグレ版のみしか書いてないのでmysqlで。

前提

Docker for Mac と Django をインストールしておく

Django

プロジェクト作成

公式とは順番変える。先にローカルにプロジェクト作ってしまう。
(プロジェクト名は暫定で webapp としておく)

$ django-admin startproject core
$ mv core webapp
$ cd webapp

Djangoの設定を修正する

core/settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django',
        'USER': 'django',
        'PASSWORD': 'django',
        'HOST': 'db',
        'PORT': 3306,
    }
}

pythonライブラリ

requirements.txt
Django>=2.0.1
mysqlclient

docker

ファイル作成

docker-compose.yml
version: '3'
services:
 db:
 image: mysql:5.7
 command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
 volumes:
 - .:/var/www/webapp
 restart: always
 environment:
 MYSQL_ROOT_PASSWORD: django
 MYSQL_DATABASE: django
 MYSQL_USER: django
 MYSQL_PASSWORD: django
 web:
 build: .
 command: sh -c "./wait-for-it.sh db:3306; python3 manage.py runserver 0.0.0.0:8000"
 volumes:
 - .:/var/www/webapp
 ports:
 - "8080:8000"
 depends_on:
 - db
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /var/www/webapp
WORKDIR /var/www/webapp
ADD requirements.txt /var/www/webapp/
RUN pip install -r requirements.txt
ADD . /var/www/webapp/

コンテナを作成・起動

$ docker-compose up -d

動作確認

スクリーンショット 2018-02-09 16.41.51.png

管理画面

migrate して管理者を作成

$ docker-compose run web python manage.py migrate
$ docker-compose run web python manage.py createsuperuser

動作確認

スクリーンショット 2018-02-09 16.42.02.png

ファイル構成

$ tree -I __pycache__
.
├── Dockerfile
├── core
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── docker-compose.yml
├── manage.py
├── requirements.txt
└── wait-for-it.sh

1 directory, 9 files
31
39
1

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
31
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?