長野エンジニアライフ

東京から長野に移住したエンジニアのブログです。🦒🗻⛰

(WIP)DockerでDjango環境を構築した時のメモ(1)

作業を行うdocker_djangoディレクトリを作成

  • web/Dockerfileの作成
FROM python:3.8

ENV PYTHONUNBUFFERED 1

RUN mkdir /code

WORKDIR /code

ADD requirements.txt /code/

RUN pip install -r requirements.txt

ADD . /code/
  • web/requirements.txtの作成
Django==3.1.1
uwsgi
mysqlclient
  • docker-compose.ymlの作成
version: '3'
services:
  # nginxコンテナの設定
  nginx:
      image: nginx
      ports:
        - "8000:8000"
      volumes:
        - ./nginx/conf:/etc/nginx/conf.d
        - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
        - ./static:/static
      depends_on:
        - django
  # DBコンテナの設定
  db:
    image: mysql:5.7
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: todoList
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      TZ: 'Asia/Tokyo'
    volumes:
      - ./mysql:/var/lib/mysql
      - ./sql:/docker-entrypoint-initdb.d
  # djangoコンテナの設定
  web:
    build: ./web
    command: uwsgi --socket :8001 --module app.wsgi --py-autoreload 1 --logto /tmp/mylog.log
    volumes: 
      - ./src:/code
      - ./static:/static
    expose:
      - "8001"
    depends_on:
      - db
GRANT ALL PRIVILEGES ON test_todoList.* TO 'user'@'%';

FLUSH PRIVILEGES;
  • uWSGIの設定(ここの手順は不要っぽい) nginx/uwsgi_paramsファイルの作成
uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;
  • 設定ファイル(nginx/conf/app_nginx.conf)の作成
upstream django {
    ip_hash;
    server python:8001;
}

server {
    listen      8000;
    server_name 127.0.0.1;
    charset     utf-8;

    location /static {
        alias /static;
    }

    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params;
    }
}

server_tokens off;

デフォルトでは/etc/nginx/conf.d配下にはdefault.conf(以下)が存在する




  • Djangoプロジェクトの作成
docker-compose run web django-admin.py startproject app .
  • mysqlとの接続 src/app/settings.pyの修正
"""
Django settings for app project.

Generated by 'django-admin startproject' using Django 3.1.1.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

# mysqlの設定 START
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'todoList',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': '3306',
    }
}
# mysqlの設定 END

docker-compose run web ./manage.py makemigrations
docker-compose run web ./manage.py migrate
  • 管理者の設定
docker-compose run web ./manage.py createsuperuser

userName: admin email:admin@nagano.com password:hogefuga1

  • djangoサイトのCSS適用 web/settings.pyの修正
# CSSの適用 START
STATIC_ROOT = '/static'
# CSSの適用 END
docker-compose run web ./manage.py collectstatic
  • コンテナの起動
docker-compose up -d

起動後、http://localhost:8000へアクセスできればOK

参考記事

qiita.com