2

昨天当我在 python:3.6-buster 图像上构建我的 Python Flask 应用程序时没有问题。但是今天我收到了这个错误。

Calculating upgrade...
The following packages will be upgraded: libgnutls-dane0 libgnutls-openssl27 libgnutls28-dev libgnutls30 libgnutlsxx28
5 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 2859 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
ERROR: Service 'gateway' failed to build: The command '/bin/sh -c apt-get upgrade' returned a non-zero code: 1

我的 Dockerfile:

FROM python:3.6-buster
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN echo $TZ > /etc/timezone
RUN apt-get update
RUN apt-get upgrade
RUN apt-get -y install gcc musl-dev libffi-dev
COPY requirements.txt requirements.txt
RUN python3 -m pip install -r requirements.txt
COPY . /application
WORKDIR /application
EXPOSE 7000

我找不到任何相关的问题。我来宾这是关于一个新的更新,但我实际上不知道。这个问题有什么建议或解决方案吗?

4

1 回答 1

2

我想这apt是等待用户输入以确认升级。如果没有 hacky 解决方案,Docker 构建器就无法处理这些交互式对话框。因此,它失败了。

最直接的解决方案是在-y您的命令中添加标志,就像您在 install 命令中执行的那样。

FROM python:3.6-buster
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN echo $TZ > /etc/timezone
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install gcc musl-dev libffi-dev
COPY requirements.txt requirements.txt
RUN python3 -m pip install -r requirements.txt
COPY . /application
WORKDIR /application
EXPOSE 7000

但是...您真的需要更新现有的软件包吗?在您的情况下,这可能不是必需的。此外,我可能会建议您查看Docker 最佳实践来编写包括apt命令在内的语句。为了使您的图像大小保持较小,您应该考虑在单个 RUN 语句中压缩这些命令。此外,您应该在之后删除 apt 缓存,以最大程度地减少两层之间的更改:

FROM python:3.6-buster
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
RUN echo $TZ > /etc/timezone
RUN apt-get update \
&&  apt-get -y install gcc musl-dev libffi-dev \
&&  rm -rf /var/lib/apt/lists/*
COPY requirements.txt requirements.txt
RUN python3 -m pip install -r requirements.txt
COPY . /application
WORKDIR /application
EXPOSE 7000
于 2020-04-07T16:26:27.047 回答