ICTについて使える情報を発信していきます. 遠回りしたり, 自分のペースで...

このブログを検索

dockerでDocker Composeができるようになっていたお話

コンカイハ, Docker ComposeをインストールすることなくDocker Composeを使う話をします. なんのこっちゃと思われそうですがw

あらかじめ, Dockerはインストールしていてください.

環境

docker/composeイメージを使う

docker/composeというdockerイメージがDocker Hubにあります.
それを使うことにより, dockerコマンドのみで, Docker Composeが使えるようになります.

実際に使ってみましょう.

$ docker run --rm \
-v "/var/run/docker.sock:/var/run/docker.sock" \
-v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 \
version

Unable to find image 'docker/compose:alpine-1.25.4' locally
alpine-1.25.4: Pulling from docker/compose
89d9c30c1d48: Pull complete
eccc02a3b135: Pull complete
7382d3c5d78f: Pull complete
f410c750fbcb: Pull complete
Digest: sha256:390bdf3da99a622f5c11f4db386dd97f2858db30cfed07d000b867f1fb25b2f7
Status: Downloaded newer image for docker/compose:alpine-1.25.4
Define and run multi-container applications with Docker.

docker-compose version 1.25.4, build 8d51620
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019

これで簡単にdocker-composeが使えちゃいます. ありがたい.

使っているオプションを説明すると

-v "/var/run/docker.sock:/var/run/docker.sock"

docker deamonの通信に必要.
Ref: https://docs.docker.com/engine/docker-overview/


-v $PWD:$PWD -w $PWD

これがないとdocker-compose.ymlのvolume機能が効かないため, 追加した.
volumeを使わない場合はいらない.

となっています.

docker/composeでhello world

では, docker-compose.ymlを使って実際に動かしてみましょう.

$ cat <<EOF > docker-compose.yml
version:  "3"
services:
  hello-world:
    image: hello-world
EOF
% docker run --rm \
-v "/var/run/docker.sock:/var/run/docker.sock" \
-v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 \
> up
Creating network "hello-world_default" with the default driver
Pulling hello-world (hello-world:)...
latest: Pulling from library/hello-world
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Downloaded newer image for hello-world:latest
Creating hello-world_hello-world_1 ... done
Attaching to hello-world_hello-world_1
hello-world_1  |
hello-world_1  | Hello from Docker!
hello-world_1  | This message shows that your installation appears to be working correctly.
hello-world_1  |
hello-world_1  | To generate this message, Docker took the following steps:
hello-world_1  | 1. The Docker client contacted the Docker daemon.
hello-world_1  | 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
hello-world_1  | (amd64)
hello-world_1  | 3. The Docker daemon created a new container from that image which runs the
hello-world_1  | executable that produces the output you are currently reading.
hello-world_1  | 4. The Docker daemon streamed that output to the Docker client, which sent it
hello-world_1  | to your terminal.
hello-world_1  |
hello-world_1  | To try something more ambitious, you can run an Ubuntu container with:
hello-world_1  | $ docker run -it ubuntu bash
hello-world_1  |
hello-world_1  | Share images, automate workflows, and more with a free Docker ID:
hello-world_1  | https://hub.docker.com/
hello-world_1  |
hello-world_1  | For more examples and ideas, visit:
hello-world_1  | https://docs.docker.com/get-started/
hello-world_1  |
hello-world_hello-world_1 exited with code 0

これによって, docker-compose.ymlが動かせることがわかりました.
実際にはdocker-composeの公式のhello worldを動かしたほうが良いと思いますが, ここは手抜きさせてください…

docker/composeを使うコマンドを関数にする

これで, docker-composeがdockerのみで動かせるようになりましたが, 何しろコマンドが長い. なので, コマンドを関数化しましょう.

docker-compose(){  docker run --rm -v "/var/run/docker.sock:/var/run/docker.sock" -v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 "$@"; }

これを\.*shrcなりにおいておくと, つぎからdocker-composeコマンドがインストールされているかのように使えます.

$ docker-compose(){  docker run --rm -v "/var/run/docker.sock:/var/run/docker.sock" -v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4 "$@"; }
$ docker-compose version
docker-compose version 1.25.4, build 8d51620
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019
$ docker-compose up -d
Starting hello-world_hello-world_1 ... done

もしくはaliasを使うという手もあります.

alias docker-compose='docker run --rm -v "/var/run/docker.sock:/var/run/docker.sock" -v $PWD:$PWD -w $PWD docker/compose:alpine-1.25.4'

まとめ

ということで, Docker ComposeをインストールせずにDocker Composeを使う方法を紹介しました. 言われてみれば思うのですが, DockerあるのにDocker Composeをインストールするのはおかしいですよね…
あんまり目立った欠点もなさそうですし, 普段使いはこれでDocker Composeを使おうと思います.

Read More

Popular Posts