Dernière fois: Dockerize à l'aide du projet créé dans Se connecter à MySQL à l'aide de Flask SQL Alchemy.
Dockerize
├── api
├── app.py
├── config.py
|
├── requirements.txt
├── Dockerfile
└── docker-compose.yaml
requirements.txt
flask
SQLAlchemy
Flask-SQLAlchemy
PyMySQL
Dans requirements.txt, écrivez les modules qui doivent être installés.
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
Cette fois, je me connecte à mysql dans un autre conteneur.
Courir
$ 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;
}
#Paramètres du serveur
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;
}
}
}
L'accès à / statics
amènera nginx à accéder au fichier / var / www /
.
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
Courir
$ docker-compose up
Recommended Posts