Docker 部署django项目(前后端分离,这是后端)
1,将django项目从本地上传到github/gitee,修改其配置:
fdfs的配置文件
创建prod.py文件:在manage.py同级目录下:
# 生产时环境
import os
pre_path = os.path.dirname(os.path.realpath(__file__))
os.system('python3 {}/manage.py runserver 0.0.0.0:8000'.format(pre_path))
然后使用git将代码上传
git add .
git commit -m 'ok'
git push origin master
2, 在服务器的命令行新建一个文件夹,将包下载在该文件夹中,然后在文件夹下新建Dockerfile文件和requirements.txt文件:
#基于的基础镜像
FROM python:3.8
#代码添加到code文件夹
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
# 安装支持
#RUN pip3 install -i https://pypi.doubanio.com/simple/ -r requirements.txt
ADD . /code/
Run pip3 install -i https://pypi.doubanio.com/simple/ -r requirements.txt
CMD ["python3", "/code/Blog_Server/prod.py"]
requirements.txt:
或者
pip freeze > requirements.txt
也是可以的,不过这个指令要看在哪个文件夹下进行的
altgraph==0.17
amqp==1.4.9
anyjson==0.3.3
argon2-cffi==20.1.0
asgiref==3.3.4
async-generator==1.10
attrs==19.3.0
Automat==20.2.0
backcall==0.2.0
beautiful==0.0.2
billiard==3.3.0.23
bleach==3.3.0
celery==3.1.25
certifi==2019.11.28
cffi==1.14.0
chardet==3.0.4
click==7.1.2
click-didyoumean==0.0.3
click-plugins==1.1.1
click-repl==0.2.0
colorama==0.4.4
constantly==15.1.0
cryptography==2.8
cssselect==1.1.0
cycler==0.10.0
decorator==4.4.2
defusedxml==0.7.1
Django==3.2.4
django-cors-headers==3.7.0
django-email-users==1.1.0
django-ranged-response==0.2.0
django-redis==5.0.0
django-simple-captcha==0.5.13
djangorestframework==3.12.2
docutils==0.17.1
easygui==0.98.1
entrypoints==0.3
environment==1.0.0
et-xmlfile==1.1.0
future==0.18.2
html5lib==1.0.1
hyperlink==19.0.0
idna==2.9
importlib-metadata==4.6.0
incremental==17.5.0
ipykernel==5.5.0
ipython==7.21.0
ipython-genutils==0.2.0
ipywidgets==7.6.3
itsdangerous==1.1.0
jieba==0.42.1
keyring==23.0.1
kiwisolver==1.3.1
kombu==3.0.37
matplot==0.1.9
matplotlib==3.4.2
mutagen==1.40.0
numpy==1.21.0
packaging==20.9
Pillow==8.2.0
pkginfo==1.7.0
prompt-toolkit==3.0.19
py3Fdfs
Pygments==2.9.0
pyloco==0.0.139
PyMySQL==1.0.2
pyparsing==2.4.7
python-dateutil==2.8.1
pytz==2021.1
readme-renderer==29.0
redis==3.5.3
requests==2.25.1
requests-toolbelt==0.9.1
rfc3986==1.5.0
SimpleWebSocketServer==0.1.1
six==1.16.0
sqlparse==0.4.1
tqdm==4.61.1
twine==3.4.1
typing==3.7.4.3
urllib3==1.26.6
ushlex==0.99.1
vine==5.0.0
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==1.1.0
xlwt==1.3.0
zipp==3.4.1
注意,如果要用到Fdfs的话,要下载py3Fdfs,不能下载fastDfs,不然你就等着怀疑人生吧
安装:
pip install -r requirements.txt
3,创建镜像django:
docker build -t django .
此时pip会下载很多包
4,运行:
docker run -it -p 8000:8000 --name django django
这样就算完整了,一定要注意细节,不然就是两三天的时间调配置调掉了。
或者不使用docker容器,由于有的配置的问题,可以直接运行prod.py
nohup python3 prod.py >null 2>&1 &
查看所有python3进程:
ps -aux|grep python3
kill -9 pid