如果构建的容器运行失败,如何编写Dockerfile让docker构建失败?

发布于 2025-01-09 02:47:24 字数 1047 浏览 0 评论 0原文

我有一个在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2025-01-16 02:47:24

我会这样处理它:

首先找出 npm run start 需要多长时间。在正常情况下,它不会超过 15-20 秒,但假设您的应用程序需要 60 秒才能成功启动。现在,我将在 server.js 或应用程序的主入口点中保留一个条件语句,例如:

if (process.argv[2] === 'BUILD') { // Call only when docker build is ran
    setTimeout(() => process.exit(0), 60000) // This would wait for 60 seconds and if the app is still running it will terminate it.
}

您只需要确保在运行服务器时调用 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:

if (process.argv[2] === 'BUILD') { // Call only when docker build is ran
    setTimeout(() => process.exit(0), 60000) // This would wait for 60 seconds and if the app is still running it will terminate it.
}

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 call npm run start and it should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文