Alpine Image を用いた Docker 横付け(Sibling)
tl;dr
- Host の Docker を使って Docker Container 内で Docker を使う方法
- Docker in Docker とは少し違う
- この場合、Docker Image に Docker を再度 Install する
- Docker Sibling っていうらしい
- Ubuntu だと楽だったが Alpine だと少し詰まった
Environment
- Ubuntu: 16.04
- Docker: 18.09.1
- docker-compose: 1.23.1
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.5 LTS Release: 16.04 Codename: xenial $ docker --version Docker version 18.09.1, build 4c52b90 $ which docker /usr/bin/docker $ docker-compose -v docker-compose version 1.23.1, build b02f1306
Ubuntu Image
- 簡単な方の例として Ubuntu Image を使った Docker Sibling を先にやってみる
- Host の下記のものを mount しなければならない
/var/run/docker.sock
/usr/bin/docker
/var/lib/docker
- apt で以下の library を Install する
libltdl7
docker-compose を使う場合は下記の docker-compose.yml
を使う
FROM ubuntu:xenial RUN apt update && \ apt install -y \ libltdl7 && \ apt clean && \ rm -rf /var/lib/apt/lists/*
version: "3" services: sibling: build: context: . dockerfile: Dockerfile volumes: - /var/run/docker.sock:/var/run/docker.sock - /usr/bin/docker:/usr/bin/docker - /var/lib/docker:/var/lib/docker command: ["tail", "-f", "/dev/null"]
上のファイルが置いてある dir で
$ docker-compose up -d --build Creating network "ubuntu_default" with the default driver Building sibling Step 1/2 : FROM ubuntu:xenial ---> 7e87e2b3bf7a Step 2/2 : RUN apt update && apt install -y libltdl7 && apt clean && rm -rf /var/lib/apt/lists/* ---> Running in ba4a3656aa54 ... Removing intermediate container ba4a3656aa54 ---> 1d55ed6f9167 Successfully built 1d55ed6f9167 Successfully tagged ubuntu_sibling:latest Creating ubuntu_sibling_1_6d8a96c7808b ... done $ docker-compose exec sibling bash root@1c72523ac4ca:/#
Docker container 内で docker command を叩いてみる
root@1c72523ac4ca:/# docker --version Docker version 18.09.1, build 4c52b90 root@1c72523ac4ca:/# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1c72523ac4ca ubuntu:xenial "tail -f /dev/null" About a minute ago Up About a minute ubuntu_sibling_1_c6a532a1bf85 root@1c72523ac4ca:/# docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
動いた。
Alpine Image
- Mount するものは同じ
/var/run/docker.sock
/usr/bin/docker
/var/lib/docker
docker-compose.yml
も同じ- apk で以下の library を Install する
libltdl
libc6-compat
libc6-compat
を install しないで container を起動した場合、以下の症状になる
/ # docker --version sh: docker: not found / # which docker /usr/bin/docker / # echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin / # /usr/bin/docker sh: /usr/bin/docker: not found
libc6-compat
を install した場合は、
FROM alpine RUN apk --update add --no-cache \ libltdl \ libc6-compat
ubuntu@dh236:~/docker/ubuntu$ docker-compose up -d --build Creating network "ubuntu_default" with the default driver Building sibling Step 1/2 : FROM alpine ---> 3f53bb00af94 Step 2/2 : RUN apk --update add --no-cache libc6-compat libltdl ---> Running in bdadee15e55a ... Removing intermediate container bdadee15e55a ---> 74a3b17a7bfa Successfully built 74a3b17a7bfa Successfully tagged ubuntu_sibling:latest Creating ubuntu_sibling_1_5c2a7d4c159a ... done ubuntu@dh236:~/docker/ubuntu$ docker-compose exec sibling sh / #
/ # docker --version Docker version 18.09.1, build 4c52b90 / # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a43a0d067029 ubuntu_sibling "tail -f /dev/null" 45 seconds ago Up 44 seconds ubuntu_sibling_1_fa2513c9a2e7 / # docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
動いた。 ちなみに alpine base の image のサイズは、
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine_sibling latest 74a3b17a7bfa 2 minutes ago 4.47MB
まとめ
libltdl
だけだと変な挙動になったlibc6-compat
が必要だった