尊敬的 Stackoverflow 社区,
我正在尝试使用 docker 镜像作为构建过程的容器来设置 Jenkins CI 管道。我正在定义一个 Jenkinsfile 以将构建管道作为代码。我正在做这样的事情:
node {
docker.withRegistry('http://my.registry.com', 'docker-credentials') {
def buildimage = docker.image('buildimage:latest');
buildimage.pull();
buildimage.inside("")
{
stage('Checkout sources') {
git url: '...', credentialsId: '...'
}
stage('Run Build and Publish') {
sh "..."
}
}
}
}
不幸的是,我偶然发现了 Docker 管道插件的奇怪行为。在构建输出中,我可以看到 Image.inside(...) 命令使用一个
触发容器docker run -t -d -u 1000:1000 ...
这使我的构建失败,因为在 Dockerfile 中定义的用户没有 UID 1000 ...实际上是另一个用户。我什至尝试在 Jenkinsfile 中指定应该使用哪个用户
node {
docker.withRegistry('http://my.registry.com', 'docker-credentials') {
def buildimage = docker.image('buildimage:latest');
buildimage.pull();
buildimage.inside("-u otheruser:othergroup")
{
stage('Checkout sources') {
git url: '...', credentialsId: '...'
}
stage('Run Build and Publish') {
sh "..."
}
}
}
}
但这会导致生成的 docker run 命令中出现重复的 -u 开关
docker run -t -d -u 1000:1000 -u otheruser:othergroup ...
显然只有第一个 -u 被应用,因为我的构建仍然失败。我还使用 whoami 进行了调试以验证我的假设。
所以我的问题是:我怎样才能改变这种行为?有没有可以关闭 -u 1000:1000 的开关?这甚至是一个错误吗?我实际上喜欢使用 Docker 插件,因为它简化了使用自己的 docker 注册表的过程,并在 Jenkins 中维护了凭据。但是,如果 Docker 插件不可用,还有其他简单的方法可以实现我的目标吗?
提前感谢您的宝贵时间
最佳答案
我发现您实际上可以通过添加 args
来更改用户,如下所示。虽然 -u 1000:1000 仍将存在于 docker run
中,但您将在 1000:1000 之后添加一个 -u [您的用户]。 Docker 将实际使用最新的 -u 参数
agent {
docker {
image 'your image'
args '-u root --privileged'
}
}
关于docker - Jenkins + Docker : How to control docker user when using Image. 内部命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42630894/