1. 程式人生 > >Docker實踐8:Compose

Docker實踐8:Compose

今天要在我的本子上搭建一個mediawiki環境,之前的經驗,用fig去配置是最簡單的了。可是下載fig失敗,去官網一看才知道,fig已經被compose工具取代了。原文是這樣說的:

Fig has been replaced by Docker Compose, and is now deprecated. The new documentation is on the Docker website.

既然如此,就去官網看看compose到底為何物。
compose是用來在docker中定義和運行復雜應用的小工具,比如在一個檔案中定義多個容器,只用一行命令就可以讓一切就緒並執行。它的功能與我們所熟知的fig相似,換句話說,compose是fig的替代產品,fig就這樣退出docker的歷史舞臺了。

Fig has been renamed to Docker Compose, or just Compose for short. This has several implications for you:

The command you type is now docker-compose, not fig.
You should rename your fig.yml to docker-compose.yml.

看來fig是被重新命名成compose了,配置檔案變成了docker-compose.yml,其他都幾乎一樣。不但fig不能下載了,原來有fig工具的環境用fig去搭建mediawiki都不可用了,報錯如下:

fig up -d
Creating hgserver_mediawiki_1...
Pulling image amclain/hgweb...
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/code/build/fig/out00-PYZ.pyz/fig.cli.main", line 31, in main
  File "/code/build/fig/out00-PYZ.pyz/fig.cli.docopt_command", line 21, in sys_dispatch
  File "/code/build/fig/out00-PYZ.pyz/fig.cli.command"
, line 28, in dispatch File "/code/build/fig/out00-PYZ.pyz/fig.cli.docopt_command", line 24, in dispatch ... fig.progress_stream.StreamOutputError: Get https://index.docker.io/v1/repositories/amclain/hgweb/images: dial tcp: lookup index.docker.io on 10.202.72.118:53: read udp 10.202.72.118:53: i/o timeout

如此看來,使用compose是必須的了。
下面說說compose的用法。
1.安裝compose
OS X和64位的Linux用如下命令安裝。

# curl -L https://github.com/docker/compose/releases/download/1.1.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

#chmod +x /usr/local/bin/docker-compose

其他平臺可以像python包一樣安裝:

$ sudo pip install -U docker-compose

2.命令簡介

$ docker-compose 
Fast, isolated development environments using Docker.

Usage:
  docker-compose [options] [COMMAND] [ARGS...]
  docker-compose -h|--help

Options:
  --verbose                 Show more output
  --version                 Print version and exit
  -f, --file FILE           Specify an alternate compose file (default: docker-compose.yml)
  -p, --project-name NAME   Specify an alternate project name (default: directory name)

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
  rm        Remove stopped containers
  run       Run a one-off command
  scale     Set number of containers for a service
  start     Start services
  stop      Stop services
  restart   Restart services
  up        Create and start containers

3.compose編寫mediawiki的docker-compose.yml
首先編寫compose的配置檔案,語法與fig類似,檔名為docker-compose.yml,內容如下:

wiki2:
    image: 'nickstenning/mediawiki'
    ports:
        - "8880:80"
    links:
        - db:database
    volumes:
        - /data/wiki2:/data

db:
    image: "mysql"
    expose:
        - "3306"
    environment:
        - MYSQL_ROOT_PASSWORD=defaultpass

4.建立並啟動mediawiki

$ docker-compose up -d