DEV Community 👩‍💻👨‍💻

Franz Wong
Franz Wong

Posted on • Updated 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 | sh; \
    sudo curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose; \
    sudo chmod +x /usr/local/bin/docker-compose

Just build the docker image as usual.

docker build -t franz/test .

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

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

Top comments (0)

Dream Big

Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category.

Join the Hackathon <-