2.实例(ubuntu搭建python3.5-django生产环境)

1.目标:搭建智能问答项目(python、django)生产环境

2.安装docker

  • 安装:wget -qO- https://get.docker.com/ | sh
  • 启动后台服务:service docker start
  • 测试运行hello-world:docker run hello-world

3.配置阿里云容器镜像

  • 申请账号

    https://cr.console.aliyun.com/?spm=5176.1971733.0.2.6c045aaaDAr0x9#/imageSearch
  • 镜像加速

    vim /etc/systemd/system/multi-user.target.wants/docker.service

    ExecStart=/usr/bin/dockerd –registry-mirror=https://eqrfvvq2.mirror.aliyuncs.com
  • 创建命名空间(pings)
  • 创建镜像(centos7-base,centos7-aiqa-pro-env)
  • 登录

    docker login –username=username registry.cn-hangzhou.aliyuncs.com
  • 下载镜像

    docker pull registry.cn-hangzhou.aliyuncs.com/pings/centos7-base
  • 上传镜像

    • docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/pings/centos7-base
    • docker push registry.cn-hangzhou.aliyuncs.com/pings/centos7-base

4.生成镜像

4.1.centos7基础镜像
# 系统镜像CentOS7
FROM daocloud.io/centos:latest

# 维护者
MAINTAINER Pings 275598139@qq.com

# 环境变量
ENV LANG en_US.UTF-8
# 设置时区
RUN ln -s -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
4.2.python环境(centos7-python3.5.4)
# 系统镜像centos7-base
FROM pings/centos7-base

# 维护者
MAINTAINER Pings 275598139@qq.com

# 安装依赖
RUN yum install -y wget net-tools  gcc openssl-devel make

# 安装python3.5.4
WORKDIR /opt/python
RUN wget http://mirrors.sohu.com/python/3.5.4/Python-3.5.4.tgz
RUN tar zxvf Python-3.5.4.tgz
WORKDIR /opt/python/Python-3.5.4
RUN ./configure --prefix=/opt/python/Python-3.5.4 && make && make install
ENV PYTHON_HOME=/opt/python/Python-3.5.4
ENV PATH=$PATH:$PYTHON_HOME/bin
RUN rm -f /opt/python/Python-3.5.4.tgz
4.3.依赖的python运行环境
  • 导出依赖的环境:pip freeze > requirement.txt
# 系统镜像centos7-python3.5
FROM pings/centos7-python3.5

# 维护者
MAINTAINER Pings 275598139@qq.com

# 安装依赖
RUN yum install -y mysql-devel

# 在镜像中导入依赖的环境
WORKDIR /opt/python/
ADD conf/requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
4.4.生产环境(nginx,uwsgi,redis)
# 系统镜像centos7-python3.5-aiqa
FROM pings/centos7-python3.5-aiqa

# 维护者
MAINTAINER Pings 275598139@qq.com

# aiqa生产环境
# nginx,uwsgi,redis

# nginx
# 安装
WORKDIR /etc/yum.repos.d
RUN echo "[nginx]" >> nginx.repo
RUN echo "name=nginx repo" >> nginx.repo
RUN echo "baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/" >> nginx.repo
RUN echo "gpgcheck=0" >> nginx.repo
RUN echo "enabled=1" >> nginx.repo
RUN yum -y install nginx
# 配置
WORKDIR /etc/nginx/conf.d
COPY conf/nginx/aiqa.conf aiqa.conf
COPY conf/nginx/robot.conf robot.conf
RUN rm -f default.conf

# uwsgi
RUN pip3 install uwsgi
WORKDIR /opt/project/product/script
COPY conf/uwsgi/uwsgi.ini uwsgi.ini

# redis
WORKDIR /opt/project
RUN wget http://download.redis.io/releases/redis-4.0.9.tar.gz
RUN tar zvxf redis-4.0.9.tar.gz
WORKDIR /opt/project/redis-4.0.9
RUN make
RUN rm -f /opt/projectredis-4.0.9.tar.gz

# 启动脚本
WORKDIR /opt/project/product
COPY conf/start_aiqa.sh start_aiqa.sh

# 开放端口
EXPOSE 8001
EXPOSE 80
4.5.智能问答环境
# 系统镜像centos7-aiqa-pro-env
FROM pings/centos7-aiqa-pro-env

# 维护者
MAINTAINER Pings 275598139@qq.com

# 添加项目
WORKDIR /opt/project/product
COPY AIQA AIQA

# 启动
CMD ["./start_aiqa.sh"]
  • 编译:

    docker build -t pings/centos7-aiqa-pro -f centos7-aiqa-pro .
  • 运行:

    docker run -p 80:80 -p 8001:8001 pings/centos7-aiqa-pro
  • 管理运行的容器:docker exec -it 容器id /bin/bash

    • 查看所有的容器:docker ps -a
    • 查看运行的容器:docker ps
    • 删除容器:docker rm 容器id
    • 删除镜像:docker image rm 镜像id
    • 启动/停止容器:docker start/stop 容器id

5.源码

https://github.com/pingszi/MyDockerfile.git -> aiqa

6.遇到的问题

  • 镜像加速
    vim /etc/systemd/system/multi-user.target.wants/docker.service
    ExecStart=/usr/bin/dockerd –registry-mirror=https://eqrfvvq2.mirror.aliyuncs.com
  • configure: error: no acceptable C compiler found in $PATH
    原因:缺少gcc
    解决:RUN yum install -y gcc
  • Ignoring ensurepip failure: pip 9.0.1 requires SSL/TLS
    原因:缺少SSL
    解决:RUN yum install -y openssl-devel
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值