docker tool in jenkins container (with mounted docker socket) is not finding docker daemon to connect to

I just started a jenkins docker container with a mounted docker socket like the following:

docker run -d \
    --publish 8080:8080 \
    --publish 50000:50000 \
    --volume /my_jenkins_home:/var/jenkins_home \
    --volume /var/run/docker.sock:/var/run/docker.sock \
    --restart unless-stopped \
    --name my_jenkins_container \
    company/my_jenkins:latest

Then I bash into the container like this:

  • How to force a docker push on mac osx
  • Docker compose port mapping
  • Amazon EFS access denied while mounting in docker container
  • `docker-machine scp` from local directory to machine
  • Docker Container Version Control
  • Inconsistency in running docker-build on different environment
  • docker exec -it my_jenkins_container bash
    

    A tool 'docker' command in a jenkins pipeline script has automatically installed a docker binary at the following path: /var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker/bin/docker

    However, when i try to run docker commands from that docker binary (assuming that it will connect with the docker socket that has been mounted at /var/run/docker.sock) it returns the following error:

    $ /var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker/bin/docker images
    Cannot connect to the Docker daemon. Is the docker daemon running on this host?
    

    How can i ensure that this docker binary (the binary that has been automatically installed via jenkins’ tool 'docker' command) runs its docker commands by connecting to the mounted docker socket at /var/run/docker.sock?

  • Is it good practice to have my sites cert.key and cert.pem in a Nginx Docker container?
  • How to access container's web application from host
  • Docker Swarm with Zookeeper - No elected primary cluster manager
  • “The output of `ssh-add -l` doesn't contain 'RSA'. Start the agent, add your keys?” in Docker
  • wercker.com connect to MySQL database
  • Host Volumes Not getting mounted on 'Docker-compose up'
  • One Solution collect form web for “docker tool in jenkins container (with mounted docker socket) is not finding docker daemon to connect to”

    Short Answer:

    The file permissions of the mounted docker socket file had to be revised


    Long Answer:

    When i simply tried to execute /path/to/dockerTool/bin/docker ps -a on the docker container, it was producing an error.

    $ docker exec -it my_jenkins_container bash -c "/var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker/bin/docker ps -a"
    Cannot connect to the Docker daemon. Is the docker daemon running on this host?
    

    Then, when i tried to execute /path/to/dockerTool/bin/docker ps -a with user=root, it worked fine.

    $ docker exec -it --user=root my_jenkins_container bash -c "/var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker/bin/docker ps -a"
    CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                                              NAMES
    c9dd56411efe        company/my_jenkins:latest   "/bin/tini -- /usr/lo"   49 seconds ago      Up 49 seconds       0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   my_jenkins_container
    

    So it means i just needed to set the right permissions to the docker socket. All i had to do was chgrp the socket file to jenkins group so that the jenkins group/users can read/write to that socket file. (the before & after of the chgrp command is included here):

    $ docker exec -it my_jenkins_container bash -c "ls -l /var/run/docker.sock"
    srw-rw---- 1 root 999     0 Jan 15 08:29 /var/run/docker.sock
    
    $ docker exec -it --user=root my_jenkins_container bash -c "chgrp jenkins /var/run/docker.sock"
    
    $ docker exec -it my_jenkins_container bash -c "ls -l /var/run/docker.sock"
    srw-rw---- 1 root jenkins 0 Jan 15 08:29 /var/run/docker.sock
    

    After that, executing /path/to/dockerTool/bin/docker ps -a as a non-root user worked fine

    $ docker exec -it my_jenkins_container bash -c "/var/jenkins_home/tools/org.jenkinsci.plugins.docker.commons.tools.DockerTool/docker/bin/docker ps -a"
    CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                                              NAMES
    c9dd56411efe        company/my_jenkins:latest   "/bin/tini -- /usr/lo"   3 minutes ago      Up 3 minutes       0.0.0.0:8080->8080/tcp, 0.0.0.0:50000->50000/tcp   my_jenkins_container
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.