dockerで以下のコンテナを実行しています。
- nginx
- uwsgi + flask
nginx から uwsgi への通信が TCP sockets を使うと実現できました。
これを unix sockets で実現することができないかと考え、nginx.conf と uwsgi.ini を以下のように変更してみましたが接続できませんでした。
nginx.conf
upstream uwsgi { # server uwsgi:3031; server unix:///tmp/uwsgi.sock; }
uwsgi.ini
[uwsgi] wsgi-file = main.py callable = app master = true processes = 1 # socket = :3031 socket = /tmp/uwsgi.sock chmod-socket = 666 vacuum = true die-on-term = true py-autoreload = 1
nginx コンテナ内で uwsgi コンテナで作成する /tmp/uwsgi.sock が参照できるか確認してみたところ見えませんでした。
nginx コンテナから /tmp/uwsgi.sock を参照するために、 uwsgi コンテナのファイルを共有する必要があると考えていますが、この観点は正しいでしょうか。
また、単一のシステム内で使用するための unix sokets をコンテナ間で共有することはできるのでしょうか。
環境情報を以下に記載します。
環境
software | version |
---|---|
Ubuntu | 18.04 |
Docker | 18.09.0 |
docker-compose | 1.23.2 |
ディレクトリ構成
┣ myServer ┣ app ┣ Dockerfile ┣ main.py ┣ requirements.txt ┣ uwsgi.ini ┣ docker-compose.yaml ┣ nginx ┣ Dockerfile ┣ nginx.conf
myServer
docker-compose.yaml
version: "3" services: uwsgi: build: ./app volumes: - ./app:/var/www/ ports: - "3031:3031" environment: TZ: "Asia/Tokyo" nginx: build: ./nginx volumes: - ./nginx/nginx.conf:/etc/nginx/nginx.conf links: - uwsgi ports: - "4231:80" environment: TZ: "Asia/Tokyo"
app
Dockerfile
FROM python:3.6 RUN mkdir /var/www WORKDIR /var/www COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt CMD ["uwsgi","--ini","/var/www/uwsgi.ini"]
main.py
from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run()
requirements.txt
Flask uwsgi
uwsgi.ini
[uwsgi] wsgi-file = main.py callable = app master = true processes = 1 socket = :3031 chmod-socket = 666 vacuum = true die-on-term = true py-autoreload = 1
nginx
Dockerfile
FROM nginx CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
nginx.conf
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; upstream uwsgi { server uwsgi:3031; } server { listen 80; charset utf-8; location / { include uwsgi_params; uwsgi_pass uwsgi; } } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。