bash - Docker 构建无法从 Dockerfile 中使用的 bash 脚本中的 archive.ubuntu.com 获取包

标签 bash docker ubuntu dockerfile

尝试通过在 Dockerfile 中执行先决条件安装脚本来构建 docker 镜像,无法通过 apt-get 从 archive.ubuntu.com 获取包。 .
使用 apt-get Dockerfile 中的命令运行完美,尽管它位于通过 ENV 设置的公司代理之后。 Dockerfile 中的命令。
无论如何,执行 apt-get来自生成的 docker 容器内终端中的 bash 脚本的命令或 Visual Studio Code 的 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"]
环境脚本的最低版本env-setup.sh应该由 Dockerfile 调用,如下所示:
#!/bin/bash

packageCommand="apt-get";
sudo $packageCommand update;
packageInstallCommand="$packageCommand install";
package="iproute2"
packageInstallCommand+=" -y";
sudo $packageInstallCommand $package;
当然,变量的使用归结为使用要安装的包的列表和其他方面。
希望这已经涵盖了该问题所必需的所有内容:
为什么执行apt-get使用 RUN以及在创建后在容器内运行 bash 脚本,但在从 Dockerfile 构建镜像时不是从相同的 bash 脚本运行?
我希望在广泛的网络搜索的帮助下找到答案,但不幸的是,除了这个案例的答案,我只能找到任何东西。

最佳答案

正如问题下方的评论部分指出的那样:

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 .

关于bash - Docker 构建无法从 Dockerfile 中使用的 bash 脚本中的 archive.ubuntu.com 获取包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70614233/

相关文章:

Linux bash 脚本卡在 sleep 状态

bash - 在 bash 的左侧为多部分命令别名

java - Tomcat 中抽象类的 NoClassDefFoundError

Docker 容器 localhost 访问问题

ubuntu - coreclr 在 ubuntu 14.4 x64 上构建失败

linux - 在 Ubuntu 中重启 Nginx

用于检索所需行的 Linux 命令

docker - 应该将所有依赖项安装到docker镜像吗?还是让它取决于主机

apache - Confluence 中的 JIRA 问题/过滤器宏生成无效链接

linux - 想要压缩 7 天前的日志文件,然后移动到 Linux 中的不同目录