Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a Compose file to configure your application’s services. Then, using a single command, you create and start all the services from your configuration.python
Dockerfile 能夠讓用戶管理一個單獨的應用容器;而 Compose 則容許用戶在一個模板(YAML 格式)中定義一組相關聯的應用容器(被稱爲一個 project,即項目),例如一個 Web 服務容器再加上後端的數據庫服務容器等,就拿官網的 Python 例子來講,功能很簡單,利用 redis 的 incr 的功能對頁面的訪問量進行統計。web
docker-compose 的安裝能夠參考官網,若是安裝了 Docker for Mac 或者 Docker for windows 則直接就存在了。redis
建立項目目錄:docker
$ mkdir composetest $ cd composetest
<!--more-->
建立 Python 的程序 app.py
,功能就是利用 redis 的 incr 方法進行訪問計數。數據庫
from flask import Flask from redis import Redis app = Flask(__name__) redis = Redis(host='redis', port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello World! I have been seen %s times.' % redis.get('hits') if __name__ == "__main__": app.run(host="0.0.0.0", debug=True)
因爲 Python 依賴的 flask
、redis
組件都須要另外安裝,好比使用 pip
來安裝,單獨設置一文件 requirements.txt
,內容以下:django
flask redis
建立 service 依賴的第一個 image,app.py
程序的運行環境,利用 Dockerfile
來製做,內容以下:flask
FROM python:2.7 #基於 python:2.7 鏡像 ADD . /code #將本地目錄中的內容添加到 container 的 /code 目錄下 WORKDIR /code #設置程序工做目錄爲 /code RUN pip install -r requirements.txt #運行安裝命令 CMD python app.py #啓動程序
Dockerfile
建立好就能夠製做鏡像了,運行 docker build -t compose/python_app .
,成功後經過 docker images
查看即能看到:windows
docker images REPOSITORY TAG IMAGE ID CREATED SIZE compose/python_app latest 1a92fed00abd 59 minutes ago 680.4 MB
接下來製做 docker-compose 須要的配置文件 docker-compose.yml
, version 要選擇 2 ,1的版本很古老了,配置中能夠看到建立了 2 個 service,web
和 redis
,各自有依賴的鏡像,其中 web
開放 container 的5000端口,並與 host 的5000端口應對,方便經過 localhost:5000
來訪問, volumes
即將本地目錄中的文件加載到容器的 /code
中,depends_on
表名 services web
是依賴另外一個 service redis
的,完整的配置以下:後端
version: '2' services: web: image: compose/python_app ports: - "5000:5000" volumes: - .:/code depends_on: - redis redis: image: redis
前提都搞定了,就差最後一步啓動了,命令 docker-compose up
,成功後以下:瀏覽器
Attaching to composetestbypython_redis_1 redis_1 | 1:C 04 Nov 10:35:17.448 # 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.2.5 (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 04 Nov 10:35:17.450 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. redis_1 | 1:M 04 Nov 10:35:17.450 # Server started, Redis version 3.2.5 redis_1 | 1:M 04 Nov 10:35:17.451 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. redis_1 | 1:M 04 Nov 10:35:17.451 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. redis_1 | 1:M 04 Nov 10:35:17.451 * The server is now ready to accept connections on port 6379
此時經過compose 的 ps 命令也能看到 docker-compose ps
:
docker-compose ps Name Command State Ports --------------------------------------------------------------------------------------------- composetestbypython_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp composetestbypython_web_1 /bin/sh -c python app.py Up 0.0.0.0:5000->5000/tcp
ok ,在瀏覽器中訪問 http://localhost:5000
就能看到最終的樣子啦。
{% qnimg docker/2016-11-04-22-40-11.jpg title: alt: 'class:' %}
docker-compose
還有不少實用的命令,好比 logs、build、start、stop 等,能夠經過 docker-compose --help
來查看:
Define and run multi-container applications with Docker. Usage: docker-compose [-f <arg>...] [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 -H, --host HOST Daemon socket to connect to --tls Use TLS; implied by --tlsverify --tlscacert CA_PATH Trust certs signed only by this CA --tlscert CLIENT_CERT_PATH Path to TLS certificate file --tlskey TLS_KEY_PATH Path to TLS key file --tlsverify Use TLS and verify the remote --skip-hostname-check Don't check the daemon's hostname against the name specified in the client certificate (for example if your docker host is an IP address) Commands: build Build or rebuild services bundle Generate a Docker bundle from the Compose file config Validate and view the compose file create Create services down Stop and remove containers, networks, images, and volumes events Receive real time events from containers exec Execute a command in a running container help Get help on a command kill Kill containers logs View output from containers pause Pause services port Print the public port for a port binding ps List containers pull Pulls service images push Push 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 unpause Unpause services up Create and start containers version Show the Docker-Compose version information
更多 docker-compose
例子參考官網 doc 文檔 :
Docker-compose with Django : https://docs.docker.com/compo...
Get started with Rails : https://docs.docker.com/compo...
Get started with WordPress : https://docs.docker.com/compo...