Use git in jenkins pipeline with docker agent

When I try to run the pipeline below, it fails with this error:

Cloning into ‘/go/src/github.com/gorilla/websocket’…

  • Use Dokku Environment variables in DockerFile
  • Install nodejs using docker
  • How-to run a node.js docker instance dropping into a shell that is auto-tailing logs
  • Docker for Windows Swarm IIS Service with Win10 Insider running but unreachable
  • Fetching git behind proxy in docker container
  • Docker build dockerfile
  • fatal: unable to look up current user in the passwd file: no such user

    package github.com/gorilla/websocket: exit status 128

    As far as I understand, the issue is, that jenkins starts the container with the userid from the jenkins user, to keep the file system permissions right (docker run -t -d -u 108:113 ….) but how can I use git then?

    pipeline {
    agent none
    stages {
    
    ...
    
        stage('Build Code') {
            agent {
                docker {
                    image 'xxx.de/go_build_container'
                    args '-v=$WORKSPACE:/go/src/bitbucket.org/xxx/service_donation'
                }
            }
            environment {
                HOME = "."
            }
            steps {
                sh 'cd /go/src/bitbucket.org/xxx/service_donation && go get github.com/gorilla/websocket'
    
            }
        }
    

  • Squishing docker image without spinning a container
  • REPOSITORY <none> TAG <none>
  • Accessing Website running in Kubernetes Which is Running on Virtualbox from Host OS
  • Gulp build into Docker container when NODE_ENV=production
  • Creating a container with a variable port
  • docker: installing a node.js application has issues, since docker runs as root
  • 2 Solutions collect form web for “Use git in jenkins pipeline with docker agent”

    Lot of programs won’t work when running with a user id not existing in /etc/passwd and git is one of those programs.

    You must specify a correct -u argument to your docker container, that is, one that exists in your xxx.de/go_build_container image, root (or 0:0) being one of them.
    So put something like: args '-v=$WORKSPACE:/go/src/bitbucket.org/xxx/service_donation -u 0:0' and it will work.

    Then, you will face another problem: the files created in your volume will belong to the UID you are using in your container, you may need to add some chown if you want to reuse those files later in your pipeline.

    Another option I found is mounting /etc/passwd to the container.

            agent {
                docker {
                    image 'xxxx'
                    args '-v=/etc/passwd:/etc/passwd'
                }
            }
    

    I’m not sure, if that has any other problems but it seems to work and you don’t have the problem with wrong permissions.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.