DEV Community

Franz Wong
Franz Wong

Posted on • Edited on

Access host's docker from container

We are going to access host's docker from container.

Container requires docker to be installed. (You can also install docker-compose if you need)

FROM centos:7

RUN set -eux; \
    yum install -y sudo;

# Install docker and docker-compose
RUN set -eux; \
    sudo curl -sSL https://get.docker.com/ | sh; \
    sudo curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose; \
    sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Just build the docker image as usual.

docker build -t franz/test .
Enter fullscreen mode Exit fullscreen mode

We mount docker socket and pass privileged flag to get permission to run.

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock --privileged --name test franz/test /bin/bash
Enter fullscreen mode Exit fullscreen mode

You can try docker ps to see if it is using the docker in the host machine.

[root@5ef32b67b635 /]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5ef32b67b635        franz/test          "/bin/bash"         3 seconds ago       Up 2 seconds                            test
Enter fullscreen mode Exit fullscreen mode

Top comments (0)