注:做者是 Cove Schneider,Joshua Conner, Pavel Antonov。原文是 Ansible 的官方文檔中 docker - manage docker containershtml
在 Ansible 的 1.4 版本及以上支持。web
管理 docker 容器的生命週期。redis
屬性 | 必需 | 默認值 | 選擇 | 說明 |
---|---|---|---|---|
command | no | 在啓動的時候設置一個命令來運行容器 | ||
count | no | 1 | 設置運行的容器的數量 | |
detach | no | true | 以 detached 模式啓動,讓容器在後臺運行 | |
dns | no | 爲容器設置自定義的 DNS 服務器 | ||
docker_api_version | no | docker-py default remote API version | 使用的 Remote API 版本,默認的是當前 docker-py 默認指定的版本(在 Ansible 1.8 中添加) | |
docker_url | no | unix://var/run/docker.sock | docker 主機發出命令的 URL | |
env | no | 設置環境變量,(好比:env="PASSWORD=sEcRe7,WORKERS=4") | ||
expose | no | 設置容器 expose 的端口,用於映射和 links。(若是這個端口已經在 Dockerfile 中 EXPOSE 了,你不須要再次 expose 它)。在 Ansible 1.5 中添加 | ||
hostname | no | 設置容器的主機名 | ||
image | yes | 設置使用的鏡像 | ||
links | no | 把容器與其餘容器鏈接起來(好比:links=redis,postgresql:db)。在 Ansible 1.5 中添加 | ||
lxc_conf | no | LXC 配置參數,好比: lxc.aa_profile:unconfined | ||
memory_limit | no | 256 MB | 設置給容器分配的內存 | |
name | no | 設置容器的名字,不能與 count 參數同時使用,在 Ansible 1.5 中添加 | ||
net | no | 爲 容器設置網絡模式(bridge, none, container:<name|id>, host). 要求 docker >= 0.11. (在 Ansible 1.8 中被添加)) | ||
password | no | 設置遠程API 的密碼 | ||
ports | no | 使用 docker CLI-style 語法,設置私有到公共端口的映射技術參數, [([
|
||
privileged | no | 設置容器是否應該容許在 privileged 模式 | ||
publish_all_ports | no | 發佈全部的暴露端口給主機接口,在 Ansible 1.5 中添加 | ||
registry | no | 用於拉取鏡像的遠程倉庫的 URL,(在 Ansible 1.8 中添加 ) | ||
state | no | present | present running stopped absent killed restarted | 設置容器的狀態 |
stdin_open | no | 保持 stdin 打開。(在 Ansible 1.6 中添加) | ||
tty | no | 分配一個 pseudo-tty (在 Ansible 1.6 添加) | ||
username | no | 設置遠程 API 的名稱 | ||
volumes | no | 設置掛載在容器中的 volume | ||
volumes_from | no | 從另一個容器中設置共享卷 |
注意:要求 docker-py >= 0.3.0
注意:docker >= 0.10.0sql
在 web 組的每臺主機上啓動一個運行 tomcat 的容器,而且把 tomcat 的監聽端口綁定到主機的 8080:docker
- hosts: web sudo: yes tasks: - name: run tomcat servers docker: image=centos command="service tomcat6 start" ports=8080
tomcat 服務器的端口是 NAT 到主機上的一個動態端口,可是你能夠決定服務器哪一個端口與 Docker 容器作映射:centos
- hosts: web sudo: yes tasks: - name: run tomcat servers docker: image=centos command="service tomcat6 start" ports=8080 count=5 - name: Display IP address and port mappings for containers debug: msg={{inventory_hostname}}:{{item['HostConfig']['PortBindings']['8080/tcp'][0]['HostPort']}} with_items: docker_containers
正如前面的例子,可是是用一個 sequence 迭代 docker 容器的列表:api
- hosts: web sudo: yes vars: start_containers_count: 5 tasks: - name: run tomcat servers docker: image=centos command="service tomcat6 start" ports=8080 count={{start_containers_count}} - name: Display IP address and port mappings for containers debug: msg="{{inventory_hostname}}:{{docker_containers[{{item}}]['HostConfig']['PortBindings']['8080/tcp'][0]['HostPort']}}" with_sequence: start=0 end={{start_containers_count - 1}}
中止、移除全部正在運行的 tomcat 容器,而且從中止的容器中列出退出碼:tomcat
- hosts: web sudo: yes tasks: - name: stop tomcat servers docker: image=centos command="service tomcat6 start" state=absent - name: Display return codes from stopped containers debug: msg="Returned {{inventory_hostname}}:{{item}}" with_items: docker_containers
建立一個命名的容器:服務器
- hosts: web sudo: yes tasks: - name: run tomcat server docker: image=centos name=tomcat command="service tomcat6 start" ports=8080
建立多個命名的容器:網絡
- hosts: web sudo: yes tasks: - name: run tomcat servers docker: image=centos name={{item}} command="service tomcat6 start" ports=8080 with_items: - crookshank - snowbell - heathcliff - felix - sylvester
建立容器並經過使用 sequence 命名:
- hosts: web sudo: yes tasks: - name: run tomcat servers docker: image=centos name={{item}} command="service tomcat6 start" ports=8080 with_sequence: start=1 end=5 format=tomcat_%d.example.com
建立兩個鏈接的容器:
- hosts: web sudo: yes tasks: - name: ensure redis container is running docker: image=crosbymichael/redis name=redis - name: ensure redis_ambassador container is running docker: image=svendowideit/ambassador ports=6379:6379 links=redis:redis name=redis_ambassador_ansible
建立容器,指定選項做爲鍵值對和列表:
- hosts: web sudo: yes tasks: - docker: image: namespace/image_name links: - postgresql:db - redis:redis
Create containers with options specified as strings and lists as comma-separated strings:
- hosts: web sudo: yes tasks: docker: image=namespace/image_name links=postgresql:db,redis:redis
建立一個沒有 networking 的容器:
- hosts: web sudo: yes tasks: docker: image=namespace/image_name net=none