DEV Community

Franz Wong
Franz Wong

Posted on • Edited on

3

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

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay