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’…

  • How do we start the daemon?
  • docker build is not work
  • Docker: group of a shared volume changes depending on host user
  • Docker with mysql: The error means mysqld does not have the access rights to the directory
  • Bash run docker command only if user has privileges (Bash if or statement)
  • I can not remove a directory in a docker container
  • 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'
    
            }
        }
    

  • How to list images and their containers
  • Docker Apache 403
  • Jenkins Docker Container can't access docker.sock
  • Schema Registry container: Server died unexpectedly when launching using docker-compose
  • Production vs Development Docker setup for Node (Express & Mongo) App
  • Load balancing of network requests between replicas of services in docker swarm mode
  • 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.