方法1:简单方式:
FROM python:3.6.8-slim-stretch ENV LANG en_US.UTF-8 LC_ALL=en_US.UTF-8 COPY . /home/code/news_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/news_label_recongnize/requirements.txt && rm -rf /var/cache/* && rm -rf /tmp/* ENV PYTHONIOENCODING=utf-8 WORKDIR /home/code/news_label_recongnize/service CMD 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/* # create and activate virtual environment # using final folder name to avoid path issues with packages RUN python3.9 -m venv /home/gyd/venv ENV PATH="/home/gyd/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 && apt-get clean && rm -rf /var/lib/apt/lists/* #timezone config RUN -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN useradd --create-home gyd COPY --from=builder-image /home/gyd/venv /home/gyd/venv USER gyd RUN mkdir /home/gyd/code WORKDIR /home/gyd/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/gyd/venv ENV PATH="/home/gyd/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 CMD ["python3.9","/home/gyd/code/service/stock_check_service.py"]



