gpt4 book ai didi

docker - Jenkinsfile 中的 "withRun"和 "inside"有什么区别?

转载 作者:行者123 更新时间:2023-12-04 19:25:49 25 4
gpt4 key购买 nike

我有一个 Jenkinsfile,我试图在其中执行 npm run test一个容器内。
当我用 inside 运行时它失败了,但是当我使用 withRun 运行时它按照我的意愿运行。

代码引用
inside

stage('Test') {
docker.image('justinribeiro/chrome-headless').inside ("-p 9222:9222 --security-opt seccomp=$WORKSPACE/chrome.json") {
sh label:
'Running npm test',
script: '''
npm run test
'''
}
}

withRun
stage('Test') {
docker.image('justinribeiro/chrome-headless').withRun ("-p 9222:9222 --security-opt seccomp=$WORKSPACE/chrome.json") {
sh label:
'Running npm test',
script: '''
npm run test
'''

}
}

现在我想了解它们之间有什么区别。

我观察到 inside添加卷并运行 cat在容器上而 withRun才不是。

我还阅读了文档 https://jenkins.io/doc/book/pipeline/docker/但还不够了解。

更详细的解释将不胜感激。

谢谢。

最佳答案

Image.run([args, command])
Uses docker run to run the image, and returns a Container which you could stop later. Additional args may be added, such as '-p 8080:8080 --memory-swap=-1'. Optional command is equivalent to Docker command specified after the image. Records a run fingerprint in the build.


Image.withRun[(args[, command])] {…}
Like run but stops the container as soon as its body exits, so you do not need a try-finally block.


Image.inside[(args)] {…}
Like withRun this starts a container for the duration of the body, but all external commands (sh) launched by the body run inside the container rather than on the host. These commands run in the same working directory (normally a Jenkins agent workspace), which means that the Docker server must be on localhost.


从上面可以看出,您的 sh方法命令(特别是 npm 命令)将在主机上为 withRun 执行,但在 inside 的容器内.
链接到文档: https://opensource.triology.de/jenkins/pipeline-syntax/globals

关于docker - Jenkinsfile 中的 "withRun"和 "inside"有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com