[提问] Jenkins k8s 中的“docker in docker ” 挂载文件夹会变成空文件。

186 天前
 justdoit123

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 可以?

1025 次点击
所在节点    Kubernetes
2 条回复
ho121
186 天前
jenkins 里的 docker socket ,命名空间是宿主机的,所以/home/jenkins/agent/workspace/blog 代表是宿主机的路径,不是 jenkins 容器里的
justdoit123
185 天前
@ho121 感谢。恍然大悟!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/1002883

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX