【问题标题】:Which Python variant to use as a base image in dockerfiles?在 dockerfiles 中使用哪个 Python 变体作为基础镜像?
【发布时间】:2017-07-31 16:51:29
【问题描述】:

我在 dockerfile 中使用 Python_onbuild 作为基础镜像,如下所示。但是,每当我对源文件进行更改时,都会使缓存无效,从而导致我的命令重复。

FROM python:2.7.13-onbuild
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN echo "Test Cache"
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install --assume-yes apt-utils
RUN apt-get update && apt-get install -y curl
RUN apt-get update && apt-get install -y unzip
RUN curl -o - url

构建日志:

Sending build context to Docker daemon  239.1kB
Step 1/6 : FROM python:2.7.13-onbuild
# Executing 3 build triggers...
Step 1/1 : COPY requirements.txt /usr/src/app/
 ---> Using cache
Step 1/1 : RUN pip install --no-cache-dir -r requirements.txt
 ---> Using cache
Step 1/1 : COPY . /usr/src/app
 ---> 13e927036649

复制(Step 1/1 : COPY . /usr/src/app) 的第三步是在我对工作目录中的任何文件进行更改时重复其余命令。我认为这是来自基本映像的 ONBUILD 命令。如果这是真的,那么在这种情况下替代的基本图像是什么?我应该使用 Python:吗?

我希望对需求安装以及复制过程进行更多控制,因为我必须下载一个 3.6GB 的文件,我不想每次构建 docker 时都重复该文件。

注意:这个特定的基础图像是由其他人选择的,我正在一些现有工作的基础上构建。

【问题讨论】:

  • 您可以将源代码复制部分移动到dockerfile的末尾..以及之前的安装过程,这样您就不必每次更改源代码时都安装。
  • @viveksyngh 我无法更改复制部分,因为它是由基本映像处理的。如您所见,我的 dockerfile 中没有明确的复制命令。

标签: python docker dockerfile


【解决方案1】:

Dockerfile 中描述了您正在使用的基本映像。
如您所见,它基于python:2.7。因此,如果您不需要 ONBUILD 说明,只需直接使用此映像或可以满足您需求的映像:您可以在 the python image on Docker Hub 上找到一个列表,其中包含指向所有相应 Dockerfile 的链接。

【讨论】:

  • 这就是我所需要的。我刚刚注意到不建议将 onbuild 用于生产用途“不推荐使用 ONBUILD 映像变体,并且不鼓励使用它们。有关更多详细信息,请参阅 docker-library/official-images#2076。”
【解决方案2】:

您可以使用基本 ubuntu 映像并安装 python,也可以使用其他安装了 python 的映像。 您需要将源代码复制部分移到需求安装部分之后,以便在更改代码时不必进行安装。这就是我的 docker 文件的样子。

############################################################
# Dockerfile to run a Django-based web application
# Based on an Ubuntu Image
############################################################

# Set the base image to use to Ubuntu
FROM ubuntu:14.04

ENV DOCKYARD_SRC=app
ENV DOCKYARD_SRVHOME=/app

# Update the default application repository sources list
RUN apt-get update 
RUN apt-get install -y vim
RUN apt-get install -y python-dev
RUN apt-get install -y python-pip
RUN apt-get install -y build-essential
RUN apt-get -y install wget
RUN apt-get -y install php5 libapache2-mod-php5 php5-mcrypt
RUN apt-get -y install curl libcurl3 libcurl3-dev php5-curl
RUN pip install uwsgi

RUN mkdir $DOCKYARD_SRC

COPY ./requirements.txt $DOCKYARD_SRVHOME/requirements.txt
RUN pip install -r $DOCKYARD_SRVHOME/requirements.txt
COPY . $DOCKYARD_SRVHOME

EXPOSE 8001 8000

WORKDIR $DOCKYARD_SRVHOME

RUN chmod +x entrypoint.sh

CMD ["./entrypoint.sh"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2017-09-29
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多