docker - manage docker containers

Author:Cove Schneider, Joshua Conner, Pavel Antonov

Synopsis

New in version 1.4.

Manage the life cycle of docker containers.

Options

parameter required default choices comments
command no
    Set command to run in a container on startup
    count no 1
      Set number of containers to run
      detach no True
        Enable detached mode on start up, leaves container running in background
        dns no
          Set custom DNS servers for the container
          docker_url no unix://var/run/docker.sock
            URL of docker host to issue commands to
            env no
              Set environment variables (e.g. env="PASSWORD=sEcRe7,WORKERS=4")
              expose no
                Set container ports to expose for port mappings or links. (If the port is already exposed using EXPOSE in a Dockerfile, you don't need to expose it again.) (added in Ansible 1.5)
                hostname no
                  Set container hostname
                  image yes
                    Set container image to use
                    links no
                      Link container(s) to other container(s) (e.g. links=redis,postgresql:db) (added in Ansible 1.5)
                      lxc_conf no
                        LXC config parameters, e.g. lxc.aa_profile:unconfined
                        memory_limit no 256MB
                          Set RAM allocated to container
                          name no
                            Set the name of the container (cannot use with count) (added in Ansible 1.5)
                            password no
                              Set remote API password
                              ports no
                                Set private to public port mapping specification using docker CLI-style syntax [([<host_interface>:[host_port]])|(<host_port>):]<container_port>[/udp] (added in Ansible 1.5)
                                privileged no
                                  Set whether the container should run in privileged mode
                                  publish_all_ports no
                                    Publish all exposed ports to the host interfaces (added in Ansible 1.5)
                                    state no present
                                    • present
                                    • stopped
                                    • absent
                                    • killed
                                    • restarted
                                    Set the state of the container
                                    username no
                                      Set remote API username
                                      volumes no
                                        Set volume(s) to mount on the container
                                        volumes_from no
                                          Set shared volume(s) from another container

                                          Note

                                          Requires docker-py >= 0.3.0

                                          Examples


                                          Start one docker container running tomcat in each host of the web group and bind tomcat's listening port to 8080
                                          on the host:
                                          
                                          - hosts: web
                                            sudo: yes
                                            tasks:
                                            - name: run tomcat servers
                                              docker: image=centos command="service tomcat6 start" 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:
                                          
                                          - 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
                                          
                                          Just as in the previous example, but iterates over the list of docker containers with a sequence:
                                          
                                          - 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}}
                                          
                                          Stop, remove all of the running tomcat containers and list the exit code from the stopped containers:
                                          
                                          - 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
                                          
                                          Create a named container:
                                          
                                          - hosts: web
                                            sudo: yes
                                            tasks:
                                            - name: run tomcat server
                                              docker: image=centos name=tomcat command="service tomcat6 start" ports=8080
                                          
                                          Create multiple named containers:
                                          
                                          - 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
                                          
                                          Create containers named in a 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
                                          
                                          Create two linked containers:
                                          
                                          - 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
                                          
                                          Create containers with options specified as key-value pairs and lists:
                                          
                                          - 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
                                          

                                          Table Of Contents

                                          Previous topic

                                          digital_ocean - Create/delete a droplet/SSH_key in DigitalOcean

                                          Next topic

                                          docker_image - manage docker images