方法1:简单方式:
FROM python:3.6.8-slim-stretch
ENV LANG en_US.UTF-8 LC_ALL=en_US.UTF-8
COPY . /home/code/label_recongnize
RUN cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& cd /home/code/news_label_recongnize \
&& python3 -m pip install --trusted-host mirrors.aliyun.com --no-cache-dir -i http://mirrors.aliyun.com/pypi/simple -r /home/code/label_recongnize/requirements.txt \
&& rm -rf /var/cache/* \
&& rm -rf /tmp/*
ENV PYTHONIOENCODING=utf-8
WORKDIR /home/code/label_recongnize/service
ENTRYPOINT ["python3","test.py","&"]
方法2:
# using ubuntu LTS version
FROM ubuntu:20.04 AS builder-image
# avoid stuck build due to user prompt
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install --no-install-recommends -y python3.9 python3.9-dev python3.9-venv python3-pip python3-wheel build-essential && \
apt-get clean && rm -rf /var/lib/apt/lists/*
#配置pip镜像源
# create and activate virtual environment
# using final folder name to avoid path issues with packages
RUN python3.9 -m venv /home/tempuser/venv
ENV PATH="/home/tempuser/venv/bin:$PATH"
# install requirements
COPY requirements.txt .
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir wheel
RUN pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple --no-cache-dir -r requirements.txt
FROM ubuntu:20.04 AS runner-image
RUN apt-get update && apt-get install --no-install-recommends -y python3.9 python3-venv curl telnet vim && \
apt-get clean && rm -rf /var/lib/apt/lists/*
#timezone config
RUN cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN mkdir /home/tempuser
COPY --from=builder-image /home/tempuser/venv /home/tempuser/venv
RUN mkdir /home/tempuser/code
WORKDIR /home/tempuser/code
COPY . .
EXPOSE 8008
# make sure all messages always reach console
ENV PYTHONUNBUFFERED=1
ENV PYTHONIOENCODING=utf-8
# activate virtual environment
ENV VIRTUAL_ENV=/home/tempuser/venv
ENV PATH="/home/tempuser/venv/bin:$PATH"
# /dev/shm is mapped to shared memory and should be used for gunicorn heartbeat
# this will improve performance and avoid random freezes
ENTRYPOINT ["python3.9","/home/tempuser/code/service/main.py"]
其中CMD和ENTRYPOINT 相关介绍Dockerfile中CMD和ENTRYPOINT的用法_雨还是不停的落下的技术博客_51CTO博客
本文介绍了两种使用Dockerfile构建Python应用镜像的方法。方法一基于`python:3.6.8-slim-stretch`,设置了环境变量,拷贝代码并安装依赖。方法二分为构建和运行阶段,使用`ubuntu:20.04`,创建虚拟环境,安装必要软件,配置时区,并从构建阶段复制虚拟环境到运行阶段。两个方法都确保了Python的编码设置,并指定了入口点。
739

被折叠的 条评论
为什么被折叠?



