LoginSignup
0
1

More than 1 year has passed since last update.

初学者がDockerを使ってDjango+gunicorn+nginx+PostgreSQLの環境を構築する

Last updated at Posted at 2021-03-05

Dockerfileを作成

Dockerfile
FROM python:3.7

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
psycopg2
gunicorn

docker-compose.ymlを作成

※一部修正しました・・・。

docker-compose.yml
version: '3'

volumes:
  gunicorn:
    driver: local
  dbdata:

services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: django
    ports:
      - '8000:8000'
    volumes:
      - './web:/code'
      #- './web/static:/static' # 不要だったかもしれません。。。
      - gunicorn:/var/run/gunicorn
    tty: true
    stdin_open: true

  nginx:
    image: nginx
    container_name: nginx
    ports:
      - '80:80'
    volumes:
      - './nginx/conf:/etc/nginx/conf.d'
      - './web/static:/static'
      - gunicorn:/var/run/gunicorn
    depends_on:
      - web

  db:
    image: postgres
    container_name: db
    ports:
      - "2345:5432"
    volumes:
      - "dbdata:/var/lib/postgresql/data"
    environment:
        - 'POSTGRES_DB=postgres'
        - 'POSTGRES_USER=postgres'
        - 'POSTGRES_PASSWORD=***'
        - 'POSTGRES_HOST_AUTH_METHOD=trust'

ディレクトリを作る

構成
.
├── Dockerifle
├── requirements.txt
├── docker-compose.yml
├── nginx
│   └── conf.d    # gunicornの設定ファイルを格納するため
└── web           # djangoプロジェクトを格納するため

gunicornの設定ファイルを作成

構成
.
├── docker-compose.yml
├── Dockerifle
├── requirements.txt
├── nginx
│   └── conf
│       └── gunicorn.conf    # コレを作成
└── web       

gunicorn.confの中身はこのような感じです。

gunicorn.conf
upstream gunicorn-django {
    server unix:///var/run/gunicorn/gunicorn.sock;
}

server {
    listen 80;
    server_name localhost;

    location /static {
        alias /static/;
    }

    location / {
        try_files $uri @gunicorn;
    }

    location @gunicorn {
        proxy_pass http://gunicorn-django;
    }
}

docker-composeでイメージをビルドし、コンテナを起動する

$ docker-compose up -d

$ docker-compose ps でコンテナが全てUpになっていることを確認します。

djangoプロジェクトを作成

まずはdjangoコンテナに入ります。

$ docker-compose exec web bash

djangoプロジェクトを作成します。

$ django-admin startproject conf .

settings.pyを変更

先ほど作成したdjangoプロジェクト内にあるsettings.pyのALLOWED_HOSTSを変更します。とりあえずどんなサーバーアドレスからのアクセスも受理することにしますが、必要に応じて変更した方が良いような気がします。。。

settings.py
ALLOWED_HOSTS = ['*']

exitでコンテナから抜けます。 そして、一旦、コンテナをダウンさせます。stopでも良いかもしれません。

$ docker-compose down

次にDockerfileに追記します。

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にアクセスし、ロケットが飛び立っていることを確認します。
スクリーンショット 2021-02-23 11.43.02.png

0
1
0

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
1