当前位置: 首页 > news >正文

构建Django的Web镜像

构建Django的Web镜像

文章目录

    • 构建Django的Web镜像
      • 1.前提
      • 2.步骤
      • 3.问题与思考

1.前提

  • 已经在环境上安装了Docker

2.步骤

  • 1.拉取最新代码

    git clone https://github.com/lp1506947671/recruitment.git
    
  • 2.指定manage.py中读取的Django配置文件来源为os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.local")

    #!/usr/bin/env python
    """Django's command-line utility for administrative tasks."""
    import os
    import sysdef main():"""Run administrative tasks."""os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings.local")try:from django.core.management import execute_from_command_lineexcept ImportError as exc:raise ImportError("Couldn't import Django. Are you sure it's installed and ""available on your PYTHONPATH environment variable? Did you ""forget to activate a virtual environment?") from excexecute_from_command_line(sys.argv)if __name__ == "__main__":main()
    
  • 3.从拉取的代码中找到Dockerfile文件

    FROM python:3.10-alpine
    MAINTAINER xiaopawnye
    ENV DJANGO_SETTINGS_MODULE=settings.local
    WORKDIR /data/recruitment
    COPY ./requirements.txt .
    COPY ./start.sh .
    COPY ./src .
    RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories # 替换为apk阿里云镜像
    RUN apk add --update --no-cache curl jq py3-configobj py3-pip py3-setuptools python3-dev \&& apk add --no-cache gcc g++ jpeg-dev zlib-dev libc-dev libressl-dev musl-dev libffi-dev \&& python -m pip install --upgrade pip \&& pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple \&& apk del gcc g++ libressl-dev musl-dev libffi-dev python3-dev \&& apk del curl jq py3-configobj py3-pip py3-setuptools \&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone  \&& rm -rf /var/cache/apk/*
    RUN sed -i 's/\r//' /data/recruitment/start.sh
    RUN chmod +x ./start.sh
    EXPOSE 39979
    CMD ["/bin/sh", "/data/recruitment/start.sh"]
    
  • 4.构建镜像,注意下面命令中recruitment/必须是Dockerfile文件的上级父目录

    docker build --network=host -t xiaopawnye/recruitment-base:v1 recruitment/
    
  • 5.镜像构建完成后启动容器,命令如下

    docker run --rm --network=host  -p 39979:39979 -v "$(pwd)":/data/recruitment  --env server_params="--settings=settings.local" xiaopawnye/recruitment-base:v1
    

3.问题与思考

  • 问题1:执行镜像构建命令时报错docker build -t xiaopawnye/recruitment-base:v1

    报错ERROR: unable to select packages:curl (no such package):required by: world[curl]jq (no such package):required by: world[jq]py3-configobj (no such package):required by: world[py3-configobj]py3-pip (no such package):required by: world[py3-pip]py3-setuptools (no such package):required by: world[py3-setuptools]python3 (no such package):required by: world[python3]python3-dev (no such package):required by: world[python3-dev]
    

    解决方法:

    查询google,Dockerfile编译django时执行pip命令报错网络不通,通过将命令修改为docker build --network=host -t xiaopawnye/recruitment-base:v1即可

  • 问题2报错

    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting                                                                                        behaviour with the system package manager. It is recommended to use a virtual environment                                                                                        instead: https://pip.pypa.io/warnings/venv
    Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
    Obtaining recruitment from git+ssh://****@git.zhlh6.cn/lp1506947671/recruitment.git@e94b4                                                                                       93b7646b0b74843cea49cfd14fa66c574b4#egg=recruitment (from -r requirements.txt (line 75))Cloning ssh://****@git.zhlh6.cn/lp1506947671/recruitment.git (to revision e94b493b7646b                                                                                       0b74843cea49cfd14fa66c574b4) to ./src/recruitmentERROR: Error [Errno 2] No such file or directory: 'git' while executing command git ver                                                                                       sion
    ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?
    The command '/bin/sh -c apk add --update --no-cache curl jq py3-configobj py3-pip py3-set                                                                                       uptools  python3-dev   && apk add --no-cache gcc g++ jpeg-dev zlib-dev libc-dev libressl-                                                                                       dev musl-dev libffi-dev   && python -m pip install --upgrade pip   && pip install -r requ                                                                                       irements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple  && apk del gcc g++ libressl-dev                                                                                        musl-dev libffi-dev python3-dev   && apk del curl jq py3-configobj py3-pip py3-setuptool                                                                                       s   && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime   && echo 'Asia/Shanghai'                                                                                        >/etc/timezone    && rm -rf /var/cache/apk/*' returned a non-zero code: 1
    

    解决方法: dockerfiles镜像加速配置:https://blog.csdn.net/gitblog_00337/article/details/153157195

  • 问题3:容器启动成功但是web无法访问,显示违反csrf,解决方法注释如下代码

    SESSION_COOKIE_SECURE = True
    CSRF_COOKIE_SECURE = True
    
http://www.dtcms.com/a/528572.html

相关文章:

  • 历史数据分析——锦江酒店
  • 做网站站怎么赚钱吗企业网站推广的收获与启示
  • 大厂硬件岗位笔试题库-卷11
  • 【操作系统】408操作系统核心考点精讲:第二章——进程的概念、组成与特征​
  • 基于脉冲神经网络的语音识别系统实现:识别“将榴弹从位置幺搬到位置两“命令
  • 破茧成蝶:全方位解析Java学习难点与征服之路
  • 江门网页建站模板aws 搭建wordpress
  • C语言:使用顺序表实现通讯录
  • 手机网站与app免费的网站平台有哪些
  • SQL186 对试卷得分做min-max归一化
  • 哪家开发app好有南昌网站优化公司
  • vue3 npm run dev局域网可以访问,vue启动设置局域网访问,
  • 网站建设续费催款通知书哈尔滨微信网站开发
  • NLP之Embedding:Youtu-Embedding的简介、安装和使用方法、案例应用之详细攻略
  • 做网站需要学哪些语言wordpress sina
  • Redis常见指令
  • 机器学习02——环境安装
  • 网站可以用中国二字做抬头吗WordPress 评论框表情
  • 随笔——记一次常见的浮点数精度问题到Grisu3初识
  • 【git】rebase 和 merge 区别及使用建议
  • 机器学习催化剂设计!
  • Agent Zero:重新定义AI Agent的有机生长框架——从“预设工具“到“自我进化“的范式革命
  • 脚本更新--CosMx、Xenium的邻域通讯分析(R版本)
  • VS Code搭建C/C++开发调试环境-Windows
  • 怎么把自己做的网站发布到网上网站建设专题页面
  • 面向智慧农业的自主移动果蔬采摘机器人:融合视觉识别与自动驾驶的智能化农作系统研究
  • 厦门专业网站设计公司低价网站建设推广优化
  • ClickHouse 介绍
  • 何时在 ClickHouse 中使用 ARRAY JOIN
  • Stream流中.filter和.map的用法区别