30

How can I install Docker inside an alpine container and run docker images? I could install, but could not start docker and while running get "docker command not found error".

030
  • 10,842
  • 12
  • 78
  • 123
Subit Das
  • 509
  • 3
  • 6
  • 14
  • 2
    Possible duplicate of [Is it ok to run docker from inside docker?](https://stackoverflow.com/questions/27879713/is-it-ok-to-run-docker-from-inside-docker) – David Maze Jan 08 '19 at 21:52
  • It's almost universally discouraged and technically complicated, but theoretically possible. The best option is to use a higher-level orchestrator like Docker Compose, and not have containers try to launch other containers at all. If you can tolerate your containers having unrestricted root access over the host you can also give them access to the host's Docker socket. – David Maze Jan 08 '19 at 21:54

2 Answers2

50

Dockerfile for running docker-cli inside alpine

FROM alpine:3.10
RUN apk add --update docker openrc
RUN rc-update add docker boot

Build docker image

docker build -t docker-alpine .

Run container (host and the alipne container will share the same docker engine)

docker run -it -v "/var/run/docker.sock:/var/run/docker.sock:rw" docker-alpine:latest /bin/sh
sai anudeep
  • 1,135
  • 13
  • 26
  • 1
    Is there any way I can move the mounting the volume part to the Dockerfile. so as soon as I start an interactive terminal session docker works just fine?? – mygitrepo Jan 04 '21 at 20:05
  • @mygitrepo No, bind mounts are runtime options. Your only chance would be to enable TCP on the docker daemon (or make a reverse proxy to the unix socket) and use the `-H` flag (which you could set with `ENTRYPOINT`) – Robin Thoni Feb 20 '21 at 21:05
30

All you need is to install Docker CLI in an image based on Alpine and run the container mounting docker.sock. It allows running sibling Docker containers using host's Docker Engine. It is known as Docker-out-of-Docker and is considered a good alternative to running a separate Docker Engine inside a container (aka Docker-in-Docker).

Dockerfile

FROM alpine:3.11

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

Build the image:

docker build -t alpine-docker .

Run the container mounting the docker.sock (-v /var/run/docker.sock:/var/run/docker.sock):

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock alpine-docker docker ps

The command above should successfully run docker ps inside the Alpine-based container.

Eugene Khyst
  • 9,236
  • 7
  • 38
  • 65
  • Is there any way I can move the mounting the volume part to the Dockerfile. so as soon as I start an interactive terminal session docker works just fine?? – mygitrepo Jan 04 '21 at 20:03
  • @mygitrepo - you can use docker-compose to do something similar. You can define the mount there, and then just use `docker-compose run --rm alpine-docker-svc` The docker-compose file would look like (sorry, no newlines in SO comments): ` version: '3' services: alpine-docker-svc: build: . volumes: - /var/run/docker.sock:/var/run/docker.sock ` – saraf.gahl May 30 '21 at 14:05