1.目标:搭建智能问答项目(python、django)生产环境
- 机器人:nginx,80端口,机器人通过接口访问后台管理系统获取数据;
- 后台管理系统:nginx->uwsgi,uwsgi8088端口,nginx代理设置8001端口;
- 项目部署结构:https://blog.csdn.net/zhouping118/article/details/80243065
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