介绍
之前已经分别搭建了flask、react和mariadb,但是如果每次发布都要执行3个命令开启3个容器,那肯定是比较低效的,也没有真正发挥docker的能力。这里介绍docker-compose方法将3个容器同时开启,做到一键部署。
参考:https://blog.csdn.net/pushiqiang/article/details/78682323
代码文件准备
- 建立如下工程目录
projectx
├── flask_app
│ └── main.py
├── react_build
│ └── index.html
└── docker
├── flask_web
│ ├── Dockerfile
│ └── requirements.txt
├── nginx_conf
│ └── nginx.conf
└── docker-compose.yml
- flask_app/main.py
from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
count = redis.incr('hits')
return 'Hello World! I have been seen {} times.\n'.format(count)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)
- requirements.txt
flask
redis
- flask_web/Dockerfile
FROM tiangolo/uwsgi-nginx-flask:python3.7
COPY ./flask_web/requirements.txt /app/
WORKDIR /app
RUN pip install -r requirements.txt
- nginx.conf
# gzip设置
# 开启或关闭gzip模块(on/off)
gzip on;
# 和http header有关系,加个vary,给代理服务器用。有的浏览器支持压缩,有的不支持。为了避免压缩不支持的浏览器,所以根据客户端的HTTP头来判断,是否需要压缩
gzip_vary on;
# 压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)
gzip_comp_level 6;
# 设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。
gzip_buffers 16 8k;
# 设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。
gzip_min_length 1000;
gzip_proxied any;
gzip_disable "msie6";
#gzip_http_version 1.0;
# 匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的。
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
# 其作用是按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。
try_files $uri /index.html;
}
location ~* html {
rewrite .* /index.html break;
root /home/hard/Project/game/web-client/build/html/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
使用docker-compose一键部署
注意每个涉及参数的冒号后面都要有空格。
编写docker-compose.yml
version: '3'
services:
在services:下添加服务
- flask服务:
flask:
# image: myflask # 这里可以直接使用现有镜像
build: ./flask_web/ # 从flask_web中的Dockerfile创建
container_name: flask_app
ports:
- 28080:80
volumes:
- ../flask_app:/app
working_dir: /app
# 运行后执行的命令
command: python main.py
links:
- db:mariadb
redis:
# 如果没有这个镜像的话会自动下载
image: "redis:latest"
container_name: myredis
- nginx服务:
nginx:
image: nginx:latest
ports:
- "28081:80"
volumes:
# 映射nginx.conf到容器的/etc/nginx/conf.d目录并覆盖default.conf
- ./nginx_conf/nginx.conf:/etc/nginx/conf.d/default.conf
# 映射build文件夹到容器的/usr/share/nginx/html文件夹
- ../build:/usr/share/nginx/html
# 覆盖容器启动后默认执行的命令。
command: /bin/bash -c "nginx -g 'daemon off;'"
- mariadb服务:
db:
image: mariadb:latest
ports:
- "28082:3306"
privileged: true
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=project_echo
完成后运行:docker-compose up -d
网友评论