Help us understand the problem. What is going on with this article?

Dockerコンテナの定義、実行の自動化

More than 3 years have passed since last update.

docker-composeを使うと、複数コンテナの構成をコードで定義でき
実行することができます。

docker-swarmと合わせて使う事で、
複数ホスト間でのスケールイン/スケールアウトが簡単になります。

1. 実行環境

  • OS: Max OSX

2. 必要ソフトウェア

  • docker
  • docker-compose
  • docker-machine
  • docker-swarm
  • virtualbox

全てbrewからインストールできます。

$ brew search docker                             
boot2docker           docker-machine (installed)        homebrew/versions/boot2docker141  homebrew/versions/docker141   homebrew/versions/docker171
docker (installed)        docker-swarm (installed)      homebrew/versions/boot2docker162  homebrew/versions/docker150
docker-compose (installed)    homebrew/versions/boot2docker133  homebrew/versions/docker133       homebrew/versions/docker162
homebrew/completions/boot2docker-completion    homebrew/emacs/dockerfile-mode             Caskroom/cask/docker-compose           Caskroom/cask/docker
homebrew/completions/docker-completion         Caskroom/cask/boot2docker-status           Caskroom/cask/docker-machine-driver-parallels  Caskroom/cask/dockertoolbox
homebrew/completions/docker-machine-completion     Caskroom/cask/boot2docker                  Caskroom/cask/docker-machine

$ brew cask search virtualbox 
==> Exact match
virtualbox
==> Partial matches
virtualbox-extension-pack

3. Swarmでコンテナのクラスタリング

Dockerコンテナをクラスタリングするを参照下さい。

4. docker-compose.ymlを作成

docker-compose.ymlでコンテナの構成を定義します。

  • docker-compose.yml
docker-compose.yml
web:
  image: tutum/hello-world
  ports:
    - 80:80
redis:
  image: redis:latest

5. コンテナを起動する

以下のコマンドで、docker-compose.yml通りにコンテナが構築されます。

$ docker-compose up -d

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                       NAMES
ac93efeba148        redis:latest        "/entrypoint.sh redis"   7 minutes ago       Up 6 minutes        6379/tcp                    cl-node2/robotconnect_redis_1
29faddfd38f1        tutum/hello-world   "/bin/sh -c 'php-fpm "   8 minutes ago       Up 6 minutes        192.168.99.108:80->80/tcp   cl-node1/robotconnect_web_1

6. スケールアウト/スケールイン

scaleコマンドで、イメージのコンテナ数を自由に変更できます。

  • webサーバーを3つ起動する
$ docker-compose scale web=3
Creating and starting 2... done
Creating and starting 3... done

$ docker ps                 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                       NAMES
3690c5a756a3        tutum/hello-world   "/bin/sh -c 'php-fpm "   35 seconds ago      Up 34 seconds       192.168.99.109:80->80/tcp   cl-node2/robotconnect_web_3
bec52a263c98        tutum/hello-world   "/bin/sh -c 'php-fpm "   35 seconds ago      Up 34 seconds       192.168.99.107:80->80/tcp   cl-manager/robotconnect_web_2
ac93efeba148        redis:latest        "/entrypoint.sh redis"   8 minutes ago       Up 7 minutes        6379/tcp                    cl-node2/robotconnect_redis_1
29faddfd38f1        tutum/hello-world   "/bin/sh -c 'php-fpm "   8 minutes ago       Up 7 minutes        192.168.99.108:80->80/tcp   cl-node1/robotconnect_web_1
  • webサーバーを1つにする
$ docker-compose scale web=1
Stopping robotconnect_web_2... done
Stopping robotconnect_web_3... done
Removing robotconnect_web_3... done
Removing robotconnect_web_2... done

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                       NAMES
ac93efeba148        redis:latest        "/entrypoint.sh redis"   8 minutes ago       Up 7 minutes        6379/tcp                    cl-node2/robotconnect_redis_1
29faddfd38f1        tutum/hello-world   "/bin/sh -c 'php-fpm "   9 minutes ago       Up 7 minutes        192.168.99.108:80->80/tcp   cl-node1/robotconnect_web_1
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away