Docker Compose ã¨ã¯
Docker Compose ã¨ã¯è¤æ°ã®ã³ã³ãããå©ç¨ãã¦ä¸ã¤ã®ãµã¼ãã¹ãæä¾ããå ´åã« YAML ä¸çºã§æ§ç¯åºæ¥ãããã«ãããã¼ã«ï¼ã¨ããèªèï¼ã§ã以åã¯åæ§ãªãã¼ã«ã§ fig ã¨ãããã¼ã«ããã£ãã¨è¨æ¶ãã¦ããããã® fig ããã®ã¾ã¾ Docker Compose ã«ååãå¤ããããã ã
Fig has been replaced by Docker Compose, and is now deprecated. The new documentation is on the Docker website.
ãªãã»ã©ããªãã»ã©ã
æºå
Docker Compose ã®ã¤ã³ã¹ãã¼ã«
ç¾ç¶ã¯ Docker Compose èªä½ã®ã¤ã³ã¹ãã¼ã©ã¼ã¯åå¨ããã以ä¸ã®ããã«ã¤ã³ã¹ãã¼ã«ãããããªãããã ã
curl -L https://github.com/docker/compose/releases/download/1.3.2/docker-compose-`uname -s`-`uname -m` > docker-compose sudo cp docker-compose /usr/local/bin/ sudo chmod +x /usr/local/bin/docker-compose
ãã«ã
$ docker-compose --help Define and run multi-container applications with Docker. Usage: docker-compose [options] [COMMAND] [ARGS...] docker-compose -h|--help Options: -f, --file FILE Specify an alternate compose file (default: docker-compose.yml) -p, --project-name NAME Specify an alternate project name (default: directory name) --verbose Show more output -v, --version Print version and exit Commands: build Build or rebuild services help Get help on a command kill Kill containers logs View output from containers port Print the public port for a port binding ps List containers pull Pulls service images restart Restart services rm Remove stopped containers run Run a one-off command scale Set number of containers for a service start Start services stop Stop services up Create and start containers migrate-to-labels Recreate containers to add labels
ãã¥ã¼ããªã¢ã«
ãµã³ãã«å種
ã¨ãããã rails ã
ãã¡ããåçµããã
ã¾ã㯠Dockerfile ãä½ãã
FROM ruby:2.2.0 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile RUN bundle install ADD . /myapp
Gemfile ãä½ãã
source 'https://rubygems.org' gem 'rails', '4.2.0'
docker-compose.yml ãä½ãã
db: image: postgres ports: - "5432" web: build: . command: bundle exec rails s -p 3000 -b '0.0.0.0' volumes: - .:/myapp ports: - "3000:3000" links: - db
ä¸æ¦ãrails new
ããã
$ docker-compose run web rails new . --force --database=postgresql --skip-bundle
docker-compose.yml ã«ã¯ bundle exec rails s -p 3000 -b '0.0.0.0'
ã¨æå®ããã¦ããã®ã§ãrails new . --force --database=postgresql --skip-bundle
ã§ä¸æ¸ãããã¤ã¡ã¼ã¸ã§å¦çãçµäºããã¨ã«ã¬ã³ããã£ã¬ã¯ããªã«ä»¥ä¸ã®ããã«ãã¡ã¤ã«ãå±éããã¦ããã
$ ls -l total 68 -rw-rw-r-- 1 vagrant vagrant 178 Jul 18 12:20 Dockerfile -rw-rw-r-- 1 vagrant vagrant 1480 Jul 18 12:21 Gemfile -rw-r--r-- 1 root root 3819 Jul 18 12:25 Gemfile.lock -rw-r--r-- 1 root root 478 Jul 18 12:26 README.rdoc -rw-r--r-- 1 root root 249 Jul 18 12:26 Rakefile drwxr-xr-x 8 root root 4096 Jul 18 12:26 app drwxr-xr-x 2 root root 4096 Jul 18 12:26 bin drwxr-xr-x 5 root root 4096 Jul 18 12:26 config -rw-r--r-- 1 root root 153 Jul 18 12:26 config.ru drwxr-xr-x 2 root root 4096 Jul 18 12:26 db -rw-rw-r-- 1 vagrant vagrant 183 Jul 18 12:21 docker-compose.yml drwxr-xr-x 4 root root 4096 Jul 18 12:26 lib drwxr-xr-x 2 root root 4096 Jul 18 12:26 log drwxr-xr-x 2 root root 4096 Jul 18 12:26 public drwxr-xr-x 8 root root 4096 Jul 18 12:26 test drwxr-xr-x 3 root root 4096 Jul 18 12:26 tmp drwxr-xr-x 3 root root 4096 Jul 18 12:26 vendor
ã¡ãã£ã¨ Gemfile ãä¿®æ£ã
gem 'therubyracer', platforms: :ruby
JavaScript ã® Runtime ã§ãã therubyracer ãã¤ã³ã¹ãã¼ã«ããããã«ã¢ã³ã³ã¡ã³ããã¦æå¾ã« docker-compose build
ãå®è¡ããã
$ docker-compose build
以ä¸ã®ããã«åºåãããã
db uses an image, skipping Building web... Step 0 : FROM ruby:2.2.0 ---> 51473a2975de Step 1 : RUN apt-get update -qq && apt-get install -y build-essential libpq-dev ---> Using cache ---> 76dbd0c53ce1 Step 2 : RUN mkdir /myapp ---> Using cache ---> c263028a071e Step 3 : WORKDIR /myapp ---> Using cache ---> 3172fc182c6c Step 4 : ADD Gemfile /myapp/Gemfile ---> 6b9bcc9f7ccb Removing intermediate container a5e6dcd92778 Step 5 : RUN bundle install ---> Running in 1482b35b5d74 Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine. Fetching gem metadata from https://rubygems.org/....... ï¼ç¥ï¼ Installing uglifier 2.7.1 Installing web-console 2.2.1 Your bundle is complete! It was installed into /usr/local/bundle ---> f30b4667c9f9 Removing intermediate container 1482b35b5d74 Step 6 : ADD . /myapp ---> 396b0def9cdf Removing intermediate container e711c5b5248e Successfully built 396b0def9cdf
config/database.yml ã以ä¸ã®ããã«ä¿®æ£ã
development: &default adapter: postgresql encoding: unicode database: postgres pool: 5 username: postgres password: host: db test: <<: *default database: myapp_test
æå¾ã«ã³ã³ãããèµ·åããã
$ docker-compose up -d
-d
ã§ãã¿ããã¢ã¼ãã以ä¸ã®ããã«åºåãããã
$ docker-compose up -d Creating composerails_db_1... Creating composerails_web_1...
docker logs
ã§ç¢ºèªã
$ docker logs composerails_web_1 => Booting WEBrick => Rails 4.2.0 application starting in development on http://0.0.0.0:3000 => Run `rails server -h` for more startup options => Ctrl-C to shutdown server [2015-07-19 07:06:16] INFO WEBrick 1.3.1 [2015-07-19 07:06:16] INFO ruby 2.2.0 (2014-12-25) [x86_64-linux] [2015-07-19 07:06:16] INFO WEBrick::HTTPServer#start: pid=1 port=3000
ãã©ã¦ã¶ã§ç¢ºèªã
ããã
å°ãã²ãã
Sinatra + Redis ã§ãªãã¡ãã£ã¦ API ã·ã¹ãã
ãã¡ããåèããã¦é ãã¦ããã§ã¼ç°¡å API ã·ã¹ãã ãä½ã£ã¦ã¿ããã¨æãã
Dockerfile ãä½æãã¨ãããå ã»ã©ã® Rails ã®ãã®ããã®ã¾ã¾ã
FROM ruby:2.2.0 RUN apt-get update -qq && apt-get install -y build-essential RUN mkdir /myapp WORKDIR /myapp ADD Gemfile /myapp/Gemfile RUN bundle install ADD . /myapp
docker-compose.yml ã以ä¸ã®ããã«ã
redis: image: redis ports: - "6379" web: build: . command: bundle exec ruby app.rb -o 0.0.0.0 volumes: - .:/myapp ports: - "14567:4567" links: - redis environment: - REDIS_HOST=redis
ã¢ããªã±ã¼ã·ã§ã³ï¼app.rbï¼ã¯ä»¥ä¸ã®ãããªç°¡åã¹ã¯ãªããã
#!/usr/bin/env ruby # require 'sinatra' require 'redis' require 'json' r = Redis.new host: ENV["REDIS_HOST"], port:"6379" get '/value/:key' do value = r.get params[:key] v = { key: params[:key], value: value } v.to_json end post '/data/:key' do msg = request.body.read r.set params[:key], msg end
Gemfile ã¯ä»¥ä¸ã®ããã«ç¨æã
source 'https://rubygems.org' gem "sinatra" gem "redis"
docker-compose build
ã§ãã«ãã
$ docker-compose build
以ä¸ã®ããã«åºåãããã
Building web... Step 0 : FROM ruby:2.2.0 ---> 51473a2975de Step 1 : RUN apt-get update -qq && apt-get install -y build-essential ---> Running in 8c1bc72000c3 Reading package lists... Building dependency tree... Reading state information... build-essential is already the newest version. The following package was automatically installed and is no longer required: libbison-dev Use 'apt-get autoremove' to remove it. 0 upgraded, 0 newly installed, 0 to remove and 160 not upgraded. ---> 2ebb189c25b1 Removing intermediate container 8c1bc72000c3 Step 2 : RUN mkdir /myapp ---> Running in 3ae7afaa1941 ---> f528a03f229b Removing intermediate container 3ae7afaa1941 Step 3 : WORKDIR /myapp ---> Running in 114d4e12d4a6 ---> 0cd25195c3ab Removing intermediate container 114d4e12d4a6 Step 4 : ADD Gemfile /myapp/Gemfile ---> 33430f3a8848 Removing intermediate container c9333ca6b80b Step 5 : RUN bundle install ---> Running in b3fac0dae84d Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine. Fetching gem metadata from https://rubygems.org/.......... ï¼ç¥ï¼ Your bundle is complete! It was installed into /usr/local/bundle ---> fd7d36d14b1e Removing intermediate container b3fac0dae84d Step 6 : ADD . /myapp ---> 5ab105adefab Removing intermediate container 2b7359c7add1 Successfully built 5ab105adefab
docker-compose up -d
ã§ã³ã³ãããèµ·åã
$ docker-compose up -d Creating composesinatra_redis_1... Creating composesinatra_web_1...
åã³ã³ãããèµ·åãã¦ãããã¨ã確èªã
$ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------- composesinatra_redis_1 /entrypoint.sh redis-server Up 0.0.0.0:32845->6379/tcp composesinatra_web_1 bundle exec ruby app.rb -o ... Up 0.0.0.0:14567->4567/tcp
docker-compose logs
ã§ç¢ºèªã
$ docker-compose logs web Attaching to composesinatra_web_1 web_1 | [2015-07-19 07:29:10] INFO WEBrick 1.3.1 web_1 | [2015-07-19 07:29:10] INFO ruby 2.2.0 (2014-12-25) [x86_64-linux] web_1 | == Sinatra (v1.4.6) has taken the stage on 4567 for development with backup from WEBrick web_1 | [2015-07-19 07:29:10] INFO WEBrick::HTTPServer#start: pid=1 port=4567 $ docker-compose logs redis Attaching to composesinatra_redis_1 redis_1 | 1:C 19 Jul 07:29:09.431 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf redis_1 | _._ redis_1 | _.-``__ ''-._ redis_1 | _.-`` `. `_. ''-._ Redis 3.0.3 (00000000/0) 64 bit redis_1 | .-`` .-```. ```\/ _.,_ ''-._ redis_1 | ( ' , .-` | `, ) Running in standalone mode redis_1 | |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 redis_1 | | `-._ `._ / _.-' | PID: 1 redis_1 | `-._ `-._ `-./ _.-' _.-' redis_1 | |`-._`-._ `-.__.-' _.-'_.-'| redis_1 | | `-._`-._ _.-'_.-' | http://redis.io redis_1 | `-._ `-._`-.__.-'_.-' _.-' redis_1 | |`-._`-._ `-.__.-' _.-'_.-'| redis_1 | | `-._`-._ _.-'_.-' | redis_1 | `-._ `-._`-.__.-'_.-' _.-' redis_1 | `-._ `-.__.-' _.-' redis_1 | `-._ _.-' redis_1 | `-.__.-' redis_1 | redis_1 | 1:M 19 Jul 07:29:09.432 # Server started, Redis version 3.0.3
以ä¸ã®ããã«ãã¼ã¿ãç»é²ã
$ curl -X POST 127.0.0.1:14567/data/foo -d 'bar'
以ä¸ã®ããã«ãã¼ã¿ãåå¾ã
$ curl -s -X GET 127.0.0.1:14567/value/foo | python -m json.tool { "key": "foo", "value": "bar" }
ãããç°¡åã
ã¹ã±ã¼ã«ã¢ã¦ããã¹ã±ã¼ã«ã¤ã³
以ä¸ã®ãããªç¶æ ã
$ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------- composesinatra_redis_1 /entrypoint.sh redis-server Up 0.0.0.0:32850->6379/tcp composesinatra_web_1 bundle exec ruby app.rb -o ... Up 0.0.0.0:32852->4567/tcp
Web ãµã¼ãã¼ããã©ã¹ã§ 2 å°å¢ãããããªãã¨æã£ãã docker-compose scale
ãå©ç¨ããã°ããã£ã¨ã¹ã±ã¼ã«ã¢ã¦ããã¹ã±ã¼ã«ã¤ã³ãåºæ¥ãã
以ä¸ã®ããã« docker-compose.yml ãä¿®æ£ããã
redis: image: redis ports: - "6379" web: build: . command: bundle exec ruby app.rb -o 0.0.0.0 volumes: - .:/myapp ports: - "4567" links: - redis environment: - REDIS_HOST=redis
以ä¸ã®ããã« docker-compose scale
ã®å¼æ°ã« web=3
ã®ããã«ãµã¼ãã¹ã«å°æ°ãæå®ãã¦å®è¡ããã
$ docker-compose scale web=3
以ä¸ã®ããã«åºåãããã
$ docker-compose scale web=3 Creating composesinatra_web_2... Creating composesinatra_web_3... Starting composesinatra_web_2... Starting composesinatra_web_3...
確èªã
$ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------- composesinatra_redis_1 /entrypoint.sh redis-server Up 0.0.0.0:32850->6379/tcp composesinatra_web_1 bundle exec ruby app.rb -o ... Up 0.0.0.0:32852->4567/tcp composesinatra_web_2 bundle exec ruby app.rb -o ... Up 0.0.0.0:32854->4567/tcp composesinatra_web_3 bundle exec ruby app.rb -o ... Up 0.0.0.0:32855->4567/tcp
ãããå¢ãã¨ãã
$ docker-compose logs Attaching to composesinatra_web_3, composesinatra_web_2, composesinatra_web_1, composesinatra_redis_1 web_3 | [2015-07-19 07:58:56] INFO WEBrick 1.3.1 web_3 | [2015-07-19 07:58:56] INFO ruby 2.2.0 (2014-12-25) [x86_64-linux] web_3 | == Sinatra (v1.4.6) has taken the stage on 4567 for development with backup from WEBrick web_3 | [2015-07-19 07:58:56] INFO WEBrick::HTTPServer#start: pid=1 port=4567 web_2 | [2015-07-19 07:58:56] INFO WEBrick 1.3.1 web_2 | [2015-07-19 07:58:56] INFO ruby 2.2.0 (2014-12-25) [x86_64-linux] web_2 | == Sinatra (v1.4.6) has taken the stage on 4567 for development with backup from WEBrick web_2 | [2015-07-19 07:58:56] INFO WEBrick::HTTPServer#start: pid=1 port=4567
ã¡ãã㨠web_2 㨠web_3 ãèµ·åãã¦ãããã¢ããã次ã«æ¸ããã¦ã¿ãã
$ docker-compose scale web=1 Stopping composesinatra_web_3... Stopping composesinatra_web_2... Removing composesinatra_web_3... Removing composesinatra_web_2... $ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------- composesinatra_redis_1 /entrypoint.sh redis-server Up 0.0.0.0:32850->6379/tcp composesinatra_web_1 bundle exec ruby app.rb -o ... Up 0.0.0.0:32852->4567/tcp
ããããµã¯ãã¨ã³ã³ãããæ¸ã£ã¦ãã
ç°¡åã«ã¹ã±ã¼ã«ã¤ã³ã¨ã¹ã±ã¼ã«ã¢ã¦ããä½é¨åºæ¥ããã©...æ°ã«ãªãã®ãå¤é¨ã«æããã¼ããåºå®åºæ¥ãªããªãç¹ãã©ãããã...ã
ã¨ãããã¨ã§
åè
ç°¡åã便å©
è¤æ°ã® Docker ã³ã³ããã§ãµã¼ãã¹ãèµ·åãããã¨ããæã«ã³ã³ããå士㮠IP ããã¼ãçãæèããªãã¦ãè¯ãã®ã¯å¬ããï¼Docker Links ãã¡ããã¨ç解åºæ¥ã¦ããªããã©è¦äºã«ãããã©ããã³ã°ãã¦ããã¦ããã¨æãï¼ãã¾ããYAML ã§åãµã¼ãã¹æ¯ã®ã³ã³ãããå®ç¾©ãã¦ãããã¨ã§ docker-compose up
ä¸çºã§èµ·ååºæ¥ãã®ãå¬ããéãã