jenkins 是运行在 k8s 集群中的,job 通过 pod 启动。然后把 k8s node 上的 /var/run/docker.sock
挂载到 pipeline 的容器里。pipeline 配置如下:
podTemplate(yaml: ''' apiVersion: v1 kind: Pod spec: volumes: - name: kubectl hostPath: path: /usr/bin/kubectl type: File - name: docker-sock hostPath: path: /var/run/docker.sock type: Socket containers: - name: docker image: docker imagePullPolicy: IfNotPresent command: - sleep args: - 99d volumeMounts: - name: kubectl mountPath: /usr/bin/kubectl readOnly: true - name: docker-sock mountPath: /var/run/docker.sock ''') { node(POD_LABEL) { // 测试下来,JNLP 如果不作为第一个容器启动,会导致无法连接到 jenkins 上。 // 参照这个 issue ,只要时间太长也会有这个问题。真正原因没时间深入了解,暂时这样处理。 // // https://github.com/jenkinsci/kubernetes-operator/issues/691 stage('JNLP') { container('jnlp') { stage('ping JNLP') { sh "echo 'Hello JNLP'" } } } stage('Docker') { container('docker') { stage('clone repo') { checkout scmGit( branches: [[name: 'master']], extensions: [ cloneOption(shallow: true) ], userRemoteConfigs: [[credentialsId: 'jenkins-ssh-key', url: '*****省略*****]] ) } stage('build & push') { sh ''' docker run --rm -v /home/jenkins/agent/workspace/blog:/home/app --entrypoint hugo betterweb/hugo:extended-0.121.1-20-1 ''' } stage('rolling upgrade: TODO') { sh ''' sleep 3 docker images ''' } } } } }
现象: docker run --rm -v /home/jenkins/agent/workspace/blog:/home/app --entrypoint hugo betterweb/hugo:extended-0.121.1-20-1
这一步预期把 jenkins 的目录挂载到容器里,并执行 hugo 。但是实际上只挂载了一个空目录上去,容器里什么也没有。但是也没报错。
暂时的解决方案:试了下,改成 docker build ,在 dockerfile 里去 COPY . /home/app
倒是能把 jenkins 里的文件复制进去。
问题:为什么 -v 挂载就不行,但是 COPY 可以?
1 ho121 2023-12-23 18:12:32 +08:00 via Android ![]() jenkins 里的 docker socket ,命名空间是宿主机的,所以/home/jenkins/agent/workspace/blog 代表是宿主机的路径,不是 jenkins 容器里的 |
2 justdoit123 OP @ho121 感谢。恍然大悟! |