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

Docker上でDjango開発準備

Last updated at Posted at 2019-12-15

目的

Dangoの開発を開始するにあたり、Docker上での開発環境を作成します。
公式のDjango Tutorialの1つ目の完了までを目標とします。

開発環境構築

DjangoのDocker環境を構築します。

Docker file作成

現時点のPythonの最新は3.8ですので、それをベースに作成します。

FROM python:3.8
# Python 標準入出力バッファーを無効(反応速度優先のため。性能は下がる。)
ENV PYTHONUNBUFFERED 1
#
# オススメのパッケージは入れない.最小限とする. --no-install-recommends
# DBを変更する場合は postgresql-client等を追加.
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .

EXPOSE 8000
# 開発完了したら、以下を有効にする.
# CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Python追加パッケージの設定

requirements.txtにPythonのパッケージを記述します。

requirements.txt
django

イメージ作成

イメージ名「django」でイメージをビルドします。

docker build -t django .

bash 起動

開発を行うため、bash起動します。

$ docker run -p 127.0.0.1:8000:8000 -it django bash

Django開発

以下のURLを参考にしてアプリを作成します。
Django ドキュメント

djangoアプリ作成

サンプル作成

確認のためにサンプルを作成します。

$ django-admin startproject sampleapp

サンプル起動

動作確認のため、一旦起動してみます。

$ python manage.py runserver 0:8000

HOST側からアクセスするため、「runserver 8000」ではなく「runserver 0:8000」で起動します。
確認したら、Ctrl+Cで終了します。

チュートリアルで確認

チュートリアルでサンプルアプリを作成します。詳細は以下のURLを参照します。
はじめての Django アプリ作成、その 1

以下のURLにアクセスし、表示されることを確認します。

http://127.0.0.1:8000/polls/

docker環境でdjango開発の準備はできた。

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