docker_image - Manage docker images.

New in version 1.5.

Synopsis

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.6
  • docker-py >= 1.7.0
  • Please note that the docker-py Python module has been superseded by docker (see here for details). For Python 2.6, docker-py must be used. Otherwise, it is recommended to install the docker Python module. Note that both modules should not be installed at the same time. Also note that when both modules are installed and one of them is uninstalled, the other might no longer function and a reinstall of it is required.
  • Docker API >= 1.20

Parameters

Parameter Choices/Defaults Comments
api_version Default:
auto
The version of the Docker API running on the Docker Host. Defaults to the latest version of the API supported by docker-py.
If the value is not specified in the task, the value of environment variable DOCKER_API_VERSION will be used instead. If the environment variable is not set, the default value will be used.

aliases: docker_api_version
archive_path
(added in 2.1)
Use with state present to archive an image to a .tar file.
buildargs
(added in 2.2)
Provide a dictionary of key:value build arguments that map to Dockerfile ARG directive.
Docker expects the value to be a string. For convenience any non-string values will be converted to strings.
Requires Docker API >= 1.21 and docker-py >= 1.7.0.
cacert_path
Use a CA certificate when performing server verification by providing the path to a CA certificate file.
If the value is not specified in the task and the environment variable DOCKER_CERT_PATH is set, the file ca.pem from the directory specified in the environment variable DOCKER_CERT_PATH will be used.

aliases: tls_ca_cert
cert_path
Path to the client's TLS certificate file.
If the value is not specified in the task and the environment variable DOCKER_CERT_PATH is set, the file cert.pem from the directory specified in the environment variable DOCKER_CERT_PATH will be used.

aliases: tls_client_cert
container_limits
(added in 2.1)
A dictionary of limits applied to each container created by the build process.
memswap
Total memory (memory + swap), -1 to disable swap.
cpushares
CPU shares (relative weight).
cpusetcpus
CPUs in which to allow execution, e.g., "0-3", "0,1".
memory
Set memory limit for build.
debug
bool
    Choices:
  • no ←
  • yes
Debug mode
docker_host Default:
unix://var/run/docker.sock
The URL or Unix socket path used to connect to the Docker API. To connect to a remote host, provide the TCP connection string. For example, 'tcp://192.0.2.23:2376'. If TLS is used to encrypt the connection, the module will automatically replace 'tcp' in the connection URL with 'https'.
If the value is not specified in the task, the value of environment variable DOCKER_HOST will be used instead. If the environment variable is not set, the default value will be used.

aliases: docker_url
dockerfile
(added in 2.0)
Use with state present to provide an alternate name for the Dockerfile to use when building an image.
force
bool

(added in 2.1)
    Choices:
  • no ←
  • yes
Use with state absent to un-tag and remove all images matching the specified name. Use with state present to build, load or pull an image when the image already exists.
http_timeout
(added in 2.1)
Timeout for HTTP requests during the image build operation. Provide a positive integer value for the number of seconds.
key_path
Path to the client's TLS key file.
If the value is not specified in the task and the environment variable DOCKER_CERT_PATH is set, the file key.pem from the directory specified in the environment variable DOCKER_CERT_PATH will be used.

aliases: tls_client_key
load_path
(added in 2.2)
Use with state present to load an image from a .tar file.
name
required
Image name. Name format will be one of: name, repository/name, registry_server:port/name. When pushing or pulling an image the name can optionally include the tag by appending ':tag_name'.
nocache
bool
    Choices:
  • no ←
  • yes
Do not use cache when building an image.
path
Use with state 'present' to build an image. Will be the path to a directory containing the context and Dockerfile for building an image.

aliases: build_path
pull
bool

(added in 2.1)
    Choices:
  • no
  • yes ←
When building an image downloads any updates to the FROM image in Dockerfile.
push
bool

(added in 2.2)
    Choices:
  • no ←
  • yes
Push the image to the registry. Specify the registry as part of the name or repository parameter.
repository
(added in 2.1)
Full path to a repository. Use with state present to tag the image into the repository. Expects format repository:tag. If no tag is provided, will use the value of the tag parameter or latest.
rm
bool

(added in 2.1)
    Choices:
  • no
  • yes ←
Remove intermediate containers after build.
ssl_version
Provide a valid SSL version number. Default value determined by ssl.py module.
If the value is not specified in the task, the value of environment variable DOCKER_SSL_VERSION will be used instead.
state
    Choices:
  • absent
  • present ←
  • build
Make assertions about the state of an image.
When absent an image will be removed. Use the force option to un-tag and remove all images matching the provided name.
When present check if an image exists using the provided name and tag. If the image is not found or the force option is used, the image will either be pulled, built or loaded. By default the image will be pulled from Docker Hub. To build the image, provide a path value set to a directory containing a context and Dockerfile. To load an image, specify load_path to provide a path to an archive file. To tag an image to a repository, provide a repository path. If the name contains a repository path, it will be pushed.
NOTE: build is DEPRECATED and will be removed in release 2.3. Specifying build will behave the same as present.
tag Default:
latest
Used to select an image when pulling. Will be added to the image when pushing, tagging or building. Defaults to latest.
If name parameter format is name:tag, then tag value from name will take precedence.
timeout Default:
60
The maximum amount of time in seconds to wait on a response from the API.
If the value is not specified in the task, the value of environment variable DOCKER_TIMEOUT will be used instead. If the environment variable is not set, the default value will be used.
tls
bool
    Choices:
  • no ←
  • yes
Secure the connection to the API by using TLS without verifying the authenticity of the Docker host server.
If the value is not specified in the task, the value of environment variable DOCKER_TLS will be used instead. If the environment variable is not set, the default value will be used.
tls_hostname Default:
localhost
When verifying the authenticity of the Docker Host server, provide the expected name of the server.
If the value is not specified in the task, the value of environment variable DOCKER_TLS_HOSTNAME will be used instead. If the environment variable is not set, the default value will be used.
tls_verify
bool
    Choices:
  • no ←
  • yes
Secure the connection to the API by using TLS and verifying the authenticity of the Docker host server.
If the value is not specified in the task, the value of environment variable DOCKER_TLS_VERIFY will be used instead. If the environment variable is not set, the default value will be used.
use_tls
(added in 2.0)
    Choices:
  • no ←
  • encrypt
  • verify
DEPRECATED. Whether to use tls to connect to the docker server. Set to no when TLS will not be used. Set to encrypt to use TLS. And set to verify to use TLS and verify that the server's certificate is valid for the server. NOTE: If you specify this option, it will set the value of the tls or tls_verify parameters.

Notes

Note

  • Connect to the Docker daemon by providing parameters with each task or by defining environment variables. You can define DOCKER_HOST, DOCKER_TLS_HOSTNAME, DOCKER_API_VERSION, DOCKER_CERT_PATH, DOCKER_SSL_VERSION, DOCKER_TLS, DOCKER_TLS_VERIFY and DOCKER_TIMEOUT. If you are using docker machine, run the script shipped with the product that sets up the environment. It will set these variables for you. See https://docker-py.readthedocs.io/en/stable/machine/ for more details.
  • When connecting to Docker daemon with TLS, you might need to install additional Python packages. For the Docker SDK for Python, version 2.4 or newer, this can be done by installing docker[tls] with pip.
  • Note that the Docker SDK for Python only allows to specify the path to the Docker configuration for very few functions. In general, it will use $HOME/docker/config.json if the DOCKER_CONFIG environment variable is not specified, and use $DOCKER_CONFIG/config.json otherwise.

Examples

- name: pull an image
  docker_image:
    name: pacur/centos-7

- name: Tag and push to docker hub
  docker_image:
    name: pacur/centos-7
    repository: dcoppenhagan/myimage
    tag: 7.0
    push: yes

- name: Tag and push to local registry
  docker_image:
     name: centos
     repository: localhost:5000/centos
     tag: 7
     push: yes

- name: Remove image
  docker_image:
    state: absent
    name: registry.ansible.com/chouseknecht/sinatra
    tag: v1

- name: Build an image and push it to a private repo
  docker_image:
    path: ./sinatra
    name: registry.ansible.com/chouseknecht/sinatra
    tag: v1
    push: yes

- name: Archive image
  docker_image:
    name: registry.ansible.com/chouseknecht/sinatra
    tag: v1
    archive_path: my_sinatra.tar

- name: Load image from archive and push to a private registry
  docker_image:
    name: localhost:5000/myimages/sinatra
    tag: v1
    push: yes
    load_path: my_sinatra.tar

- name: Build image and with buildargs
  docker_image:
     path: /path/to/build/dir
     name: myimage
     buildargs:
       log_volume: /var/log/myapp
       listen_port: 8080

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key Returned Description
image
dict
success
Image inspection results for the affected image.



Status

This module is flagged as preview which means that it is not guaranteed to have a backwards compatible interface.

Maintenance

This module is flagged as community which means that it is maintained by the Ansible Community. See Module Maintenance & Support for more info.

For a list of other modules that are also maintained by the Ansible Community, see here.

Author

  • Pavel Antonov (@softzilla)
  • Chris Houseknecht (@chouseknecht)

Hint

If you notice any issues in this documentation you can edit this document to improve it.