Documentation

docker_image - Manage docker images.

New in version 1.5.

Synopsis

  • Build, load or pull an image, making the image available for creating containers. Also supports tagging an image into a repository and archiving an image to a .tar file.

Requirements

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

  • python >= 2.6
  • docker-py >= 1.7.0
  • Docker API >= 1.20

Parameters

Parameter
Choices/Defaults
Comments
api_version
Default:
default provided by docker-py
The version of the Docker API running on the Docker Host. Defaults to the latest version of the API supported by docker-py.

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.

aliases: tls_ca_cert
cert_path
Path to the client's TLS certificate file.

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.
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'.

aliases: docker_url
dockerfile
(added in 2.0)
Default:
Dockerfile
Use with state present to provide an alternate name for the Dockerfile to use when building an image.
force
(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.

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
    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
(added in 2.1)
    Choices:
  • no
  • yes ←
When building an image downloads any updates to the FROM image in Dockerfile.
push
(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
(added in 2.1)
    Choices:
  • no
  • yes ←
Remove intermediate containers after build.
ssl_version
Default:
1.0
Provide a valid SSL version number. Default value determined by docker-py, currently 1.0.
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.
tls
Secure the connection to the API by using TLS without verifying the authenticity of the Docker host server.
tls_hostname
Default:
localhost
When verifying the authenticity of the Docker Host server, provide the expected name of the server.
tls_verify
Secure the connection to the API by using TLS and verifying the authenticity of the Docker host server.
use_tls
(added in 2.0)
    Choices:
  • no ←
  • encrypt
  • verify
Default:
no
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.org/en/stable/machine/ for more details.

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.

Author

  • Pavel Antonov (@softzilla)
  • Chris Houseknecht (@chouseknecht)
  • James Tanner (@jctanner)

Hint

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