# How can i run docker command inside a docker container?

**URL:** <https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337>\
**Category:** Open Source Registry API\
**Created:** [November 4, 2014, 12:03pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337 "2014-11-04T12:03:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![laoyumi](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@laoyumi](https://forums.docker.com/u/laoyumi)\
**Post date:** [November 4, 2014, 12:03pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/1 "2014-11-04T12:03:42Z")

</div>

Hi gurus,

I want use docker build/push command to manage docker images (into our private registry) inside a docker container, by which I will gain a lot of flexibilities.

My host os is coreos and the base image is ubuntu. But I found inside the docker container, /usr/bin/docker is not available. I guess it was designed intentionally.

Is my request valid? DO I have a alternative way to achieve the same function?

Regards  
wangyumi

---

<div class="post-metadata">

**Author:** ![usertaken](https://avatars.discourse-cdn.com/v4/letter/u/f04885/32.png) [@usertaken](https://forums.docker.com/u/usertaken)\
**Post date:** [November 4, 2014, 6:42pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/2 "2014-11-04T18:42:30Z")

</div>

I’m not sure about CoreOS but normally you can manage your host containers from within a container by mounting the Docker socket.

Such as

```
docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:latest sh -c "apt-get update ; apt-get install docker.io -y ; bash"

```

or

[https://registry.hub.docker.com/u/abh1nav/dockerui/](https://registry.hub.docker.com/u/abh1nav/dockerui/)

---

<div class="post-metadata">

**Author:** ![laoyumi](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@laoyumi](https://forums.docker.com/u/laoyumi)\
**Post date:** [November 5, 2014, 3:04am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/3 "2014-11-05T03:04:34Z")

</div>

Thanks for your quick response.

Any ideas about how to quick “enable” docker in a docker image by Dockerfile?

Regards,  
wangyumi

---

<div class="post-metadata">

**Author:** ![usertaken](https://avatars.discourse-cdn.com/v4/letter/u/f04885/32.png) [@usertaken](https://forums.docker.com/u/usertaken)\
**Post date:** [November 5, 2014, 4:11am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/4 "2014-11-05T04:11:28Z")

</div>

Within a Dockerfile I think you can only create a data volume. You need to manually specific which host directory or file to mount as a volume when running.

[http://docs.docker.com/userguide/dockervolumes/#mount-a-host-file-as-a-data-volume](http://docs.docker.com/userguide/dockervolumes/#mount-a-host-file-as-a-data-volume)

---

<div class="post-metadata">

**Author:** ![nathanleclaire](https://avatars.discourse-cdn.com/v4/letter/n/ebca7d/32.png) [@nathanleclaire](https://forums.docker.com/u/nathanleclaire)\
**Post date:** [November 5, 2014, 6:02am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/5 "2014-11-05T06:02:59Z")

</div>

> [@laoyumi](#):
>
> But I found inside the docker container, /usr/bin/docker is not available. I guess it was designed intentionally.

Yes, anyone with direct access to the Docker socket has root privileges on the host system. Usually not what you want.

If you’re running on Linux, you don’t have to directly install Docker in the container at all. You can bind mount the docker binary (usually at `/usr/bin/docker`) directly. Note that bind mounting the socket does not give you a totally new Docker, but rather access to the existing Docker daemon from inside the container. If you want to bake in the Docker binary to an image you could always make one called `laoyumi/docker` or something and then to “quickly” get access to it in another image you just start the Dockerfile with `from laoyumi/docker`.

There is [Docker in Docker](https://github.com/jpetazzo/dind) but it’s a little heavyweight if all you want to do is “some docker-ey stuff in containers that doesn’t need to be that isolated”.

---

<div class="post-metadata">

**Author:** ![laoyumi](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@laoyumi](https://forums.docker.com/u/laoyumi)\
**Post date:** [November 5, 2014, 11:17am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/6 "2014-11-05T11:17:09Z")

</div>

Hi,  
I wrote a Dockerfile like:  
…  
RUN apt-get -yqq update  
VOLUME ["/var/run/docker.sock"]  
RUN apt-get -yqq install [docker.io](http://docker.io)

Subsequently, I build the image and run a container and attach it.

When I was trying to build a docker image inside the container, I got following error:  
root@fd8d47323d89:/Dockerimages/sample/2014-11-05 10:59:18.431193458 +0000 UTC# docker build .  
2014/11/05 11:11:05 Cannot connect to the Docker daemon. Is ‘docker -d’ running on this host?

How can I let a docker client inside a docker container connects the docker daemon on the host os?

Regards,  
wangyumi

---

<div class="post-metadata">

**Author:** ![bdruemen](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/bdruemen/32/2331_2.png) [@bdruemen](https://forums.docker.com/u/bdruemen)\
**Post date:** [October 6, 2015, 5:28pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/7 "2015-10-06T17:28:03Z")

</div>

Hi wangyumi, the commands you wrote are working!  
Here is my Dockerfile (the parent is on docker hub, so you can try it out):

FROM bdruemen/jenkins-uid-from-volume  
RUN apt-get -yqq update && apt-get -yqq install [docker.io](http://docker.io) && usermod -g docker jenkins  
VOLUME /var/run/docker.sock  
ENTRYPOINT groupmod -g $(stat -c “%g” /var/run/docker.sock) docker && usermod -u $(stat -c “%u” /var/jenkins\_home) jenkins && gosu jenkins /bin/tini – /usr/local/bin/jenkins.sh

docker build .  
docker run -d -v /var/run/docker.sock:/var/run/docker.sock (IMAGE)  
docker exec -u jenkins (CONTAINER) bash  
jenkins@8da12737527b:/$ docker images  
(shows a list)

---

<div class="post-metadata">

**Author:** ![tracker1](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/tracker1/32/16120_2.png) [@tracker1](https://forums.docker.com/u/tracker1)\
**Post date:** [May 31, 2017, 5:57pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/8 "2017-05-31T17:57:15Z")

</div>

Just create a volume map for both the docker executable, and the docker socket descriptor…

```
docker run -it -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker ubuntu:latest bash

```

In your case, your image, and the command to run are up to you, but mapping those two pieces are enough to interact with the docker executable. 🙂

---

<div class="post-metadata">

**Author:** ![jmcollin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jmcollin](https://forums.docker.com/u/jmcollin)\
**Post date:** [June 1, 2017, 2:16pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/9 "2017-06-01T14:16:03Z")

</div>

Hi Michael,

I try your proposal but without success. Into container I’ve this message:

> root$ docker exec -it Jenkins /bin/bash  
> bash-4.3$ docker ps  
> bash: /usr/bin/docker: No such file or directory  
> bash-4.3$ which docker  
> /usr/bin/docker  
> bash-4.3$

Any idea ? My root docker is on Alpine Linux/ Maybe it have so impacts.

EDIT:  
same problem with unbuntu

> root$ docker run -it -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker ubuntu:latest bash  
> Unable to find image ‘ubuntu:latest’ locally  
> latest: Pulling from library/ubuntu  
> b6f892c0043b: Pull complete  
> 55010f332b04: Pull complete  
> 2955fb827c94: Pull complete  
> 3deef3fcbd30: Pull complete  
> cf9722e506aa: Pull complete  
> Digest: sha256:382452f82a8bbd34443b2c727650af46aced0f94a44463c62a9848133ecb1aa8  
> Status: Downloaded newer image for ubuntu:latest  
> root@57fb8536d5b1:/#  
> root@57fb8536d5b1:/#  
> root@57fb8536d5b1:/# docker ps  
> docker: error while loading shared libraries: libltdl.so.7: cannot open shared object file: No such file or directory  
> root@57fb8536d5b1:/#

JM.

---

<div class="post-metadata">

**Author:** ![vp2405](https://avatars.discourse-cdn.com/v4/letter/v/f4b2a3/32.png) [@vp2405](https://forums.docker.com/u/vp2405)\
**Post date:** [June 8, 2017, 7:32am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/10 "2017-06-08T07:32:09Z")

</div>

same problem for me, with Alpine Linux

---

<div class="post-metadata">

**Author:** ![jmcollin](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jmcollin](https://forums.docker.com/u/jmcollin)\
**Post date:** [June 8, 2017, 6:42pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/11 "2017-06-08T18:42:07Z")

</div>

The solution for me was to chmod /var/run/docker.sock with correct rightd considering that user/group inside container is not the user/group on the host.

---

<div class="post-metadata">

**Author:** ![fangzx](https://avatars.discourse-cdn.com/v4/letter/f/ed8c4c/32.png) [@fangzx](https://forums.docker.com/u/fangzx)\
**Post date:** [June 22, 2017, 1:56pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/12 "2017-06-22T13:56:27Z")

</div>

yes, I do like this:

(1) In Dockerfile , just add  
RUN curl -fsSLO [https://get.docker.com/builds/Linux/x86\_64/docker-17.03.1-ce.tgz](https://get.docker.com/builds/Linux/x86_64/docker-17.03.1-ce.tgz) &&   
tar --strip-components=1 -xvzf docker-17.03.1-ce.tgz -C /usr/local/bin

(2) in docker run command, add  
-v /var/run/docker.sock:/var/run/docker.sock \

(3) in container .bash\_profile file add  
chown -R dev:dev /var/run/docker.sock

dev is the user in container.

---

<div class="post-metadata">

**Author:** ![sinahwz](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sinahwz](https://forums.docker.com/u/sinahwz)\
**Post date:** [January 10, 2019, 1:34pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/13 "2019-01-10T13:34:13Z")

</div>

After searching a lot and trying all possible solutions, your simple comment was the savior. Thanks.

---

<div class="post-metadata">

**Author:** ![vibhutisawant](https://avatars.discourse-cdn.com/v4/letter/v/7bcc69/32.png) [@vibhutisawant](https://forums.docker.com/u/vibhutisawant)\
**Post date:** [January 22, 2019, 7:07am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/14 "2019-01-22T07:07:12Z")

</div>

apt-get install -y libltdl7 solves the above issue.

---

<div class="post-metadata">

**Author:** ![dovidweisz](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/dovidweisz/32/38447_2.png) [@dovidweisz](https://forums.docker.com/u/dovidweisz)\
**Post date:** [March 26, 2019, 5:03pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/15 "2019-03-26T17:03:07Z")

</div>

Can i do this with a Windows host?

---

<div class="post-metadata">

**Author:** ![dovidweisz](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/dovidweisz/32/38447_2.png) [@dovidweisz](https://forums.docker.com/u/dovidweisz)\
**Post date:** [March 26, 2019, 6:16pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/16 "2019-03-26T18:16:46Z")

</div>

turns out you can

> <https://stackoverflow.com/questions/36765138/bind-to-docker-socket-on-windows/41005007#41005007>

---

<div class="post-metadata">

**Author:** ![nestybox](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/nestybox/32/25044_2.png) [@nestybox](https://forums.docker.com/u/nestybox)\
**Post date:** [September 18, 2019, 9:54pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/17 "2019-09-18T21:54:47Z")

</div>

There are two well known ways of launching Docker containers from inside a Docker container: Docker-in-Docker (DinD) and Docker-out-of-Docker (DooD).

DinD runs the Docker daemon inside a Docker container. This means that child containers are created inside the parent container. Docker has an official image for it in Docker Hub (search for “dind”). It’s easy to setup but has a caveat: the outer container must be a privileged container, which means it’s not secure. Depending on your security requirements it may not be a viable solution.

DooD is the solution where you run the Docker CLI inside a container, and connect it to the host’s Docker by virtue of mount the /var/run/docker.sock into the container. It’s easy to setup too, but has some drawbacks that stem from the fact that the container is launched from a different context that where it actually runs (i.e., it’s launched from within a parent container, but runs as a sibling of that parent container). Again, depending on your scenario those drawbacks may void use of this solution.

I wrote a blog on DinD vs DooD [here](https://blog.nestybox.com/2019/09/13/dind.html).

I am the founder of [Nestybox](https://www.nestybox.com), and we have developed a solution that runs Docker-in-Docker without using privileged containers, with total isolation between the Docker in the container and the Docker on the host. The solution is in an experimental stage, and we are looking for early adopters. In fact, our goal is to enable Docker containers to run any workloads (apps or system-level workloads such as Docker), much like a VM does.

---

<div class="post-metadata">

**Author:** ![nestybox](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/nestybox/32/25044_2.png) [@nestybox](https://forums.docker.com/u/nestybox)\
**Post date:** [November 13, 2019, 10:47pm UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/18 "2019-11-13T22:47:08Z")

</div>

Messed up the link to the DinD vs DooD post: it’s [here](https://blog.nestybox.com/2019/09/14/dind.html)

---

<div class="post-metadata">

**Author:** ![vietnguyen1254](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@vietnguyen1254](https://forums.docker.com/u/vietnguyen1254)\
**Post date:** [May 6, 2020, 8:06am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/19 "2020-05-06T08:06:58Z")

</div>

may i know what is the right or chmod number you set? thank you in advance.

---

<div class="post-metadata">

**Author:** ![nitinroxx](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/nitinroxx/32/37688_2.png) [@nitinroxx](https://forums.docker.com/u/nitinroxx)\
**Post date:** [November 29, 2022, 4:51am UTC](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337/20 "2022-11-29T04:51:57Z")

</div>

I was looking for `container IP` with `docker inspect` command,  
I am running containers with `docker-compose`. I had mounted the `/var/run/docker.sock` in docker-compose’s volumes section, after I was able run docker command with python scripts.

[Next page](https://forums.docker.com/t/how-can-i-run-docker-command-inside-a-docker-container/337.md?page=2)
