フリーランス 技術調査ブログ

フリーランス/エンジニア Ruby Python Nodejs Vuejs React Dockerなどの調査技術調査の備忘録

dockerにDjango REST framework環境を作る

はじめに

インストール

Python (3.5, 3.6, 3.7, 3.8)
Django (1.11, 2.0, 2.1, 2.2, 3.0)
  • 下記をインストールする
pip install django
pip install djangorestframework
pip install django-filter

プロジェクト作成&アプリケーション作成

cd srv/
## backendというプロジェクトを作成する
django-admin startproject backend
cd backend
## backendプロジェクトにあるapiアプリケーションを作成する
django-admin startapp api
cd backend
# python manage.py runserver 0:8000
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
# python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

アクセスする

  • アクセスするとDisallowedHostとエラーになる。 この場合は、DEBUGをFalseにするかALLOWED_HOSTSにサーバのIPアドレスまたは自分のドメインを追加すると回避できる。 デバッグが見れないとテスト環境では困ることもあるので、本番環境ではDEBUG、テスト環境ではALLOWED_HOSTSを変更すると良い。 f:id:PX-WING:20200721082236p:plain

  • 回避するための設置変更する箇所

vi backend/backend/settings.py
ALLOWED_HOSTS = ["IP or Your Domain"]

再度アクセスする

f:id:PX-WING:20200721083048p:plain

Dockerfile

FROM alpine:latest

RUN apk update

RUN apk add --no-cache git bash build-base libffi-dev openssl-dev bzip2-dev zlib-dev readline-dev sqlite-dev 

ENV ENV="/root/.ashrc"

RUN git clone https://github.com/pyenv/pyenv.git ~/.pyenv

## dockerのコンテナログイン時にpyenvコマンドを実行するためにパスを設定しておく
RUN echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.ashrc
RUN echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.ashrc


RUN /root/.pyenv/bin/pyenv install 3.8.4
RUN echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.ashrc

RUN /root/.pyenv/bin/pyenv global 3.8.4

RUN /root/.pyenv/versions/3.8.4/bin/pip install django
RUN /root/.pyenv/versions/3.8.4/bin/pip install djangorestframework
RUN /root/.pyenv/versions/3.8.4/bin/pip install django-filter 

docker-compose

version: '3'
services:
  backend:
    container_name: backend
    build:
      context: ./
      dockerfile: ./containers/backend/Dockerfile
    volumes:
      - ./backend/:/srv/backend   
    environment:
      - TZ=Asia/Tokyo
    ports:
      - "8000:8000"
    command: /root/.pyenv/versions/3.8.4/bin/python /srv/backend/manage.py runserver 0:8000
    tty: true