1
1

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 1 year has passed since last update.

DockerでNginx+uWSGI+Flask環境を作る

Posted at

取り急ぎ必要だったため作りました。

環境

OS:Windows 11 Pro
Docker Desktopインストール済み

Flaskアプリ

最低限表示されるようにしておきます

app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
	return "Hello World!"

if __name__ == "__main__":
	app.run(debug=True)

Nginx

web/nginx.conf
server {
	listen 5000;
	server_name  localhost;

	location / {
		include uwsgi_params;
		uwsgi_pass unix:///tmp/uwsgi.sock;
	}
}

uWSGI

uwsgi.ini
[uwsgi]
module = app
callable = app
master = true
processes = 1
socket = /tmp/uwsgi.sock
chmod-socket = 666
vacuum = true
die-on-term = true
wsgi-file = /app.py

Docker

.env
HOME_PATH = './'
ROOT_PATH = '/var/www/html/'
requirements.txt
flask
uwsgi
Dockerfile
FROM python:3.11

RUN mkdir -p /var/www/html/
COPY requirements.txt /var/www/html/
COPY uwsgi.ini /var/www/html/
COPY .env /var/www/html/
COPY const.py /var/www/html/
COPY app.py /var/www/html/
COPY /data/ /var/www/html/
WORKDIR /var/www/html/

RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install -r requirements.txt
docker-compose.yml
version: '3'

services:
  app:
    image: python:3.11
    build: ${HOME_PATH}
    container_name: app
    working_dir: ${ROOT_PATH}
    volumes:
      - ${HOME_PATH}:${ROOT_PATH}
      - socket:/tmp
    command: uwsgi --ini ${ROOT_PATH}uwsgi.ini
    restart: always
    tty: true

  nginx:
    image: nginx:latest
    ports:
      - "5000:5000"
    volumes:
      - ${HOME_PATH}web/nginx.conf:/etc/nginx/conf.d/default.conf
      - socket:/tmp

volumes:
  socket:

構成

こんな感じになります。
ちゃんと分けるべきなんですが取り急ぎ...

app/
 ├── web/
 │    └── nginx.conf
 ├── .env
 ├── app.py
 ├── docker-compose.yml
 ├── Dockerfile
 ├── requirements.txt
 └── uwsgi.ini

実行

docker compose up --build

http://localhost:5000 にアクセス
image.png
表示されました!

一応、何をやっているかというと、NginxとuWSGI間で疎通するようソケットファイル等を設定しています。
Docker起動時、実行用ファイルをNginxの公開ディレクトリ(/var/www/html)にコピーし、コマンド「uwsgi --ini /var/www/html/uwsgi.ini」を実行しています。

参考

参考にさせていただきました。ありがとうございます。

Flask+uWSGI+NginxでWSGIを試してみる
Flask + uWSGI + Nginx でハローワールドするまで @ さくらのVPS (CentOS 6.6)
Flask + uwsgi + NginxでAPIを実行する

1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?