作業ディレクトリを作る
普段使っている作業ディレクトリ置くところの
~/code/
に
~/code/rest4/
として作成
Dockerfile を作成
Dockerfile というテキストファイルを作って
中身に
FROM python:3 ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1 WORKDIR /code COPY requirements.txt /code/ RUN pip install -r requirements.txt COPY . /code/
このファイルを書く。中身を解説していく。
言語
FROM python:3
Python3 を使用
環境変数 -- PYTHONDONTWRITEBYTECODE, PYTHONUNBUFFERED
ENV PYTHONDONTWRITEBYTECODE=1
https://devlights.hatenablog.com/entry/2018/02/23/124719
Python don't write byte code
邪魔な __pycache__
を作成しないようにする
ENV PYTHONUNBUFFERED=1
https://docs.python.org/3.5/using/cmdline.html#envvar-PYTHONUNBUFFERED
0 にセットされてると -u と同じ、この説明は意味不明
https://qiita.com/amazipangu/items/bce228f506f894cd825d#%E5%88%9D%E6%9C%9F%E8%A8%AD%E5%AE%9A
Docker 環境作成中の無駄な標準入出力を無効化すると解釈。
rest4/ 内部の作業ディレクトリを作成
WORKDIR /code COPY requirements.txt /code/ RUN pip install -r requirements.txt COPY . /code/
rest4/code/ のディレクトリを作って使用ディレクトリに設定
rest4/requirements.txt を
rest4/code/requirements.txt にコピー
使用ディレクトリの rest4/code/requirements.txt から
強制的に pip install で中身のライブラリをオンラインでダウンロード
作業ディレクトリの内容を /code/ から一つ前にコピー
===================
requirements.txt を作成
Django>=3.0,<4.0 psycopg2-binary>=2.8 djangorestframework>=3.11.0,<3.12.0
各ライブラリを指定
===================
docker-compose.yml を作成
version: "3.9" services: db: image: postgres:12 volumes: - ./data/db:/var/lib/postgresql/data environment: - POSTGRES_DB=postgres - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres web: build: . command: python manage.py runserver 0.0.0.0:8001 volumes: - .:/code ports: - "8002:8001" depends_on: - db
port 6000 で起動
=================
docker-compose run で web 部分の django-admin startproject を使って rest4 という名前の Django プロジェクトを . の現在地に作る
sudo docker-compose run web django-admin startproject rest4 . Step 7/7 : COPY . /code/ ---> 57d82f068b23 Successfully built 57d82f068b23 Successfully tagged rest4_web:latest Creating rest4_db_1 ... done Creating rest4_web_run ... done
プロジェクトが無事に作成された
data/db/pg_/
rest4/.py
manage.py
などが code/rest4/ の配下に作成されているのを確認できる。
==================
rest4/rest4/setting.py の DATABASES に postgres を登録
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': 'db', 'PORT': 5432, } }
==============
docker-compose up で 起動
docker-compose up web_1 | January 10, 2022 - 06:06:44 web_1 | Django version 3.2.11, using settings 'rest4.settings' web_1 | Starting development server at http://0.0.0.0:8001/ web_1 | Quit the server with CONTROL-C.
これで 仮想環境側の 8001 に 8002 からアクセスできた。
Top comments (0)