Docker client installed in a docker container can't access docker host

I’m running a DooD (Docker outside of Docker) scenario on Docker for Mac.
This means I have a Docker container (specifically a Jenkins agent/slave) with installed Docker engine inside the container.

The container is started with docker-compose with the compose setup below:

agent:
  image: softwarecraftsmen/jenkins-agent
  restart: always
  environment:
    - JENKINS_AGENT_PASSWORD
  ports:
    - "22:22"
  volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    # http://stackoverflow.com/questions/26021181/not-enough-entropy-to-support-dev-random-in-docker-containers-running-in-boot2d
    - /dev/urandom:/dev/random

Running a bash inside the agent container using docker exec -ti jenkins_agent_1 bash -c "docker ps" is giving a list of running containers on the host (my mac). The command is successfully executed as root as the docker host’s socket is mapped to the container using a volume.

I have a user jenkins in this container that was added to the groups jenkins, docker, and even root. The groups command is confirming that.

root@aa926508b08a:/# groups jenkins                                                                                                                                                                                      
jenkins : jenkins root docker

However when becoming user jenkins the docker ps command is failing:

jenkins@aa926508b08a:/$  docker ps -a    
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

As I have followed the suggested setup I have to assume that the problem is somehow related to mapping the host’s socket to the container. Though I may be wrong and something essential is missing.

My docker version inside the client is:

docker -v 
Docker version 1.12.1

My docker host (mac) is

docker -v
Docker version 1.12.1, build 6f9534c, experimental

Hey, I managed to get DooD working with Docker for Mac Version 1.12.3 by doing the following:

Dockerfile:

FROM jenkins
USER root
RUN apt-get update
# install necessary packages to run docker
RUN apt-get install docker.io -y
RUN apt-get install sudo -y

# add jenkins user to sudoers, no password
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers

USER jenkins

Building it:

docker build -t jenkins-docker .

Running it:

$ docker run -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock jenkins-docker

Now you can make a build step, that executes something like sudo docker build . -t my-image

You might useful this step by step tutorial I did for Jenkins and docker pipeline