初学者がDockerを使ってdjango+gunicorn+Nginxの環境を構築する
Dockerfileを作成
Dockerfile
FROM python:3
RUN apt-get update && apt-get install -y \
nano
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
RUN mkdir -p /var/run/gunicorn
# CMD ["gunicorn", "conf.wsgi", "--bind=unix:/var/run/gunicorn/gunicorn.sock"]
requirements.txtを作成
requirements.txt
Django==2.2.6
gunicorn
docker-compose.ymlを作成
docker-compose.yml
version: '3'
volumes:
gunicorn:
driver: local
services:
web:
build:
context: .
dockerfile: Dockerfile
container_name: django
ports:
- '8000:8000'
volumes:
- './web:/code'
- gunicorn:/var/run/gunicorn
tty: true
stdin_open: true
nginx:
image: nginx
container_name: test_nginx
ports:
- '80:80'
volumes:
- './nginx/html:/usr/share/nginx/html'
- './nginx/conf.d:/etc/nginx/conf.d'
- gunicorn:/var/run/gunicorn
depends_on:
- web
ディレクトリを作る
構成
.
├── Dockerifle
├── requirements.txt
├── docker-compose.yml
├── nginx
│ └── conf.d # gunicornの設定ファイルを格納するため
└── web # djangoプロジェクトを格納するため
gunicornの設定ファイルを作成
構成
.
├── docker-compose.yml
├── Dockerifle
├── requirements.txt
├── nginx
│ └── conf.d
│ └── gunicorn.conf # コレを作成
└── web
FROM python:3
RUN apt-get update && apt-get install -y \
nano
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
RUN mkdir -p /var/run/gunicorn
# CMD ["gunicorn", "conf.wsgi", "--bind=unix:/var/run/gunicorn/gunicorn.sock"]
Django==2.2.6
gunicorn
docker-compose.ymlを作成
docker-compose.yml
version: '3'
volumes:
gunicorn:
driver: local
services:
web:
build:
context: .
dockerfile: Dockerfile
container_name: django
ports:
- '8000:8000'
volumes:
- './web:/code'
- gunicorn:/var/run/gunicorn
tty: true
stdin_open: true
nginx:
image: nginx
container_name: test_nginx
ports:
- '80:80'
volumes:
- './nginx/html:/usr/share/nginx/html'
- './nginx/conf.d:/etc/nginx/conf.d'
- gunicorn:/var/run/gunicorn
depends_on:
- web
ディレクトリを作る
構成
.
├── Dockerifle
├── requirements.txt
├── docker-compose.yml
├── nginx
│ └── conf.d # gunicornの設定ファイルを格納するため
└── web # djangoプロジェクトを格納するため
gunicornの設定ファイルを作成
構成
.
├── docker-compose.yml
├── Dockerifle
├── requirements.txt
├── nginx
│ └── conf.d
│ └── gunicorn.conf # コレを作成
└── web
version: '3'
volumes:
gunicorn:
driver: local
services:
web:
build:
context: .
dockerfile: Dockerfile
container_name: django
ports:
- '8000:8000'
volumes:
- './web:/code'
- gunicorn:/var/run/gunicorn
tty: true
stdin_open: true
nginx:
image: nginx
container_name: test_nginx
ports:
- '80:80'
volumes:
- './nginx/html:/usr/share/nginx/html'
- './nginx/conf.d:/etc/nginx/conf.d'
- gunicorn:/var/run/gunicorn
depends_on:
- web
.
├── Dockerifle
├── requirements.txt
├── docker-compose.yml
├── nginx
│ └── conf.d # gunicornの設定ファイルを格納するため
└── web # djangoプロジェクトを格納するため
gunicornの設定ファイルを作成
構成
.
├── docker-compose.yml
├── Dockerifle
├── requirements.txt
├── nginx
│ └── conf.d
│ └── gunicorn.conf # コレを作成
└── web
.
├── docker-compose.yml
├── Dockerifle
├── requirements.txt
├── nginx
│ └── conf.d
│ └── gunicorn.conf # コレを作成
└── web
gunicorn.confの中身はこのような感じです。
upstream gunicorn-django {
server unix:///var/run/gunicorn/gunicorn.sock;
}
server {
listen 80;
server_name localhost;
location / {
try_files $uri @gunicorn;
}
location @gunicorn {
proxy_pass http://gunicorn-django;
}
}
docker-composeでイメージをビルドし、コンテナを起動する
$ docker-compose up -d
$ docker-compose up -d
$ docker-compose ps でコンテナが二つともUpになっていることを確認します。
djangoプロジェクトを作成
まずはdjangoコンテナに入ります。
$ docker-compose exec web bash
djangoプロジェクトを作成します。
$ django-admin startproject conf .
バインドマウントをしているので、ホストのwebフォルダの配下にもconfフォルダやmanage.pyが作成されていると思います。今回、アプリケーションの作成は割愛しますが、実際のアプリ開発はここで行うと便利かもしれません。
settings.pyを変更
先ほど作成したdjangoプロジェクト内にあるsettings.pyのALLOWED_HOSTSを変更します。とりあえずどんなサーバーアドレスからのアクセスも受理することにしますが、必要に応じて変更した方が良いような気がします。。。
ALLOWED_HOSTS = ['*']
exitでコンテナから抜けます。
ここからは試行錯誤…
ここからは試行錯誤の結果なので、非効率なやり方になっていると思います。何卒、ご容赦ください。良いやり方があったら教えて頂けると嬉しい…。
|ω・`)チラ
一旦、コンテナをダウンさせます。
$ docker-compose down
次にDockerfileに追記します。
# 末尾に追加
CMD ["gunicorn", "conf.wsgi", "--bind=unix:/var/run/gunicorn/gunicorn.sock"]
再びDockerをupさせます。
$ docker-compose up --build -d
$ docker-compose psでコンテナがUpになっていることを確認したら、ブラウザからlocalhost:80にアクセスし、ロケットが飛び立っていることを確認します。
うーん…ひとまず動きましたが、うまくいっている確証が持てていません…。
間違っているところがありましたら申し訳ありません!
Author And Source
この問題について(初学者がDockerを使ってdjango+gunicorn+Nginxの環境を構築する), 我々は、より多くの情報をここで見つけました https://qiita.com/grv2688/items/69871aac54722327c59e著者帰属:元の著者の情報は、元の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 .