Search code examples
dockerdockerfilealpine-linuxdocker-in-docker

Install Docker in Alpine Docker


I have a Dockerfile with a classic Ubuntu base image and I'm trying to reduce the size. That's why I'm using Alpine base.

In my Dockerfile, I have to install Docker, so Docker in Docker.

FROM alpine:3.9 

RUN apk add --update --no-cache docker

This works well, I can run docker version inside my container, at least for the client. Because for the server I have the classic Docker error saying :

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I know in Ubuntu after installing Docker I have to run

usermod -a -G docker $USER

But what about in Alpine ? How can I avoid this error ?

PS:

My first idea was to re-use the Docker socket by bind-mounting /var/run/docker.sock:/var/run/docker.sock for example and thus reduce the size of my image even more, since I don't have to reinstall Docker.

But as bind-mount is not allowed in Dockerfile, do you know if my idea is possible and how to do it ? I know it's possible in Docker-compose but I have to use Dockerfile only.

Thanks


Solution

  • You can do that, and your first idea was correct: just need to expose the docker socket (/var/run/docker.sock) to the "controlling" container. Do that like this:

    host:~$ docker run \
                      -v /var/run/docker.sock:/var/run/docker.sock \  
                      <my_image>
    host:~$ docker exec -u root -it <container id> /bin/sh
    

    Now the container should have access to the socket (I am assuming here that you have already installed the necessary docker packages inside the container):

    root@guest:/# docker ps -a
    
    CONTAINER ID        IMAGE                 COMMAND                  CREATED       ...
    69340bc13bb2        my_image              "/sbin/tini -- /usr/…"   8 minutes ago ...
    

    Whether this is a good idea or not is debatable. I would suggest not doing this if there is any way to avoid it. It's a security hole that essentially throws out the window some of the main benefits of using containers: isolation and control over privilege escalation.