Docker 构建无法从 Docker 文件中使用的 bash 脚本中的 archive.ubuntu.com 获取包
Docker build fails to fetch packages from archive.ubuntu.com inside bash script used in Dockerfile
尝试通过在 Dockerfile 中执行先决条件安装脚本来构建 docker 映像,但无法通过 apt-get 从 archive.ubuntu.com.
获取包
在 Dockerfile 中使用 apt-get
命令可以完美运行,尽管它位于企业代理之后,该代理是通过 Dockerfile 中的 ENV
命令设置的。
无论如何,从生成的 docker 容器内的终端中的 bash-脚本执行 apt-get
命令,或者在 Visual Studio 代码的 devcontainer.json 中作为“postCreateCommand”执行确实也按预期工作。但在我的情况下,从 Dockerfile 内部调用 bash 脚本是行不通的。
它只会告诉:
Starting installation of package iproute2
Reading package lists...
Building dependency tree...
The following additional packages will be installed:
libatm1 libcap2 libcap2-bin libmnl0 libpam-cap libxtables12
Suggested packages:
iproute2-doc
The following NEW packages will be installed:
iproute2 libatm1 libcap2 libcap2-bin libmnl0 libpam-cap libxtables12
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 971 kB of archives.
After this operation, 3,287 kB of additional disk space will be used.
Err:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libcap2 amd64 1:2.32-1
Could not resolve 'archive.ubuntu.com'
... more output ...
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/libc/libcap2/libcap2_2.32-1_amd64.deb Could not resolve 'archive.ubuntu.com'
... more output ...
例如,Dockerfile 的片段如下所示:
FROM ubuntu:20.04 as builderImage
USER root
ARG HTTP_PROXY_HOST_IP='http://172.17.0.1'
ARG HTTP_PROXY_HOST_PORT='3128'
ARG HTTP_PROXY_HOST_ADDR=$HTTP_PROXY_HOST_IP':'$HTTP_PROXY_HOST_PORT
ENV http_proxy=$HTTP_PROXY_HOST_ADDR
ENV https_proxy=$http_proxy
ENV HTTP_PROXY=$http_proxy
ENV HTTPS_PROXY=$http_proxy
ENV ftp_proxy=$http_proxy
ENV FTP_PROXY=$http_proxy
# it is always helpful sorting packages alpha-numerically to keep the overview ;)
RUN apt-get update && \
apt-get -y upgrade && \
apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
&& \
apt-get -y install \
default-jdk \
git \
python3 python3-pip
SHELL ["/bin/bash", "-c"]
ADD ./env-setup.sh .
RUN chmod +x env-setup.sh && ./env-setup.sh
CMD ["bash"]
应该由 Dockerfile 调用的环境脚本 env-setup.sh
的最小版本如下所示:
#!/bin/bash
packageCommand="apt-get";
sudo $packageCommand update;
packageInstallCommand="$packageCommand install";
package="iproute2"
packageInstallCommand+=" -y";
sudo $packageInstallCommand $package;
当然,变量的使用归结为使用要安装的软件包列表和其他方面。
希望这已经涵盖了问题的所有必要内容:
为什么 apt-get
的执行与 RUN
以及 运行 创建后容器内的 bash 脚本一起工作,但不是来自同一个bash 从 Dockerfile 构建镜像时的脚本?
我希望在广泛的网络搜索的帮助下找到答案,但不幸的是我只能找到这个案例的答案。
正如问题下方的评论部分所指出的:
using sudo to launch the command, wiping out all the current vars set in the current environment, more specifically your proxy settings
原来如此。
解决方案是从 bash 脚本中删除 sudo 并在 Dockerfile 中以 root 身份调用脚本。
或者,使用 sudo
将使用 ENV
个变量,只需应用 sudo -E
.
尝试通过在 Dockerfile 中执行先决条件安装脚本来构建 docker 映像,但无法通过 apt-get 从 archive.ubuntu.com.
获取包在 Dockerfile 中使用 apt-get
命令可以完美运行,尽管它位于企业代理之后,该代理是通过 Dockerfile 中的 ENV
命令设置的。
无论如何,从生成的 docker 容器内的终端中的 bash-脚本执行 apt-get
命令,或者在 Visual Studio 代码的 devcontainer.json 中作为“postCreateCommand”执行确实也按预期工作。但在我的情况下,从 Dockerfile 内部调用 bash 脚本是行不通的。
它只会告诉:
Starting installation of package iproute2
Reading package lists...
Building dependency tree...
The following additional packages will be installed:
libatm1 libcap2 libcap2-bin libmnl0 libpam-cap libxtables12
Suggested packages:
iproute2-doc
The following NEW packages will be installed:
iproute2 libatm1 libcap2 libcap2-bin libmnl0 libpam-cap libxtables12
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 971 kB of archives.
After this operation, 3,287 kB of additional disk space will be used.
Err:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libcap2 amd64 1:2.32-1
Could not resolve 'archive.ubuntu.com'
... more output ...
E: Failed to fetch http://archive.ubuntu.com/ubuntu/pool/main/libc/libcap2/libcap2_2.32-1_amd64.deb Could not resolve 'archive.ubuntu.com'
... more output ...
例如,Dockerfile 的片段如下所示:
FROM ubuntu:20.04 as builderImage
USER root
ARG HTTP_PROXY_HOST_IP='http://172.17.0.1'
ARG HTTP_PROXY_HOST_PORT='3128'
ARG HTTP_PROXY_HOST_ADDR=$HTTP_PROXY_HOST_IP':'$HTTP_PROXY_HOST_PORT
ENV http_proxy=$HTTP_PROXY_HOST_ADDR
ENV https_proxy=$http_proxy
ENV HTTP_PROXY=$http_proxy
ENV HTTPS_PROXY=$http_proxy
ENV ftp_proxy=$http_proxy
ENV FTP_PROXY=$http_proxy
# it is always helpful sorting packages alpha-numerically to keep the overview ;)
RUN apt-get update && \
apt-get -y upgrade && \
apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
&& \
apt-get -y install \
default-jdk \
git \
python3 python3-pip
SHELL ["/bin/bash", "-c"]
ADD ./env-setup.sh .
RUN chmod +x env-setup.sh && ./env-setup.sh
CMD ["bash"]
应该由 Dockerfile 调用的环境脚本 env-setup.sh
的最小版本如下所示:
#!/bin/bash
packageCommand="apt-get";
sudo $packageCommand update;
packageInstallCommand="$packageCommand install";
package="iproute2"
packageInstallCommand+=" -y";
sudo $packageInstallCommand $package;
当然,变量的使用归结为使用要安装的软件包列表和其他方面。
希望这已经涵盖了问题的所有必要内容:
为什么 apt-get
的执行与 RUN
以及 运行 创建后容器内的 bash 脚本一起工作,但不是来自同一个bash 从 Dockerfile 构建镜像时的脚本?
我希望在广泛的网络搜索的帮助下找到答案,但不幸的是我只能找到这个案例的答案。
正如问题下方的评论部分所指出的:
using sudo to launch the command, wiping out all the current vars set in the current environment, more specifically your proxy settings
原来如此。
解决方案是从 bash 脚本中删除 sudo 并在 Dockerfile 中以 root 身份调用脚本。
或者,使用 sudo
将使用 ENV
个变量,只需应用 sudo -E
.