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 (on host that executes module)

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

Options

parameter required default choices comments
api_version
no 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)
no
Use with state present to archive an image to a .tar file.
buildargs
(added in 2.2)
no
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
no
Use a CA certificate when performing server verification by providing the path to a CA certificate file.
aliases: tls_ca_cert
cert_path
no
Path to the client's TLS certificate file.
aliases: tls_client_cert
container_limits
(added in 2.1)
no
A dictionary of limits applied to each container created by the build process.
Dictionary object container_limits
parameter required default choices comments
memswap
no
Total memory (memory + swap), -1 to disable swap.
cpushares
no
CPU shares (relative weight).
cpusetcpus
no
CPUs in which to allow execution, e.g., "0-3", "0,1".
memory
no
Set memory limit for build.
docker_host
no 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)
no Dockerfile
Use with state present to provide an alternate name for the Dockerfile to use when building an image.
force
(added in 2.1)
no
  • yes
  • no
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)
no
Timeout for HTTP requests during the image build operation. Provide a positive integer value for the number of seconds.
key_path
no
Path to the client's TLS key file.
aliases: tls_client_key
load_path
(added in 2.2)
no
Use with state present to load an image from a .tar file.
name
yes
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
no
  • yes
  • no
Do not use cache when building an image.
path
no
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)
no True
  • yes
  • no
When building an image downloads any updates to the FROM image in Dockerfile.
push
(added in 2.2)
no
  • yes
  • no
Push the image to the registry. Specify the registry as part of the name or repository parameter.
repository
(added in 2.1)
no
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)
no True
  • yes
  • no
Remove intermediate containers after build.
ssl_version
no 1.0
Provide a valid SSL version number. Default value determined by docker-py, currently 1.0.
state
no present
  • 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
no 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
no 60
The maximum amount of time in seconds to wait on a response from the API.
tls
no
Secure the connection to the API by using TLS without verifying the authenticity of the Docker host server.
tls_hostname
no localhost
When verifying the authenticity of the Docker Host server, provide the expected name of the server.
tls_verify
no
Secure the connection to the API by using TLS and verifying the authenticity of the Docker host server.
use_tls
(added in 2.0)
no
  • False
  • 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.

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 Return Values, the following are the fields unique to this module:

name description returned type sample
image
Image inspection results for the affected image.
success dict {}

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.

Status

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

For help in developing on modules, should you be so inclined, please read Community Information & Contributing, Testing Ansible and Developing Modules.

© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.4/docker_image_module.html