如果构建的容器运行失败,如何编写Dockerfile让docker构建失败?
我有一个在docker中运行的nodejs web应用程序,Dockerfile只是常见的东西,如 Docker化 Node.js Web 应用。但我希望做一件事不同:如果docker容器无法运行,例如不正确的node_modules,我希望docker build
失败。
如果我只是在 Dockerfile 中添加 RUN npm run start 却无法运行,那么 docker build 确实会失败。但是如果npm run start
成功,docker build将不会退出。
我的目标是,如果 docker run 失败,我希望 docker build 首先失败。目前我找不到办法做到这一点,所以我必须先编写一个脚本 docker build
然后 docker run
并检查其结果。
--- 更新 ---
我知道通常是按照构建/测试/发布顺序进行的,测试是在构建之后进行的。但我正在尝试改进我们的 CI 流程,并且在构建之后我希望进行快速测试以确保容器可以正确启动。如果需要手动检查,那是非常低效的。所以我想为什么不更进一步呢?如果容器无法启动,为什么不只是构建失败呢?
--- 更新 2 ---
我对得到的答案感到相当不舒服。主要原因是我觉得溶液很脏。我在 SE 的软件工程网站上提出了另一个问题 通过对 docker 镜像进行测试来改进 CI 流程,如果测试失败则使 docker 构建失败,现在我完全同意我提出的建议不是一个好主意。另外 github action 是减少失败的 docker 镜像的一个很好的解决方案。
I have a nodejs webapp run in docker, the Dockerfile is just the usual stuff as in Dockerizing a Node.js web app. But I hope to do one thing differently: if the docker container fails to run, e.g. incorrect node_modules I would like docker build
fails.
If I just add RUN npm run start
in Dockerfile and it fails to run, docker build
indeed fails. But if npm run start
succeeds, docker build won't exit then.
My goal is that if docker run
fails, I hope docker build
fails first. Currently I can't find a way to do that, so I have to write a script to first docker build
then docker run
and check its result.
--- update ---
I know normally it is in build/test/release order and testing comes after the build. But I am trying to improving our CI process and after build I hope to do a quick test to make sure the container can start correctly. If that needs a manual check it is quite unproductive. So I think why not do a one step further? If container can not start why not just fails the build?
--- update 2 ---
I felt rather uncomfortable with the answer I got. The main reason is that I felt that solution was dirty. I raised another question about it at SE's softwareengineering website Improve CI process by testing against docker image and fail docker build if test fails and now I fully agree that what I proposed was not a good idea. Also github action is a good solution to reduce failed docker images.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会这样处理它:
首先找出
npm run start
需要多长时间。在正常情况下,它不会超过 15-20 秒,但假设您的应用程序需要 60 秒才能成功启动。现在,我将在 server.js 或应用程序的主入口点中保留一个条件语句,例如:您只需要确保在运行服务器时调用
RUN npm run start -- --BUILD
docker build 作为-- --BUILD
会将运行时参数传递给您的服务器,以确保在构建过程中调用它。对于普通的 docker run 入口点,您只需调用 npm run start 即可,它应该可以正常工作。I would approach it in this way:
First find out how long does
npm run start
takes for you. In normal cases it won't take more than 15-20 seconds but let's say, it takes 60 seconds for your app to successfully start. Now I would keep a conditional statement in the server.js or the main entrypoint of your app like:You just need to ensure that you call
RUN npm run start -- --BUILD
while running the server during docker build as the-- --BUILD
would pass a runtime argument to your server to ensure that it was called during the build process. For normal docker run entrypoint you can just callnpm run start
and it should work fine.