Getting Started with Docker

    Ansible 2.1.0 includes major updates to the Docker modules, marking the start of a project to create a complete andintegrated set of tools for orchestrating containers. In addition to the above modules, we are also working on thefollowing:

    Still using Dockerfile to build images? Check out ,and start building images from your Ansible playbooks.

    Use the shipit command in ansible-containerto launch your docker-compose file on . Go from an app on your laptop to a fullyscalable app in the cloud in just a few moments.

    There’s more planned. See the latest ideas and thinking at the Ansible proposal repo.

    Using the docker modules requires having the installed on the host running Ansible. You will need to have >= 1.7.0 installed. For Python 2.7 orPython 3, you can install it as follows:

    For Python 2.6, you need a version before 2.0. For these versions, the SDK was called ,so you need to install it as follows:

    1. $ pip install 'docker-py>=1.7.0'

    The docker_service module also requires docker-compose

    1. $ pip install 'docker-compose>=1.7.0'

    You can connect to a local or remote API using parameters passed to each task or by setting environment variables.The order of precedence is command line parameters and then environment variables. If neither a command lineoption or an environment variable is found, a default value will be used. The default values are provided under

    Control how modules connect to the Docker API by passing the following parameters:

    docker_host
    The URL or Unix socket path used to connect to the Docker API. Defaults to .To connect to a remote host, provide the TCP connection string. For example: . IfTLS is used to encrypt the connection to the API, then the module will automatically replace ‘tcp’ in theconnection URL with ‘https’.
    api_version
    The version of the Docker API running on the Docker Host. Defaults to the latest version of the API supportedby docker-py.
    timeout
    The maximum amount of time in seconds to wait on a response from the API. Defaults to 60 seconds.
    tls
    Secure the connection to the API by using TLS without verifying the authenticity of the Docker host server.Defaults to False.
    tls_verify
    Secure the connection to the API by using TLS and verifying the authenticity of the Docker host server.Default is False.
    cacert_path
    Use a CA certificate when performing server verification by providing the path to a CA certificate file.
    cert_path
    Path to the client’s TLS certificate file.
    key_path
    Path to the client’s TLS key file.
    tls_hostname
    When verifying the authenticity of the Docker Host server, provide the expected name of the server. Defaultsto ‘localhost’.
    ssl_version
    Provide a valid SSL version number. Default value determined by docker-py, which at the time of this writingwas 1.0

    Control how the modules connect to the Docker API by setting the following variables in the environment of the hostrunning Ansible:

    The inventory script generates dynamic inventory by making API requests to one or more Docker APIs. It’s dynamicbecause the inventory is generated at run-time rather than being read from a static file. The script generates theinventory by connecting to one or many Docker APIs and inspecting the containers it finds at each API. Which APIs thescript contacts can be defined using environment variables or a configuration file.

    The script will create the following host groups:

    • container id
    • container name
    • container short id
    • imagename (image<image name>)
    • docker_host
    • running
    • stopped

    You can control the behavior of the inventory script by defining environment variables, orcreating a docker.yml file (sample provided in ansible/contrib/inventory). The order of precedence is the docker.ymlfile and then environment variables.

    Environment Variables

    To connect to a single Docker API the following variables can be defined in the environment to control the connectionoptions. These are the same environment variables used by the Docker modules.

    In addition to the connection variables there are a couple variables used to control the execution and output of thescript:

    DOCKER_CONFIG_FILE
    Path to the configuration file. Defaults to ./docker.yml.
    DOCKER_PRIVATE_SSH_PORT:
    The private port (container port) on which SSH is listening for connections. Defaults to 22.
    DOCKER_DEFAULT_IP:
    The IP address to assign to ansible_host when the container’s SSH port is mapped to interface ‘0.0.0.0’.

    Configuration File

    Using a configuration file provides a means for defining a set of Docker APIs from which to build an inventory.

    The default name of the file is derived from the name of the inventory script. By default the script will look forbasename of the script (i.e. docker) with an extension of ‘.yml’.

    You can also override the default name of the script by defining DOCKER_CONFIG_FILE in the environment.

    For the default host and each host in the hosts list define the following attributes:

    1. host:
    2. description: The URL or Unix socket path used to connect to the Docker API.
    3. required: yes
    4. tls:
    5. description: Connect using TLS without verifying the authenticity of the Docker host server.
    6. default: false
    7. required: false
    8.  
    9. tls_verify:
    10. description: Connect using TLS without verifying the authenticity of the Docker host server.
    11. default: false
    12. required: false
    13.  
    14. cert_path:
    15. description: Path to the client's TLS certificate file.
    16. default: null
    17. required: false
    18.  
    19. cacert_path:
    20. description: Use a CA certificate when performing server verification by providing the path to a CA certificate file.
    21. default: null
    22. required: false
    23.  
    24. key_path:
    25. description: Path to the client's TLS key file.
    26. required: false
    27.  
    28. version:
    29. description: The Docker API version.
    30. required: false
    31. default: will be supplied by the docker-py module.
    32.  
    33. timeout:
    34. description: The amount of time in seconds to wait on an API response.
    35. required: false
    36. default: 60
    37.  
    38. default_ip:
    39. description: The IP address to assign to ansible_host when the container's SSH port is mapped to interface
    40. '0.0.0.0'.
    41. required: false
    42. default: 127.0.0.1
    43.  
    44. private_ssh_port:
    45. description: The port containers use for SSH
    46. required: false
    47. default: 22