Tho*_*bbe 5 postgresql root jenkins docker jenkins-pipeline
Stack Overflow 社区大家好!
\n\n我有一个 Maven - java 项目,需要使用 jenkins 管道构建。\n为此,我已使用 docker 映像配置了作业maven:3.3.3。一切正常,除了我使用的事实ru.yandex.qatools.embed:postgresql-embedded. 这在本地有效,但在 jenkins 上它抱怨启动 Postgres:
2019-02-08 09:31:20.366 WARN 140 --- [ost-startStop-1] r.y.q.embed.postgresql.PostgresProcess: Possibly failed to run initdb: \n\ninitdb: cannot be run as root\n\nPlease log in (using, e.g., "su") as the (unprivileged) user that will own the server process.\n\n2019-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)\n\njava.io.FileNotFoundException: File \'/var/.../target/database/postmaster.pid\' does not exist\nRun Code Online (Sandbox Code Playgroud)\n\n显然,出于安全原因,Postgres 不允许以超级用户权限运行。
\n\n我尝试通过创建我自己的 docker-image 版本并将以下内容添加到 DockerFile 来以用户身份运行:
\n\nRUN useradd myuser\nUSER myuser\nRun Code Online (Sandbox Code Playgroud)\n\n当我从服务器的终端启动 docker 映像时,这会起作用。但是通过使用 jenkins pipeline,whoami 仍然打印 \'root\',这表明 Jenkins Pipeline 使用run -u在方案后面使用,这会否决 DockerFile?
我的管道工作目前很简单:
\n\npipeline {\n agent {\n docker {\n image \'custom-maven:1\'\n }\n }\n stages {\n stage(\'Checkout\') {\n ...\n }\n stage(\'Build\') {\n steps {\n sh \'whoami\'\n sh \'mvn clean install\'\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n所以,我的问题是:如何以不同的用户身份启动此 docker 映像?或者运行前切换用户mvn clean install?
更新:
\n\n通过在 jenkins 管道中添加 -u myuser 作为参数,我确实以正确的用户身份登录,但随后作业无法访问 jenkins-log 文件(希望这是唯一的问题)。用户 myuser 被添加到组 root 中,但这没有什么区别:
\n\nagent {\n docker {\n image \'custom-maven:1\'\n args \'-u myuser\'\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n和错误:
\n\nsh: 1: cannot create /var/.../jenkins-log.txt: Permission denied\nsh: 1: cannot create /var/.../jenkins-result.txt.tmp: Permission denied\nmv: cannot stat \xe2\x80\x98/var/.../jenkins-result.txt.tmp\xe2\x80\x99: No such file or directory\ntouch: cannot touch \xe2\x80\x98/var/.../jenkins-log.txt\xe2\x80\x99: Permission denied\nRun Code Online (Sandbox Code Playgroud)\n
我已经解决了我们案例中的问题。我所做的是在命令sudo之前mvn。请记住,每个sh步骤都有自己的外壳,因此您需要sudo在每个sh步骤中执行以下操作:
sh 'sudo -u <youruser> mvn <clean or whatever> -f <path/to/pomfile.xml>'
Run Code Online (Sandbox Code Playgroud)
必须在 Dockerfile 中创建用户。我创建的它没有密码,但我认为这并不重要,因为你是 root ......
您必须使用sudo而不是切换用户或其他东西,否则您需要提供密码。
这远非一种干净的方式...我建议不要搞乱用户切换,除非你真的需要(比如运行嵌入式 postgres)
| 归档时间: |
|
| 查看次数: |
5237 次 |
| 最近记录: |