【技术栈⭐00067】docker-compose运行dockerfile+gunicorn+flask

祖奇
2023-12-01

docker-compose简单说明

利用docker-compose管理docker容器
操作docker-compose集合了我们操作Dockerfile和Dockerfile里的一下命令,
总结就是,Dockerfile帮我们管理docker镜像,docker-compose帮我们管理docker众多容器。
总之如果你还没用过,试着用起来就知道了,一个很简单的工具。

requirements.txt

alembic==2.5.8
cryptography==3.3.2
aliyun-python-sdk-core==2.13.33
aliyun-python-sdk-kms==2.14.0
aliyunsdkcore==1.0.3
amqp==5.0.6
#amqp
aniso8601==9.0.1
bidict==0.21.2
blinker==1.4
certifi==2020.12.5
cffi==1.14.5
chardet==4.0.0
click==7.1.2
crcmod==1.7
Flask==1.1.2
Flask-Cors==3.0.10
Flask-Migrate==2.7.0
Flask-RESTful==0.3.8
Flask-Script==2.0.6
Flask-SQLAlchemy==2.5.1
Flask-WTF==0.14.3
greenlet==1.0.0
idna==2.10
importlib-metadata==3.10.0
itsdangerous==1.1.0
Jinja2==2.11.3
jmespath==0.10.0
jwt==1.2.0
kombu==5.0.2
Mako==1.1.4
MarkupSafe==1.1.1
mysql-connector-python==8.0.23
mysqlclient
oss2==2.14.0
Pillow==8.2.0
protobuf==3.15.7
pycparser==2.20
pycrypto==2.6.1
pycryptodome==3.10.1
PyMySQL==1.0.2
python-dateutil==2.8.1
python-editor==1.0.4
python-engineio==4.0.1
python-socketio==5.1.0
pytz==2021.1
redis==3.5.3
requests==2.25.1
sentry-sdk==1.0.0
six==1.15.0
SQLAlchemy==1.4.6
typing-extensions==3.7.4.3
urllib3==1.26.4
vine==5.0.0
Werkzeug==1.0.1
WTForms==2.3.3
zipp==3.4.1
celery
gunicorn
gevent
psutil==5.7.0

Dockerfile

# 指定基于的基础镜像
FROM ubuntu:18.04

RUN echo export LANGUAGE=en_US.UTF-8 >> /etc/profile
RUN echo export LANG=en_US.UTF-8>> /etc/profile

RUN echo export LC_ALL=C.UTF-8 >> /etc/profile
RUN echo export LANG=C.UTF-8 >> /etc/profile

RUN echo "export FLASK_APP=\"/usr/src/app/manage.py\"" >> /etc/profile
RUN echo source /etc/profile >> ~/.bashrc
RUN apt-get  update

RUN apt-get  upgrade -y

#RUN apt-get install sudo

#RUN apt-get install vim -y

#RUN apt-get install python3.7 -y && apt-get install python3-pip -y
RUN apt-get install python3-pip -y

#RUN apt-get install python2.7 -y && apt-get install python-pip -y
#RUN apt-get install python-pip -y
# 默认的pip配置需要做一下修改
# 我们到 指定的目录下

WORKDIR /root/.pip/

# 把我们的pip.conf 拷贝到这里
# ADD 源目录     目标目录
# 源目录: 是相当于 Dockerfile所在的文件
# 目标目录: 镜像目录
# COPY ./pip.conf ./
ADD ./pip.conf ./
WORKDIR /usr/src/app

COPY requirements.txt ./
# mysql 的依赖
RUN apt-get install libmysqlclient-dev -y
# 阿里云的依赖需要python3-pip
RUN pip3 install -r requirements.txt
COPY . .
CMD ["gunicorn", "manage:app", "-c", "gunicorn.py"]

gunicorn.py

import multiprocessing

#预加载资源
preload_app = True

# 并行工作进程数
workers = 2

# 指定每个工作者的线程数
threads = 2

# 端口 5000
bind = '127.0.0.1:5000'

# 设置守护进程,将进程交给supervisor管理
daemon = 'false'

# 工作模式协程
worker_class = 'gevent'

# 设置最大并发量
worker_connections = 2000

# 设置进程文件目录
#pidfile = '/var/run/gunicorn.pid'

# 设置访问日志和错误信息日志路径
#accesslog = "/usr/src/app/gunlog/access.log"
#errorlog = "/usr/src/app/gunlog/error.log"
loglevel = "debug"

# 设置日志记录水平
loglevel = 'warning'

docker-compose.yml

version: '3'
services:

  web:
    build: .
    network_mode: "host"
    container_name: "webe2"
    working_dir: /usr/src/app
    volumes:
      - .:/usr/src/app
    command:  gunicorn --preload -c gunicorn.py manage:app
docker-compose up --remove-orphans
 类似资料: