【发布时间】:2020-06-11 21:59:28
【问题描述】:
上下文
我有一个 Dockerfile 来创建一个包含 apache 网络服务器的图像。但是,我还想使用 Dockerfile 构建我的网站,以便构建过程不依赖于开发人员的本地环境。请注意,docker 容器仅用于本地开发而非生产。
问题
我有这个 Dockerfile:
FROM httpd
RUN apt-get update -yq
RUN apt-get -yq install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get update -yq
RUN apt-get install -yq \
dh-autoreconf=19 \
ruby=1:2.5.* \
ruby-dev=1:2.5.* \
nodejs
我建造它:
sudo docker build --no-cache .
构建成功完成,以下是部分输出:
Step 9/15 : RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
---> Running in e6c747221ac0
......
......
......
Removing intermediate container 5a07dd0b1e01
---> 6279003c1e80
Successfully built 6279003c1e80
但是,当我使用这个在容器中运行图像时:
sudo docker container run --rm -it --name=debug 6279003c1e80 /bin/bash
然后在容器内执行apt-cache policy 时,它不会显示应该使用 curl 命令添加的存储库。同样在执行apt-cache policy nodejs 时,它显示已安装旧版本。
但是,当我在容器内运行以下命令时:
curl -sL https://deb.nodesource.com/setup_12.x | bash
apt-cache policy
apt-cache policy nodejs
它显示存储库已添加,并显示更新的 nodejs 版本可用。
那么为什么当使用 curl 命令在 docker 文件中使用 RUN 时它似乎不起作用,但是当从 shell 在容器中手动执行它时它确实起作用?我该如何解决这个问题?
更新
- 请注意,为了防止出现缓存问题,我使用了 --no-cache 标志。
- 我还删除了所有容器并执行了
sudo docker system prune并重建了映像,但没有成功。 - 我尝试按照用户“hmm”的建议将所有内容捆绑在一个 RUN 命令中(因为这是 apt 命令的最佳做法):
RUN apt-get update -yq \
&& apt-get -yq install curl gnupg && \
&& curl -sL https://deb.nodesource.com/setup_12.x | bash \
&& apt-get update -yq \
&& apt-get install -yq \
dh-autoreconf=19 \
ruby=1:2.5.* \
ruby-dev=1:2.5.* \
nodejs \
&& rm -rf /var/lib/apt/lists/*
【问题讨论】: