我想在docker上运行jenkins并更改用户访问权限,以便可以读取SSH密钥并访问git。这是dockerfile的示例
FROM jenkins/jenkins:lts USER root COPY --chown=jenkins:jenkins id_rsa $JENKINS_HOME/.ssh/id_rsa COPY --chown=jenkins:jenkins id_rsa.pub $JENKINS_HOME/.ssh/id_rsa.pub RUN /bin/bash -c 'ls -la $JENKINS_HOME/.ssh; chmod 600 -R $JENKINS_HOME/.ssh; ls -la $JENKINS_HOME/.ssh'
生成时输出成功,访问已更改!
Step 3/3 : RUN /bin/bash -c 'ls -la $JENKINS_HOME/.ssh; chmod 600 -R $JENKINS_HOME/.ssh; ls -la $JENKINS_HOME/.ssh' ---> Running in 137d1a4f9f6d total 16 drwxr-xr-x 2 jenkins jenkins 4096 Jan 8 04:11 . drwxr-xr-x 3 jenkins jenkins 4096 Jan 8 04:11 .. -rwxr-xr-x 1 jenkins jenkins 1843 Jan 2 02:33 id_rsa -rwxr-xr-x 1 jenkins jenkins 413 Jan 2 02:33 id_rsa.pub total 16 drw------- 2 jenkins jenkins 4096 Jan 8 04:11 . drwxr-xr-x 3 jenkins jenkins 4096 Jan 8 04:11 .. -rw------- 1 jenkins jenkins 1843 Jan 2 02:33 id_rsa -rw------- 1 jenkins jenkins 413 Jan 2 02:33 id_rsa.pub Removing intermediate container 137d1a4f9f6d ---> 7d6334d2b044
但是,当我进入/bin/bash访问设置为默认时,chmod无法正常工作
/bin/bash
jenkins@f49048ec8c88:/$ ls -al /var/jenkins_home/.ssh/ total 16 drwxr-xr-x 2 jenkins jenkins 4096 Jan 8 04:25 . drwxr-xr-x 16 jenkins jenkins 4096 Jan 8 04:26 .. -rwxr-xr-x 1 jenkins jenkins 1843 Jan 2 02:33 id_rsa -rwxr-xr-x 1 jenkins jenkins 413 Jan 2 02:33 id_rsa.pub
知道为什么这种行为吗?
发生这种情况$JENKINS_HOME是因为VOLUME在jenkins:lts基本映像中进行了定义。您可以通过3种方式中的任何一种来解决此问题
$JENKINS_HOME
VOLUME
jenkins:lts
您可以在构建之前在主机上修复权限,它应该可以工作。
您可以使用多阶段构建,更改权限并从第一阶段复制文件
FROM jenkins/jenkins:lts as base USER root COPY --chown=jenkins:jenkins id_rsa /tmp/ssh_keys/ COPY --chown=jenkins:jenkins id_rsa.pub /tmp/ssh_keys/ RUN chmod 600 -R /tmp/ssh_keys FROM jenkins/jenkins:lts USER root COPY --chown=jenkins:jenkins --from=base /tmp/ssh_keys $JENKINS_HOME/.ssh