Docker:与手动逐步构建相比,构建结果不同

Docker: build with different results compared to manual step-by-step build

我正在尝试使用简单的 Dockerfile 构建 docker 图像:

FROM ubuntu:16.04

RUN echo '91.189.88.161   archive.ubuntu.com' >> /etc/hosts;
RUN echo '91.189.88.162   security.ubuntu.com' >> /etc/hosts;
RUN apt-get update
RUN apt-get install -y git-core python-pip xvfb wkhtmltopdf python-setuptools python-dev build-essential imagemagick libjpeg-dev locales libpq-dev postgresql-client
RUN pip install -r /home/user/requirements/homologacao.txt

CMD /bin/bash

但它不起作用。几秒钟后,它在第 3 步(apt-get 更新)中抛出一些错误:

Temporary failure resolving 'archive.ubuntu.com'

但是...如果我使用以下命令手动创建并附加到容器

docker run -it ubuntu:16.04

然后 运行 每个单独的命令,它就像一个魅力!任何人都可以向我解释为什么这个构建不起作用,而手动 运行ning 命令可以吗?看起来每个 layer/command 都没有任何效果。

我应该怎么做才能让这个自动构建工作?我已将 /etc/default/docker 更改为在 DNS 列表中包含 docker 网络接口:

DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4 --dns 172.17.0.1"

一些其他可能有帮助的信息:运行 在 ubuntu 16.04 主机中,并且此问题不会发生在同一主机的 VM(也 ubuntu 16.04)中机器。在 VM 内部,apt-get 更新开箱即用,无需更新 /etc/hosts.

我刚试过你的 Dockerfileapt-get update 效果很好。

请尝试以下步骤:

  1. 创建或编辑/etc/docker/daemon.json

  2. 添加以下内容

{
  "dns": ["8.8.8.8", "8.8.4.4"]
}
  1. 重新启动 Docker 守护程序:sudo service docker restart

编辑:试试这个。

FROM ubuntu:16.04

RUN echo '91.189.88.161   archive.ubuntu.com' >> /etc/hosts && \
    echo '91.189.88.162   security.ubuntu.com' >> /etc/hosts && \
    apt-get update && \
    apt-get install -y git-core python-pip xvfb wkhtmltopdf python-setuptools python-dev build-essential imagemagick libjpeg-dev locales libpq-dev postgresql-client

RUN pip install -r /home/user/requirements/homologacao.txt

CMD /bin/bash