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

  • How to enable theme on WordPress on Docker
  • How to setup redis/sentinel replication/monitoring with docker on separate docker machines?
  • Change Docker backoff time
  • Docker volume created with nfs server is not reflecting the data from nfs server
  • How to mount a directory in docker container to host
  • Failing to execute nginx proxy_pass directive for a Dancer2 app inside a Docker container
  • fatal: unable to look up current user in the passwd file: no such user

    package 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 ''
                    args '-v=$WORKSPACE:/go/src/'
            environment {
                HOME = "."
            steps {
                sh 'cd /go/src/ && go get'

  • How do I run a salt state on a running docker container
  • Deploy a docker stack on one node (co-schedule containers like docker swarm)
  • Logging in Docker
  • Update hosts file in all containers
  • Kubernetes cluster attempts endless DNS lookups, swamping the router. What is wrong and how can I put a stop to it?
  • Installing docker on centos7: docker-engine-selinux conflicts with docker-selinux-*
  • 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 image, root (or 0:0) being one of them.
    So put something like: args '-v=$WORKSPACE:/go/src/ -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.