Docker に Django/PostgreSQL の環境を構築
6815 ワード
環境構築はこちらのページとほぼ同様です。
Quickstart: Compose and Django
mkdir proj01
cd proj01/
次の3つのファイルが必要です。
Dockerfile
requirements.txt
docker-compose.yml
フォルダー構造
$ tree proj01
proj01
├── docker-compose.yml
├── Dockerfile
└── requirements.txt
プロジェクトの作成
docker-compose run web django-admin startproject proj01 .
実行後のフォルダー構造
$ tree proj01
proj01
├── data
│ └── db [error opening dir]
├── docker-compose.yml
├── Dockerfile
├── manage.py
├── proj01
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── requirements.txt
この時点でサーバーの起動
docker-compose up
Web サーバーと DB サーバーが動いていることを確認
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
066256c49c1a proj01_web "python manage.py ru…" 12 minutes ago Up 12 minutes 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp proj01_web_1
078426ac5bea postgres "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 5432/tcp proj01_db_1
ブラウザーで http://localhost:8000/ にアクセス
ファイル属性の変更
sudo chown -R uchida:uchida .
Web サーバーにアクセス
$ docker exec -it proj01_web_1 bash
root@066256c49c1a:/code#
DB サーバーにアクセス
$ docker exec -it proj01_db_1 bash
root@078426ac5bea:/#
Django のプログラムを改造してみます。
こちらと同様の変更を行います。
Django で Hello World
python manage.py migrate
python manage.py startapp home
フォルダー構造
$ tree -L 1 proj01
proj01
├── data
├── db.sqlite3
├── docker-compose.yml
├── Dockerfile
├── home
├── manage.py
├── proj01
└── requirements.txt
次を作成または編集します。
home/views.py
home/urls.py
proj01/settings.py
proj01/urls.py
home/view.py
from django.http import HttpResponse
def index(request):
str_out = "<p>Good Afternoon</p>"
str_out += "<p>こんにちは</p>"
str_out += "<blockquote>"
str_out += "<p>Jun/25/2021</p>"
str_out += "</blockquote>"
return HttpResponse(str_out)
ブラウザーで http://localhost:8000/ にアクセス
PostgreSQL を使ってみます。
ユーザーの作成
データベースの作成
psql を起動してデータベースの選択
$ docker exec -it proj01_db_1 bash
root@4c30256e6c8e:/# su postgres
postgres@4c30256e6c8e:/$ createuser scott -P
Enter password for new role:
Enter it again:
postgres@4c30256e6c8e:/$ createdb city
postgres@4c30256e6c8e:/$ psql
psql (13.3 (Debian 13.3-1.pgdg100+1))
Type "help" for help.
postgres=# \c city;
You are now connected to database "city" as user "postgres".
city=#
テーブルの作成
データの入れ込み
データの表示
city=# create table cities (id varchar(10) primary key, name text, population int, date_mod date);
CREATE TABLE
city=# insert into cities values ('t3461','広島',72814,'2001-6-14');
INSERT 0 1
city=# insert into cities values ('t3462','福山',81738,'2001-7-11');
INSERT 0 1
city=# insert into cities values ('t3463','東広島',93513,'2001-6-12');
INSERT 0 1
city=# select * from cities;
id | name | population | date_mod
-------+--------+------------+------------
t3461 | 広島 | 72814 | 2001-06-14
t3462 | 福山 | 81738 | 2001-07-11
t3463 | 東広島 | 93513 | 2001-06-12
(3 rows)
city=#
psql の終了
city=# exit
postgres@4c30256e6c8e:/$
参考
ユーザーにテーブルへのアクセス権限をつける方法
city=# grant all on cities to scott;
GRANT
Author And Source
この問題について(Docker に Django/PostgreSQL の環境を構築), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/bb4ac1575a7743a18401著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .