Application development with Docker + Python + Flask

Last time: Dockerize using the project created in Connect to MySQL using Flask SQLAlchemy.

Dockerize

file organization

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

requirements.txt


flask
SQLAlchemy
Flask-SQLAlchemy
PyMySQL

In requirements.txt, write the modules that need to be installed.


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

This time I'm connecting to mysql in another container.

Run

$ docker-compose up

Static file

file organization

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

Create nginx docker file

/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 settings
    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;
        }
    }
}

Accessing / statics will cause nginx to go to the/ var / www /file.

Fixed docker-compose file

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

Run

$ docker-compose up

Recommended Posts

Application development with Docker + Python + Flask
Web application development with Flask
Web application with Python + Flask ② ③
Web application with Python + Flask ④
Run a Python web application with Docker
Launch Flask application with Docker on Heroku
Use python with docker
Programming with Python Flask
[Python] Build a Django development environment with Docker
Prepare python3 environment with Docker
Rails application building with Docker
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
[Mac OS] Use Kivy with PyCharm! [Python application development]
Launch a Python web application with Nginx + Gunicorn with Docker
Build a machine learning application development environment with Python
A memo about building a Django (Python) application with Docker
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
SNS Python basics made with Flask
Creating a Flask server with Docker
Web application development memo in python
Build Mysql + Python environment with docker
Deploy a Django application with Docker
[Python] Use Basic/Digest authentication with Flask
Monitor Python application performance with Dynatrace ♪
I made a simple book application with python + Flask ~ Introduction ~
Othello game development made with Python
Create a simple Python development environment with VSCode & Docker Desktop
Prepare Python development environment with Atom
Rebuild Django's development environment with Docker! !! !! !!
Python application: Data cleansing # 2: Data cleansing with DataFrame
IOS application development with Python (kivy) that even monkeys can do
Create a simple Python development environment with VS Code and Docker
Build Jupyter Lab (Python) environment with Docker
[Development environment] Python with Xcode [With screen transition]
Behind the flyer: Using Docker with Python
POST variously with Python and receive with Flask
Page cache in Python + Flask with Flask-Caching
Connect to MySQL with Python within Docker
Use cryptography library cryptography with Docker Python image
[Python] A quick web application with Bottle!
Application development using SQLite with Django (PTVS)
Easy web app with Python + Flask + Heroku
Use Application Insights with Python 3 (including bottles)
[Python] Quickly create an API with Flask
Launch environment with LineBot + Heroku + Docker + Python
How to install python3 with docker centos
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Flask development introduction
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
Flask application settings
python starts with ()
with syntax (Python)
[GCP] Procedure for creating a web application with Cloud Functions (Python + Flask)