Last time: Dockerize using the project created in Connect to MySQL using Flask SQLAlchemy.
Dockerize
├── 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
├── api
├── app.py
├── config.py
├── requirements.txt
├── Dockerfile
├── docker-compose.yaml
|
├── nginx
| ├── Dockerfile
| └── nginx.conf
└── statics
└── css
└── sample.css
/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.
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