dockerfile+docker-compose + nginx +uwsgi 部署flask后端项目

项目目录摆放示例在这里插入图片描述
1 pip freeze > requirements.txt 自动生成此文件(安装环境依赖包)
2 创建test 下的Dockerfile 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Use the Python3.6 image
# 使用python 3.6作为基础镜像
FROM python:3.6.8
# Set the working directory to /app
# 设置工作目录,作用是启动容器后直接进入的目录名称
ENV TZ=Asia/Shanghai
WORKDIR /app
# Copy the current directory contents into the container at /app
# . 表示和Dockerfile同级的目录
# 该句将当前目录下的文件复制到docker镜像的/app目录中
ADD . /app

# Install the dependencies
# 安装相关依赖
RUN pip install -r requirements.txt -i http://pypi.douban.com/simple –trusted-            host pypi.douban.com \
       && pip3 install uwsgi  -i http://pypi.douban.com/simple --trusted-host           pypi.douban.com    \
        &&  ln -s  /usr/local/python3/bin/uwsgi /usr/bin/uwsgi

# run the command to start uWSGI
# 容器启动后要执行的命令 -> 启动uWSGI服务器
#CMD ["uwsgi", "--ini", "uwsgi.ini"]
ENTRYPOINT  uwsgi  --ini    /app/uwsgi.ini

3 创建uwsgi.ini 文件

1
2
3
4
5
6
7
8
9
10
11
[uwsgi]
master = true
socket = :9876
mdoule = main
wsgi-file = /app/app.py
callable = app
processes = 4
threads = 2
buffer-size = 65536
vacuum = true
pidfile = /app/uwsgi.pid

4 创建nginx default.conf文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
        listen 80;                    # 监听80端口
         charset UTF-8;
          server_name localhost;
          client_max_body_size 30M;
           location / {
            include uwsgi_params;
            uwsgi_pass test:9876;   # test指容器名字,该配置是指将信息转发至test           容器的   9876端口
        }
    }
创建nginx下的Dockerfile文件
    # Use the Nginx image
    # 使用Nginx镜像
    FROM nginx

    # Remove the default default.conf
    # 移除官方的配置文件, 并换为自己的
    RUN rm /etc/nginx/conf.d/default.conf

    # Replace with our own default.conf
    COPY default.conf /etc/nginx/conf.d/default.conf

5 创建docker-compose.yml文件
version: “3.7”
networks:
test:
services:
test:
build: ./test # 指向相关镜像的Dockerfile所在目录
restart: always
container_name: test
networks:
- test
expose: # 将该容器的9876端口开放给同一网络下的其他容器和服务
- 9876

1
2
3
4
5
6
7
8
9
10
    nginx:
      build: ./nginx
      container_name: nginx
      networks:
            - test
      depends_on:
            - test
    restart: always
        ports:                   # HOST:CONTAINER 将主机的83端口映射到容器的80端口,                 相当于将nginx容器的80端口开放给外部网络
          - "83:80"

6 主机 安装docker-compose

1
Sudo  apt-get   install docker-compose

7 安装mysql docker pull mysql:8.0

1
2
3
sudo docker run -p 3306:3306 --name mysqls -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0
Docker exec –it mysql_t /bin/bash
https://www.cnblogs.com/yy-cola/p/11226924.html docker下mysql设置远程  连接

8 https://www.cnblogs.com/denghb/p/12309831.html(参考连接) docker将数据库sql文件运行加载
9 https://blog.csdn.net/u010358168/article/details/97143703(参考链接) 安装redis,设置远程连接
进入创建的redis容器交互模式
cat /etc/hosts 命令查看分配给redis的ip,使用时连接此ip

10 执行命令

1
2
3
sudo docker-compose  down
sudo docker-compose  build
sudo docker-compose  up