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 3 years have passed since last update.

Docker + Python + Flaskでアプリケーション開発

Last updated at Posted at 2020-03-03

前回:Flask SQLAlchemyを使ってMySQLに接続で作成したプロジェクトを使ってDockerizeします。

Dockerize

ファイル構成

├── api
├── app.py
├── config.py
|
├── requirements.txt
├── Dockerfile
└── docker-compose.yaml
requirements.txt
flask
SQLAlchemy
Flask-SQLAlchemy
PyMySQL

requirements.txtにはインストールが必要なモジュールを書き込んでいきます。


FROM python:3.8-alpine

ADD requirements.txt /var/www/html/tool/
WORKDIR /var/www/html/tool
RUN pip3 install -r requirements.txt
docker-compose.yaml
version: '3'
services:
  api:
    build: .
    container_name: tool-api
    volumes:
      - ~/path/tool-api:/var/www/html/api
    ports:
      - 5000:5000
    tty: true
    command: flask run --host 0.0.0.0 --port 5000
    external_links:
      - "db:db"

networks:
  default:
    external:
      name: db_default

今回は他のコンテナにあるmysqlに接続しています。

実行

$ docker-compose up

静的ファイル

ファイル構成

├── api
├── app.py
├── config.py
├── requirements.txt
├── Dockerfile
├── docker-compose.yaml
|
├── nginx
|   ├── Dockerfile
|   └── nginx.conf
└── statics
    └── css
        └── sample.css

nginxのdockerfileを作成

/nginx/Dockerfile
FROM nginx
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
nginx.conf
user  nginx;
worker_processes  auto;

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;

    keepalive_timeout  75;

    upstream flask_server {
        server tool-api:5000 fail_timeout=0;
    }

    # サーバの設定
    server {
        location /statics/ {
            alias /var/www/;
        }
        
        location / {
            try_files $uri @proxy_to_api;
        }
        
        location @proxy_to_api {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_redirect off;
            proxy_pass http://flask_server;
        }
    }
}

/staticsにアクセスするとnginxが/var/www/ファイルを見に行くようになります。

docker-composeファイルを修正

docker-compose.yaml
version: '3'
services:
  nginx:
    build: ./nginx
    container_name: tool-nginx
    volumes:
      - ./statics/:/var/www/
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    ports:
      - 80:80
    depends_on: 
      - tool-api
  api:
    build: .
    container_name: tool-api
    volumes:
      - ~/path/tool-api:/var/www/html/api
    ports:
      - 5000:5000
    tty: true
    command: flask run --host 0.0.0.0 --port 5000
    external_links:
      - "db:db"

networks:
  default:
    external:
      name: db_default

実行

$ docker-compose up
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?