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, set
the docker_url
argument to http://${inventory_hostname}
and expose the remote API via HTTP protocol.
- Copy
docker-ansible.py
to your ansible module directory asdocker
(e.g./usr/local/share/ansible/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
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 docker_containers:
- name: start tomcat
hosts: web
user: root
tasks:
- name: run tomcat servers
docker: image=cove/tomcat7 command=/start-tomcat.sh ports=8080 count=5
- name: Display IP address and port mappings for containers
debug: msg={{inventory_hostname}}:{{item.NetworkSettings.Ports['8080/tcp'][0].HostPort}}
with_items: docker_containers
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
docker: image=cove/tomcat7 command=/start-tomcat.sh ports=8080 count={{start_containers_count}}
- name: Display IP address and port mappings for containers
debug: msg={{inventory_hostname}}:{{docker_containers[{{item}}].NetworkSettings.Ports['8080/tcp'][0].HostPort}}
with_sequence: start=0 end={{start_containers_count - 1}}
Stop and remove all of the running tomcat containers:
- name: stop tomcat
hosts: web
user: root
tasks:
- name: stop tomcat servers
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 | ||
docker_url | no | unix://var/run/docker.sock | URL of docker host to issue commands to | |
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 | |
count | no | 1 | Set number of containers to run | |
password | no | Set remote API password | ||
hostname | no | Set container hostname | ||
lxc_conf | no | LXC config parameters, e.g. lxc.aa_profile:unconfined | ||
ports | no | Set private to public port mapping specification (e.g. ports=22,80 or ports=:8080 maps 8080 directly to host) | ||
state | no | present |
|
Set the state of the container |
command | no | 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 | ||
privileged | no | Set whether the container should run in privileged mode |