Jenkins使用docker的管道:以特定用户身份运行docker(嵌入式postgresql

时间:2019-02-08 09:45:39

标签: postgresql docker jenkins jenkins-pipeline root

嗨,Stack Overflow社区!

我有一个maven-java项目,需要使用jenkins管道进行构建。 为此,我使用docker映像maven:3.3.3配置了作业。一切正常,除了我使用ru.yandex.qatools.embed:postgresql-embedded之外。这在本地有效,但是在詹金斯上,它抱怨启动Postgres:

2019-02-08 09:31:20.366  WARN 140 --- [ost-startStop-1] r.y.q.embed.postgresql.PostgresProcess: Possibly failed to run initdb: 

initdb: cannot be run as root

Please log in (using, e.g., "su") as the (unprivileged) user that will own the server process.

2019-02-08 09:31:40.999 ERROR 140 --- [ost-startStop-1] r.y.q.embed.postgresql.PostgresProcess: Failed to read PID file (File '/var/.../target/database/postmaster.pid' does not exist)

java.io.FileNotFoundException: File '/var/.../target/database/postmaster.pid' does not exist

显然,出于安全原因,Postgres不允许以超级用户特权运行。

我尝试通过创建自己的docker-image版本并将以下内容添加到DockerFile来以用户身份运行:

RUN useradd myuser
USER myuser

当我从服务器的终端启动docker镜像时,这有效。但是通过使用jenkins管道,whoami仍会打印“ root”,这表明Jenkins管道在方案后面使用了run -u,这会否定DockerFile?

我的管道作业目前是如此简单:

pipeline {
    agent {
        docker {
            image 'custom-maven:1'
        }
    }
    stages {
        stage('Checkout') {
             ...
        }
        stage('Build') {
            steps {
                sh 'whoami'
                sh 'mvn clean install'
            }
        }
    }
}

所以,我的问题是:如何以其他用户身份启动此docker映像?还是在运行mvn clean install之前切换用户?

更新:

通过在jenkins管道中将-u myuser作为args添加,我确实以正确的用户身份登录,但是该作业无法访问jenkins-log文件(希望这是唯一的问题)。用户myuser已添加到组根目录,但这没有区别:

agent {
    docker {
        image 'custom-maven:1'
        args '-u myuser'
    }
}

错误:

sh: 1: cannot create /var/.../jenkins-log.txt: Permission denied
sh: 1: cannot create /var/.../jenkins-result.txt.tmp: Permission denied
mv: cannot stat ‘/var/.../jenkins-result.txt.tmp’: No such file or directory
touch: cannot touch ‘/var/.../jenkins-log.txt’: Permission denied

3 个答案:

答案 0 :(得分:1)

@Thomas Stubbe

我遇到了与您上面提到的几乎相同的错误。我也有一张上面有postgresql的图片。在Dockerfile上,我创建了一个名为tdv的用户,并在容器上使用id命令检查tdv权限为1000:1000。我可以通过Jenkins管道启动容器,但是它无法执行sh命令,即使我为每个命令添加sudo -u tdv。您做了其他配置吗?

我的Jenkins Pileline脚本如下:

pipeline{
    agent none
  stages{
    stage('did operation inside container'){
     agent {
        docker{
        image 'tdv/tdv-test:8.2'
        label 'docker_machine'
        customWorkspace "/opt/test"
        registryUrl 'https://xxxx.xxxx.com'
        registryCredentialsId '8269c5cd-321e-4fab-919e-9ddc12b557f3'
        args '-u tdv --name tdv-test -w /opt/test -v /opt/test:/opt/test:rw,z -v /opt/test@tmp:/opt/test@tmp:rw,z -p 9400:9400 -p 9401:9401 -p 9402:9402 -p 9403:9403 -p 9407:9407 -p 9303:9303 --cpus=2.000 -m=4g xxx.xxx.com/tdv/tdv-test:8.2 tdv.server'
      }
    }
       steps{

               sh 'sudo tdv whoami'
               sh 'sudo tdv pwd'
               sh 'sudo tdv echo aaa'

       }

  }
    }
}

运行作业后,我可以检查容器实际启动了。但仍然会出现类似以下的错误

$ docker top f1140072d77c5bed3ce43a5ad2ab3c4be24e8c32cf095e83c3fd01a883e67c4e -eo pid,comm
ERROR: The container started but didn't run the expected command. Please double check your ENTRYPOINT does execute the command passed as docker run argument, as required by official docker images (see https://github.com/docker-library/official-images#consistency for entrypoint consistency requirements).
Alternatively you can force image entrypoint to be disabled by adding option `--entrypoint=''`.
[Pipeline] {
[Pipeline] sh
sh: /opt/test@tmp/durable-081990da/jenkins-log.txt: Permission denied
sh: /opt/test@tmp/durable-081990da/jenkins-result.txt.tmp: Permission denied
touch: cannot touch ‘/opt/test@tmp/durable-081990da/jenkins-log.txt’: Permission denied
mv: cannot stat ‘/opt/test@tmp/durable-081990da/jenkins-result.txt.tmp’: No such file or directory
touch: cannot touch ‘/opt/test@tmp/durable-081990da/jenkins-log.txt’: Permission denied
touch: cannot touch ‘/opt/test@tmp/durable-081990da/jenkins-log.txt’: Permission denied

答案 1 :(得分:0)

我已经解决了我们的问题。我所做的是在sudo命令之前的mvn。请记住,每个sh步骤都有自己的外壳,因此您需要在每个sudo步骤中执行sh

sh 'sudo -u <youruser> mvn <clean or whatever> -f <path/to/pomfile.xml>'

必须在Dockerfile中创建用户。我已经创建了没有密码的密码,但是由于您是root用户,所以我认为这无关紧要。

您必须使用sudo而不是切换用户或其他方式,否则必须提供密码。

这绝不是一种干净的方法...我建议不要搞乱用户切换,除非您确实需要(例如运行嵌入式postgres)

答案 2 :(得分:0)

解决方案

您当前有sh 'sudo tdv whoami'

我认为您应该在sudo之后添加-u标志,如下所示: sh 'sudo -u tdv whoami'