在django主目录下创建dockerfile,requirments.txt, pip.conf 文件,内容为:
(1)Dockerfile编辑
# 建立 python3.8 环境
FROM python:3.8
# 镜像作者
MAINTAINER ykq
# 设置语言默认值为中文,时区改为东八区
RUN echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV LANG zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8
# 设置 python 环境变量
ENV PYTHONUNBUFFERED 1
# 设置pip源为国内源
COPY pip.conf /root/.pip/pip.conf
# 在容器内创建mysite文件夹
RUN mkdir -p /var/ykq/test
# 设置容器内工作目录
WORKDIR /var/ykq/test
# 将当前目录文件加入到容器工作目录中(. 表示当前宿主机目录)
ADD . /var/ykq/test
# pip安装依赖
RUN pip install -r requirements.txt
RUN apt-get update -y&&apt-get install vim -y
# zidongqidong
CMD python manage.py runserver 0:9001
(2)requirments.txt文件安装依赖(依据自己项目环境需要增加所需安装包),也可命令生成 requirements.txt文件,在django项目主目录下:
pip list --format=freeze > requirements.txt
requirments.txt内容为:
Django==3.2.15
PyMySQL==1.0.2
django-cors-headers==3.13.0
(3)pip.conf文件建立安装包安装源,内容为:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com