前言
参考详细地址:https://juejin.cn/post/6970663530215407652,感谢作者,作者主页直达
正文
flask的dockerfile(python版本3.8.6)
注意:在docker里面运行python需要指定host和port:app.run(host=‘0.0.0.0’, port=5000,debug=True)
requirements.txt 依赖项内容:
Flask
gunicorn
项目目录如下:
docker-compose.yml文件
version: '3'
services:
flask:
build:
context: ./app
container_name: flask
ports:
- "5001:5000" # Flask's internal port mapped to host port 5001
volumes:
- D:\python\app:/home/app
environment:
PYTHONUNBUFFERED: 'true'
DATABASE_URL: mysql://python:python_123456@mysql:3306/python_db
REDIS_URL: redis://redis:6379/0
networks:
- app_network
nginx:
build:
context: ./nginx
container_name: nginx
ports:
- "5000:5000" # Nginx's internal port 5000 mapped to host port 5000
depends_on:
- flask
volumes:
- ./nginx/conf:/etc/nginx/conf.d
networks:
- app_network
#容器mysql使用工具可以连接但是在flask里面连不上,不管是flask_mysqldb还是pymysql都不行,可能是端口映射的问题,先使用本地mysql,后期找下原因
# mysql:
# build:
# context: ./mysql
# container_name: mysql
# ports:
# - "3307:3306" # MySQL's internal port 3306 mapped to host port 3307
# volumes:
# - ./mysql/data:/var/lib/mysql
# - ./mysql/conf:/etc/mysql/conf.d
# environment:
# MYSQL_ROOT_PASSWORD: 123456
# MYSQL_DATABASE: python_db
# MYSQL_USER: python
# MYSQL_PASSWORD: python_123456
# networks:
# - app_network
redis:
build:
context: ./redis
container_name: redis
ports:
- "6380:6379" # Redis's internal port 6379 mapped to host port 6380
supervisord:
build:
context: ./supervisord
container_name: supervisord
ports:
- "9001:9001" # Supervisord's web interface port
volumes:
- ./supervisord/conf:/etc/supervisor/conf.d
depends_on:
- flask
- nginx
networks:
- app_network
networks:
app_network:
driver: bridge
上面的mysql一直连不上,当时因为一些原因没时间处理使用了本地的mysql,有知道的大佬可以告知一下原因
下面是项目具体目录,在app文件夹里面使用了简单的蓝图:
views当时是过于随意命名了,可以改成其他的,里面是mvc里面的c部分功能
有兴趣的大佬可以查看一下资源