Docker in docker not running when using official docker dind image

I am trying to created my own docker image.
In my docker file I used official docker dind image
FROM docker:20.10.7-dind

I install everything else I need inside Dockerfile then create container using that dockerfile and start it.
I shell into it using docker exec -it however when I try to run docker ps or any other command I receive the following error:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Should docker already be started if I used dind image? Or I am not understanding this properly.
My CMD to start container is
CMD ["/usr/sbin/sshd", “-D”, “-e”]
because I also want to start ssh server.

How can I start both ssh server and docker so that docker commands can be used inside container?

Thank you.

Is it ssafe to assume that the dind container is started with --privliged flag?

It is. I run it with the following command:
docker run --privileged dind:latest

Did you check the output of ps (literaly, not docker ps) inside the container to check if dockerd is actualy running?

my guess: you override the ENTRYPOINT instruction or your CMD instruction causes the entrypoint script to skip the start of the docker engine inside the container.

No it is not running.
ps command outputs:

PID USER TIME COMMAND
1 root 0:00 sshd: /usr/sbin/sshd -D -e [listener] 0 of 10-100 startups
7 root 0:00 bin/bash
36 root 0:00 ps

I also use entryopoint, that is correct.
And if that is true for my entrypoint or CMD, any idea on what to do in order to resolve it?

Remove your CMD instruction and verify if my assumption is true. Then you know the problem is within your CMD instruction… I neither use a dind container, nor would I ever (repeat in an endless loop!) run an sshd inside a container.

@crodock were you able to figure out the issue ? I’m facing similar issue

Hey there, when using docker:cli image you have to bind your host docker.sock to container docker.sock like this

docker run -d -p 22:22 -v /var/run/docker.sock:/var/run/docker.sock my-ssh-image

This option maps the Docker socket file on the host (/var/run/docker.sock ) to the same location inside the container (/var/run/docker.sock ).

@crodock, I faced the same problem. This is how I resolved it. In short, I place --init when starting container and I use custom script as the entrypoint.
Dockerfile:

FROM docker:dind
CMD /dump/startup.sh

Create a script file /share/my-folder-with-script/startup.sh that will run sshd and original dockerd-entrypoint.sh:

#!/bin/sh
/usr/sbin/sshd
dockerd-entrypoint.sh

Build image:
docker build -t dockerdev .

Run container:
docker run --init --privileged --name test-container -d -v /share/my-folder-with-script:/dump:ro dockerdev

Inside container you can see the processes:

/ # ps -ef
PID   USER     TIME  COMMAND
    1 root      0:00 {startup.sh} /bin/sh /dump/startup.sh
    7 root      0:00 docker-init -- dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376 --tlsverify --tlscacert /certs/server/ca.pem --tlscert /certs/server/cert.pem --tlskey /cer
   66 root      0:00 dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376 --tlsverify --tlscacert /certs/server/ca.pem --tlscert /certs/server/cert.pem --tlskey /certs/server/key.p
   78 root      0:00 containerd --config /var/run/docker/containerd/containerd.toml
  240 root      0:00 sh
  246 root      0:00 ps -ef