03-Dockerfile 创建Django项目镜像

  1. 创建一个目录
    mkdir my_django_catMail

  2. 创建Dockerfile文件
    touch Dockerfile

  3. 创建国内源文件
    touch sources.list

  4. 编写国内源文件
    vim sources.list
    将下面复制到 sources.list 中

deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-updates multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security main restricted
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security universe
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ xenial-security multiverse
  1. 编写Dockerfile文件
    vim Dockerfile

5.1 指定基础镜像
FROM ubuntu:16.04

5.2 复制 django 项目所用的包到容器
ADD requirements.txt /home

5.3 复制国内源 并且 更新源文件
COPY sources.list /etc/atp/sources.list

5.4 安装vim、python、pip3 以及 django 项目依赖
RUN apt-get update && apt-get install vim -y
&& apt-get install python3 -y
&& apt-get install python3-pip -y
&& pip3 install -r requirements.txt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

5.5 复制项目到容器
ADD /Users/onlyone/PycharmProjects/catMail /home

5.6 跳转到项目目录
WORKDIR /home/catMail

5.7 运行项目
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

详细Dockerfile 内容

FROM ubuntu:16.04

# 复制文件到容器
ADD requirements.txt /home

# 跳转到指定目录
WORKDIR /home

# 复制国内更新源
COPY sources.list /etc/apt/sources.list

# 安装vim、python、pip3以及Django项目依赖
RUN apt-get update && apt-get install vim -y \
    && apt-get install python3 -y \
    && apt-get install python3-pip -y \
    && pip3 install -r requirements.txt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

# 复制项目到容器
ADD /catMail /home

# 跳转到项目目录
WORKDIR /home/catMail

# 对外暴露端口
EXPOSE 8000

# 运行项目
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
  1. 创建镜像 镜像名建议纯小写
    docker build -t 镜像名字:版本 上下文路径
    docker build -t django_catmail:1.0 .

  2. 注意
    django 版本 和python 版本的对应关系

posted @ 2020-12-17 18:14  萌新_python  阅读(292)  评论(0)    收藏  举报