栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Python服务docker file 的两种方式

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python服务docker file 的两种方式

方法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"]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/840900.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号