我有一个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的。 或许值得一读。
主要是Docker无法识别第一个和第二个运行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.
该问题的解决方案是在运行curl
之前安装ca-certificates
。 我已经更新了上面的run
命令。