This module allows you to use Ansible to provision and de-provision Linux containers using the docker container engine.
-
Install docker
-
Install docker-py on the docker server, and/or on the host you will be running ansible playbooks from if you would like to use the docker remote API instead of ansible's SSH session.
git clone https://github.com/dotcloud/docker-py.git cd docker-py sudo python setup.py install
NB: In order to use the docker remote API you will need to use
local_action
in your playbooks and set thedocker_url
argument tohttp://${inventory_hostname}
. -
Copy
docker-ansible.py
to your ansible module directory asdocker
(e.g./usr/local/share/ansbile/docker
)curl https://raw.github.com/cove/docker-ansible/master/docker-ansible.py > docker sudo mv docker /usr/local/share/ansible
The module will try to determine which containers it has already started on subsequent runs of the playbook.
Start one docker container running tomcat in each host of the web group and bind tomcat's listening port to 8080 on the host:
- name: start tomcat
hosts: web
user: root
tasks:
- name: run tomcat servers
action: docker image=cove/tomcat7 command=/start-tomcat.sh ports=:8080
The tomcat server's port is NAT'ed to a dynamic port on the host, but you can determine which port the server was mapped to using $DockerContainers:
- name: start tomcat
hosts: web
user: root
tasks:
- name: run tomcat servers
action: docker image=cove/tomcat7 command=/start-tomcat.sh ports=8080 count=5
- name: Display IP address and port mappings for containers
action: shell echo Mapped to ${inventory_hostname}:${item.NetworkSettings.PortMapping.8080}
with_items: $DockerContainers
Just as in the previous example, but iterates through the list of docker containers with a sequence:
- name: start tomcat
hosts: web
user: root
vars:
start_containers_count: 5
tasks:
- name: run tomcat servers
action: docker image=cove/tomcat7 command=/start-tomcat.sh ports=8080 count=$start_containers_count
- name: Display IP address and port mappings for containers
local_action: shell echo Mapped to ${inventory_hostname}:${DockerContainers[${item}].NetworkSettings.PortMapping.8080}
with_sequence: start=0 end=$start_containers_count
Stop and remove all of the running tomcat containers:
- name: stop tomcat
hosts: web
user: root
tasks:
- name: run tomcat servers
action: docker image=cove/tomcat7 command=/start-tomcat.sh state=absent
parameter | required | default | choices | comments |
---|---|---|---|---|
username | no | Set remote API username | ||
memory_limit | no | 256MB | Set RAM allocated to container | |
env | no | Set environment variables | ||
image | yes | Set container image to use | ||
volumes | no | Set volume(s) to mount on the container | ||
detach | no | True | Enable detached mode on start up, leaves container running in background | |
memory_swap | no | Set virtual memory swap space allocated to container | ||
count | no | 1 | Set number of containers to run | |
password | no | Set remote API password | ||
hostname | no | Set container hostname | ||
docker_url | no | http://127.0.0.1:4243 | URL of docker host to issue commands to | |
state | no | present |
|
Set the state of the container |
command | yes | Set command to run in a container on startup | ||
dns | no | Set custom DNS servers for the container | ||
volumes_from | no | Set shared volume(s) from another container | ||
ports | yes | Set private to public port mapping specification (e.g. ports=22,80 or ports=:8080 maps 8080 directly to host) |