V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
justdoit123
V2EX  ›  Kubernetes

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

  •  
  •   justdoit123 · 130 天前 · 957 次点击
    这是一个创建于 130 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    2 条回复    2023-12-24 15:33:26 +08:00
    ho121
        1
    ho121  
       130 天前 via Android   ❤️ 1
    jenkins 里的 docker socket ,命名空间是宿主机的,所以/home/jenkins/agent/workspace/blog 代表是宿主机的路径,不是 jenkins 容器里的
    justdoit123
        2
    justdoit123  
    OP
       129 天前
    @ho121 感谢。恍然大悟!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2381 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 12:42 · PVG 20:42 · LAX 05:42 · JFK 08:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.