上下文
我有一个Dockerfile创建包含Apache Web服务器的镜像。但是,我也想使用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版本。
那么,为什么在docker文件中使用带有
RUN
的curl命令时,它似乎不起作用,但是从shell在容器中手动进行操作就可以了吗?我该如何解决这个问题?更新
sudo docker system prune
并重建了图像,但没有成功。 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/*
最佳答案
您可能会遇到缓存图层的问题。 Dockerfile最佳实践文档中有一个关于apt-get的long section。大概值得一读。
要点是,Docker无法识别第一个和第二个RUN apt-get update
之间的区别,也不知道apt-get install
取决于新的apt-get update
层。
解决方案是将所有这些组合到单个RUN
命令中(推荐),或者在构建过程中禁用缓存(docker build --no-cache
)。
RUN apt-get update -yq \
&& apt-get -yq install curl gnupg ca-certificates \
&& curl -L 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
编辑:在本地运行Dockerfile,我发现
curl
命令没有输出。删除-s
标志(静默失败)后,您会看到它由于无法验证服务器的SSL证书而失败:curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
该问题的解决方案是在运行
ca-certificates
之前安装curl
。我已经更新了上面的RUN
命令。
关于node.js - 在Dockerfile中安装Node.js和NPM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62325403/