docker打包python+uwsgi+redis项目
经过半个月的研究,终于将一个python+uwsgi+redis项目打包成了docker镜像,必须要来记录一下我在docker碰到的坎
docker安装
安装其实很简单,我最开始遇到了一些问题,按照教程直接安装就可
安装链接
Dockerfile
基础镜像
基础镜像我是直接使用的python3.6的镜像
依赖安装
opencv 依赖
因为我使用到了opencv,所以需要安装opencv的依赖
RUN apt-get install -y libglib2.0-dev
RUN apt-get install -y libgl1-mesa-dev
uwsgi依赖
RUN apt-get install -y libsm6
RUN apt-get install -y libxrender1
RUN apt-get install -y libxext-dev
忘记什么依赖,乱七八糟的各种依赖
RUN apt-get install -y vim
RUN apt-get install -y redis-server
RUN apt-get install -y libc6-dev gcc mime-support
设置工作目录
设置docker镜像工作目录并将本地文件复制到镜像中
WORKDIR /docker-restapi/app
COPY . /docker-restapi/
pip requirements
RUN pip install --trusted-host pypi.python.org -r ./requirements.txt
开放端口
ENV NAME World
设置运行语句
要运行多个文件,redis设置为后台开放,python应用使用nohup后台运行
ENTRYPOINT nohup python ../server.py & ../redis-server ../redis.conf && nohup python ../zy_callback.py & python p_zhongyou_batch.py
完整代码
FROM python:3.6-slim
RUN apt-get update
RUN apt-get install -y libglib2.0-dev
RUN apt-get install -y libgl1-mesa-dev
RUN apt-get install -y libsm6
RUN apt-get install -y libxrender1
RUN apt-get install -y libxext-dev
#RUN apt-get install -y uwsgi
RUN apt-get install -y vim
#RUN apt-get install -y ps
#RUN apt-get install -y uwsgi-python3
RUN apt-get install -y redis-server
#RUN apt-get install aptitude
#RUN aptitude install -y python3.6-dev
#RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ precise-updates main restricted" | tee -a /etc/apt/sources.list.d/precise-updates.list
#RUN apt-get install python3.6-dev
RUN apt-get install -y libc6-dev gcc mime-support
##RUN mkdir app/docker-restapi
# Set the working directory to /app
WORKDIR /docker-restapi/ai-zyocr
# Copy the current directory contents into the container at /app
COPY . /docker-restapi/
#RUN pip install uwsgi
# Install any needed packages specified in requirements.txt
#RUN pip install --trusted-host pypi.python.org -r ./requirements.txt --use-feature=2020-resolver
RUN pip install --trusted-host pypi.python.org -r ./requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
#ENTRYPOINT uwsgi -d ../uwsgi.ini && ../redis-server ../redis.conf && nohup python ../zy_callback.py & && nohup python p_zhongyou_batch.py &
ENTRYPOINT nohup python ../server.py & ../redis-server ../redis.conf && nohup python ../callback.py & python p_app.py