Letztes Mal: Dockerisieren Sie mit dem Projekt, das in Mit Flask SQL Alchemy mit MySQL verbinden erstellt wurde.
Dockerize
├── api
├── app.py
├── config.py
|
├── requirements.txt
├── Dockerfile
└── docker-compose.yaml
requirements.txt
flask
SQLAlchemy
Flask-SQLAlchemy
PyMySQL
Schreiben Sie in die Datei require.txt die Module, die installiert werden müssen.
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
Dieses Mal verbinde ich mich mit MySQL in einem anderen Container.
Lauf
$ 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 Einstellungen
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;
}
}
}
Wenn Sie auf / statics
zugreifen, wechselt nginx in die Datei / 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
Lauf
$ docker-compose up
Recommended Posts