Docker學習筆記4: Docker-Compose—簡化複雜容器應用的利器
[[email protected] dockerfile_dir]# docker-compose up Building web Step 1 : FROM python:2.7 ---> 77cf0ea98df6 Step 2 : ADD . /code ---> 75cd864e0ccb Removing intermediate container ac7333aef426 Step 3 : WORKDIR /code ---> Running in 5b5313e1b03c ---> e972204373ea Removing intermediate container 5b5313e1b03c Step 4 : RUN pip install -r requirements.txt ---> Running in 8b5e6cb8288a Collecting flask (from -r requirements.txt (line 1)) Downloading Flask-0.11.1-py2.py3-none-any.whl (80kB) Collecting redis (from -r requirements.txt (line 2)) Downloading redis-2.10.5-py2.py3-none-any.whl (60kB) Collecting itsdangerous>=0.21 (from flask->-r requirements.txt (line 1)) Downloading itsdangerous-0.24.tar.gz (46kB) Collecting Jinja2>=2.4 (from flask->-r requirements.txt (line 1)) Downloading Jinja2-2.8-py2.py3-none-any.whl (263kB) Collecting Werkzeug>=0.7 (from flask->-r requirements.txt (line 1)) Downloading Werkzeug-0.11.11-py2.py3-none-any.whl (306kB) Collecting click>=2.0 (from flask->-r requirements.txt (line 1)) Downloading click-6.6.tar.gz (283kB) Collecting MarkupSafe (from Jinja2>=2.4->flask->-r requirements.txt (line 1)) Downloading MarkupSafe-0.23.tar.gz Building wheels for collected packages: itsdangerous, click, MarkupSafe Running setup.py bdist_wheel for itsdangerous: started Running setup.py bdist_wheel for itsdangerous: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/fc/a8/66/24d655233c757e178d45dea2de22a04c6d92766abfb741129a Running setup.py bdist_wheel for click: started Running setup.py bdist_wheel for click: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/b0/6d/8c/cf5ca1146e48bc7914748bfb1dbf3a40a440b8b4f4f0d952dd Running setup.py bdist_wheel for MarkupSafe: started Running setup.py bdist_wheel for MarkupSafe: finished with status 'done' Stored in directory: /root/.cache/pip/wheels/a3/fa/dc/0198eed9ad95489b8a4f45d14dd5d2aee3f8984e46862c5748 Successfully built itsdangerous click MarkupSafe Installing collected packages: itsdangerous, MarkupSafe, Jinja2, Werkzeug, click, flask, redis Successfully installed Jinja2-2.8 MarkupSafe-0.23 Werkzeug-0.11.11 click-6.6 flask-0.11.1 itsdangerous-0.24 redis-2.10.5 ---> cbdb581853a2 Starting dockerfiledir_redis_1 Starting dockerfiledir_web_1 Attaching to dockerfiledir_redis_1, dockerfiledir_web_1 redis_1 | 1:C 31 Oct 13:52:41.359 # 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.4 (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 31 Oct 13:52:41.363 # 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 31 Oct 13:52:41.363 # Server started, Redis version 3.2.4 redis_1 | 1:M 31 Oct 13:52:41.363 # 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 31 Oct 13:52:41.364 # 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 31 Oct 13:52:41.364 * DB loaded from disk: 0.000 seconds redis_1 | 1:M 31 Oct 13:52:41.364 * The server is now ready to accept connections on port 6379 web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) web_1 | * Restarting with stat web_1 | * Debugger is active! web_1 | * Debugger pin code: 160-426-538
至此,兩個應用就跑起來了,演示完畢.
瀏覽器訪問http://192.168.142.134:5000/ 會有如下內容,其中的22是重新整理瀏覽器得來的,剛開始數字是1.3. Yaml檔案參考
在上面的yaml檔案中,我們可以看到compose檔案的基本結構。首先是定義一個服務名,下面是yaml服務中的一些選項條目:
image
:映象的ID
build
:直接從pwd的Dockerfile來build,而非通過image選項來pull
links
:連線到那些容器。每個佔一行,格式為SERVICE[:ALIAS],例如 – db[:database]
external_links
:連線到該compose.yaml檔案之外的容器中,比如是提供共享或者通用服務的容器服務。格式同links
command
:替換預設的command命令
ports
: 匯出埠。格式可以是:
ports:-"3000"-"8000:8000"-"127.0.0.1:8001:8001"
expose
:匯出埠,但不對映到宿主機的埠上。它僅對links的容器開放。格式直接指定埠號即可。
volumes
:載入路徑作為卷,可以指定只讀模式:
volumes:-/var/lib/mysql
- cache/:/tmp/cache
-~/configs:/etc/configs/:ro
volumes_from
:載入其他容器或者服務的所有卷
environment:- RACK_ENV=development
- SESSION_SECRET
env_file
extends
:擴充套件另一個服務,可以覆蓋其中的一些選項。一個sample如下:
common.yml
webapp:
build:./webapp
environment:- DEBUG=false- SEND_EMAILS=false
development.yml
web:extends:
file: common.yml
service: webapp
ports:-"8000:8000"
links:- db
environment:- DEBUG=true
db:
image: postgres
net
:容器的網路模式,可以為”bridge”, “none”, “container:[name or id]”, “host”中的一個。
dns
:可以設定一個或多個自定義的DNS地址。
dns_search
:可以設定一個或多個DNS的掃描域。
其他的 working_dir, entrypoint, user, hostname, domainname, mem_limit, privileged, restart, stdin_open, tty, cpu_shares
,和 docker run
命令是一樣的,這些命令都是單行的命令。例如:
cpu_shares:73
working_dir:/code
entrypoint: /code/entrypoint.sh
user: postgresql
hostname: foo
domainname: foo.com
mem_limit:1000000000
privileged:true
restart: always
stdin_open:true
tty:true
4. docker-compose常用命令
在第二節中的 docker-compose up
,這兩個容器都是在前臺執行的。我們可以指定-d命令以daemon的方式啟動容器。除此之外,docker-compose還支援下面引數:
--verbose
:輸出詳細資訊
-f
制定一個非docker-compose.yml命名的yaml檔案
-p
設定一個專案名稱(預設是directory名)
docker-compose的動作包括:
build
:構建服務
kill -s SIGINT
:給服務傳送特定的訊號。
logs
:輸出日誌
port
:輸出繫結的埠
ps
:輸出執行的容器
pull
:pull服務的image
rm
:刪除停止的容器
run
: 執行某個服務,例如docker-compose run web python manage.py shell
start
:執行某個服務中存在的容器。
stop
:停止某個服務中存在的容器。
up
:create + run + attach容器到服務。
scale
:設定服務執行的容器數量。例如:docker-compose scale web=2 worker=3
參考:
Compose Document