docker手动部署django项目Dockerfile编排-后端发布

1、首先创建一个桥接网络

docker network create auto

2、部署redis,提供celery的消息队列服务

bash 复制代码
docker run --name redis --restart=always -d --network auto -v redis:/data redis:alpine

3、部署数据库

注意数据库账号密码

bash 复制代码
docker run --name mariadb --restart=always -d --network auto -v mariadb:/var/lib/mysql -e MARIADB_ROOT_PASSWORD=123456789 -e TZ=Asia/Shanghai -p 3309:3306 -e MARIADB_DATABASE=apiauto mariadb:latest

查看网络的服务

bash 复制代码
docker inspect auto

3.1(可以忽略,手动部署,查看问题)

bash 复制代码
docker run --name app -it --network auto -v /diyauto/djangoapp:/app -p 9191:8000 python:3.8-alpine /bin/sh

3.2安装

进入app目录下

bash 复制代码
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt

提示更新

bash 复制代码
python3 -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple/ 

再次安装pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt

提示报错

图片这个alpine内核装mysqlclient比较困难

3.3解决办法:

sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

apk update

apk upgrade

apk add tzdata mariadb-dev gcc libc-dev

python3 -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple/

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r reqirements.txt

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r reqiremensts.txt

再次下载第一个命令行。即可成了

3.4准备文件gunicorn.conf.py,放在根目录下

python 复制代码
bind = '0.0.0.0:8000'                               # 绑定地址和端口
reload = False
pidfile = '/app/logs/gunicorn.pid'                  # 进程id文件
accesslog = '/app/logs/gunicorn_acess.log'          # 通过日志
errorlog = '/app/logs/gunicorn_error.log'           # 启动,错误日志

执行文件命令

gunicron -c gunicorn.conf.py apiauto.wsgi

这是走到了开发环境,有错。

解决办法:查看环境变量,

export ENV=production
gunicorn -c gunicorn.conf.py apiauto.wsgi

手动启动容器,只对当前的进程5有用,1234窗口需要重新配置环境变量ENV=pro

3.5,迁移数据库(代码写数据库ip地址)。写mariadb报错。

migrate 创建超管。

3.6执行supervisord.conf,配置celery和wsgi执行

[unix_http_server]

file=/tmp/supervisor.sock ; the path to the socket file

[supervisord]

logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log

logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB

logfile_backups=10 ; # of main logfile backups; 0 means none, default 10

loglevel=info ; log level; default info; others: debug,warn,trace

pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid

nodaemon=true ; start in foreground if true; default false

silent=false ; no logs to stdout if true; default false

minfds=1024 ; min. avail startup file descriptors; default 1024

minprocs=200 ; min. avail process descriptors;default 200

[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket

[program:gunicorn]

command=gunicorn -c gunicorn.conf.py djangoapp.wsgi

[program:celery_worker]

command=celery -A djangoapp worker -l info

[program:celery_beat]

command=celery -A djangoapp beat -l info

bash 复制代码
supervisord -c supervisord.conf

再次重新执行,

也可以进行ps 查看进程

4、编写DockerDfile文件

bash 复制代码
FROM python:3.8-alpine
# 给镜像打上一些标签,信息
LABEL maintainer='xiaoyong'
LABEL description='Django project'
WORKDIR /app
# 拷贝代码到镜像中
# COPY使用相对路径
# 第一个路径是相对的是宿主机dockerfile所在的目录
# 第二个路径相对的是构建命令所在的目录
COPY . .
# 安装必要的库 shell命令
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \
    apk update && \
    apk upgrade && \
    apk add --no-cache tzdata mariadb-dev gcc libc-dev && \
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip && \
    pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple -r reqiremensts.txt &&\
    chmod 777 ./entrypoint.sh
VOLUME /app/logs
# 暴露监听端口,注意它不会自动映射,只是标注
EXPOSE 8000
# CMD 容器启动的时候要执行的命令
# CMD supervisord -c supervisord.conf
# 执行入口脚本
# 把容器要执行的复杂命令写到一个shell脚本中
ENTRYPOINT ["./entrypoint.sh"]

构建镜像,在DockerDfile所在目录

(就是把3的步骤写在一个文件内进行一步一步执行)

bash 复制代码
docker build -t auto_app .

docker images 查看镜像auto_app

5、通过镜像启动容器

bash 复制代码
docker run --name app --network auto -p 9292:8000 -e TZ=Asia/Shanghai -e ENV="production" -d --restart=always -v auto_logs:/app/logs auto_app:latest
相关推荐
FreakStudio28 分钟前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
拾光师1 小时前
spring获取当前request
java·后端·spring
redcocal2 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali2 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ2 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.3 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Java小白笔记3 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
Trouvaille ~3 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算
爆更小小刘3 小时前
Python基础语法(3)下
开发语言·python
哪 吒3 小时前
华为OD机试 - 第 K 个字母在原来字符串的索引(Python/JS/C/C++ 2024 E卷 100分)
javascript·python·华为od