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 link internal port to outside port in docker?
  • How can I use Docker to run my all selenium test cases on all instance of my application using a single machine?
  • Dockerfile vs docker run
  • docker - NGINX Proxy + Let's encrypt
  • Mesos killing tasks. Failed to determine cgroup for the 'cpu' subsystem
  • Docker port bindings
  • 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?

  • my own customizations of boot2docker are not reflected into the iso image
  • How to connect docker containers in separate deployments on the same host machine using docker-compose?
  • What docker commands does rancher run when I perform an upgrade
  • How to mount a device of host to host in a Docker container?
  • Resources required for running application on server VS running application in LXC container?
  • Zookeeper my ID not in the peer list
  • 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.