ansible.builtin.docker_image (v2.9.24) — module

Manage docker images.

| "added in version" 1.5 of ansible.builtin"

Authors: Pavel Antonov (@softzilla), Chris Houseknecht (@chouseknecht), Sorin Sbarnea (@ssbarnea)

preview | supported by community

Install Ansible via pip

Install with pip install ansible==2.9.24

Description

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.

Since Ansible 2.8, it is recommended to explicitly specify the image's source (I(source) can be C(build), C(load), C(pull) or C(local)). This will be required from Ansible 2.12 on.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.

- name: pull an image
  docker_image:
    name: pacur/centos-7
    source: pull
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Tag and push to docker hub
  docker_image:
    name: pacur/centos-7:56
    repository: dcoppenhagan/myimage:7.56
    push: yes
    source: local
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Tag and push to local registry
  docker_image:
    # Image will be centos:7
    name: centos
    # Will be pushed to localhost:5000/centos:7
    repository: localhost:5000/centos
    tag: 7
    push: yes
    source: local
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add tag latest to image
  docker_image:
    name: myimage:7.1.2
    repository: myimage:latest
    # As 'latest' usually already is present, we need to enable overwriting of existing tags:
    force_tag: yes
    source: local
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove image
  docker_image:
    state: absent
    name: registry.ansible.com/chouseknecht/sinatra
    tag: v1
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Build an image and push it to a private repo
  docker_image:
    build:
      path: ./sinatra
    name: registry.ansible.com/chouseknecht/sinatra
    tag: v1
    push: yes
    source: build
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Archive image
  docker_image:
    name: registry.ansible.com/chouseknecht/sinatra
    tag: v1
    archive_path: my_sinatra.tar
    source: local
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
    source: load
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Build image and with build args
  docker_image:
    name: myimage
    build:
      path: /path/to/build/dir
      args:
        log_volume: /var/log/myapp
        listen_port: 8080
    source: build
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Build image using cache source
  docker_image:
    name: myimage:latest
    build:
      path: /path/to/build/dir
      # Use as cache source for building myimage
      cache_from:
        - nginx:latest
        - alpine:3.8
    source: build

Inputs

    
rm:
    default: true
    description:
    - Remove intermediate containers after build.
    - Please use I(build.rm) instead. This option will be removed in Ansible 2.12.
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

tag:
    default: latest
    description:
    - Used to select an image when pulling. Will be added to the image when pushing, tagging
      or building. Defaults to I(latest).
    - If I(name) parameter format is I(name:tag), then tag value from I(name) will take
      precedence.
    type: str

tls:
    default: false
    description:
    - Secure the connection to the API by using TLS without verifying the authenticity
      of the Docker host server. Note that if I(validate_certs) is set to C(yes) as well,
      it will take precedence.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_TLS)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: bool

name:
    description:
    - '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''.'
    - Note that image IDs (hashes) are not supported.
    required: true
    type: str

path:
    aliases:
    - build_path
    description:
    - Use with state 'present' to build an image. Will be the path to a directory containing
      the context and Dockerfile for building an image.
    - Set I(source) to C(build) if you want to build the image. The option will be set
      automatically before Ansible 2.12 if this option is used. From Ansible 2.12 on,
      you have to set I(source) to C(build).
    - Please use I(build.path) instead. This option will be removed in Ansible 2.12.
    type: path

pull:
    description:
    - When building an image downloads any updates to the FROM image in Dockerfile.
    - Please use I(build.pull) instead. This option will be removed in Ansible 2.12.
    - The default is currently C(yes). This will change to C(no) in Ansible 2.12.
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

push:
    default: false
    description:
    - Push the image to the registry. Specify the registry as part of the I(name) or I(repository)
      parameter.
    type: bool
    version_added: '2.2'
    version_added_collection: ansible.builtin

build:
    description:
    - Specifies options used for building images.
    suboptions:
      args:
        description:
        - Provide a dictionary of C(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.
        type: dict
      cache_from:
        description:
        - List of image names to consider as cache source.
        elements: str
        type: list
      container_limits:
        description:
        - A dictionary of limits applied to each container created by the build process.
        suboptions:
          cpusetcpus:
            description:
            - CPUs in which to allow execution, e.g., "0-3", "0,1".
            type: str
          cpushares:
            description:
            - CPU shares (relative weight).
            type: int
          memory:
            description:
            - Set memory limit for build.
            type: int
          memswap:
            description:
            - Total memory (memory + swap), -1 to disable swap.
            type: int
        type: dict
      dockerfile:
        description:
        - Use with state C(present) and source C(build) to provide an alternate name for
          the Dockerfile to use when building an image.
        - This can also include a relative path (relative to I(path)).
        type: str
      etc_hosts:
        description:
        - Extra hosts to add to C(/etc/hosts) in building containers, as a mapping of
          hostname to IP address.
        type: dict
        version_added: '2.9'
        version_added_collection: ansible.builtin
      http_timeout:
        description:
        - Timeout for HTTP requests during the image build operation. Provide a positive
          integer value for the number of seconds.
        type: int
      network:
        description:
        - The network to use for C(RUN) build instructions.
        type: str
      nocache:
        default: false
        description:
        - Do not use cache when building an image.
        type: bool
      path:
        description:
        - Use with state 'present' to build an image. Will be the path to a directory
          containing the context and Dockerfile for building an image.
        required: true
        type: path
      pull:
        description:
        - When building an image downloads any updates to the FROM image in Dockerfile.
        - The default is currently C(yes). This will change to C(no) in Ansible 2.12.
        type: bool
      rm:
        default: true
        description:
        - Remove intermediate containers after build.
        type: bool
      target:
        description:
        - When building an image specifies an intermediate build stage by name as a final
          stage for the resulting image.
        type: str
        version_added: '2.9'
        version_added_collection: ansible.builtin
      use_config_proxy:
        description:
        - If set to C(yes) and a proxy configuration is specified in the docker client
          configuration (by default C($HOME/.docker/config.json)), the corresponding environment
          variables will be set in the container being built.
        - Needs Docker SDK for Python >= 3.7.0.
        type: bool
    type: dict
    version_added: '2.8'
    version_added_collection: ansible.builtin

debug:
    default: false
    description:
    - Debug mode
    type: bool

force:
    description:
    - Use with state I(absent) to un-tag and remove all images matching the specified
      name. Use with state C(present) to build, load or pull an image when the image already
      exists. Also use with state C(present) to force tagging an image.
    - Please stop using this option, and use the more specialized force options I(force_source),
      I(force_absent) and I(force_tag) instead.
    - This option will be removed in Ansible 2.12.
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

state:
    choices:
    - absent
    - present
    - build
    default: present
    description:
    - Make assertions about the state of an image.
    - When C(absent) an image will be removed. Use the force option to un-tag and remove
      all images matching the provided name.
    - When C(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, depending on the I(source) option.
    - By default the image will be pulled from Docker Hub, or the registry specified in
      the image's name. Note that this will change in Ansible 2.12, so to make sure that
      you are pulling, set I(source) to C(pull). To build the image, provide a I(path)
      value set to a directory containing a context and Dockerfile, and set I(source)
      to C(build). To load an image, specify I(load_path) to provide a path to an archive
      file. To tag an image to a repository, provide a I(repository) path. If the name
      contains a repository path, it will be pushed.
    - '*Note:* C(state=build) is DEPRECATED and will be removed in Ansible 2.11. Specifying
      C(build) will behave the same as C(present).'
    type: str

source:
    choices:
    - build
    - load
    - pull
    - local
    description:
    - Determines where the module will try to retrieve the image from.
    - Use C(build) to build the image from a C(Dockerfile). I(build.path) must be specified
      when this value is used.
    - Use C(load) to load the image from a C(.tar) file. I(load_path) must be specified
      when this value is used.
    - Use C(pull) to pull the image from a registry.
    - Use C(local) to make sure that the image is already available on the local docker
      daemon, i.e. do not try to build, pull or load the image.
    - Before Ansible 2.12, the value of this option will be auto-detected to be backwards
      compatible, but a warning will be issued if it is not explicitly specified. From
      Ansible 2.12 on, auto-detection will be disabled and this option will be made mandatory.
    type: str
    version_added: '2.8'
    version_added_collection: ansible.builtin

ca_cert:
    aliases:
    - tls_ca_cert
    - cacert_path
    description:
    - 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 C(DOCKER_CERT_PATH)
      is set, the file C(ca.pem) from the directory specified in the environment variable
      C(DOCKER_CERT_PATH) will be used.
    type: path

nocache:
    default: false
    description:
    - Do not use cache when building an image.
    - Please use I(build.nocache) instead. This option will be removed in Ansible 2.12.
    type: bool

timeout:
    default: 60
    description:
    - 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 C(DOCKER_TIMEOUT)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: int

use_tls:
    choices:
    - 'no'
    - encrypt
    - verify
    description:
    - DEPRECATED. Whether to use tls to connect to the docker daemon. Set to C(encrypt)
      to use TLS. And set to C(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 I(tls) or I(validate_certs)
      parameters if not set to C(no).'
    - Will be removed in Ansible 2.11.
    type: str
    version_added: '2.0'
    version_added_collection: ansible.builtin

buildargs:
    description:
    - Provide a dictionary of C(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.
    - Please use I(build.args) instead. This option will be removed in Ansible 2.12.
    type: dict
    version_added: '2.2'
    version_added_collection: ansible.builtin

force_tag:
    default: false
    description:
    - Use with state C(present) to force tagging an image.
    type: bool
    version_added: '2.8'
    version_added_collection: ansible.builtin

load_path:
    description:
    - Use with state C(present) to load an image from a .tar file.
    - Set I(source) to C(load) if you want to load the image. The option will be set automatically
      before Ansible 2.12 if this option is used (except if I(path) is specified as well,
      in which case building will take precedence). From Ansible 2.12 on, you have to
      set I(source) to C(load).
    type: path
    version_added: '2.2'
    version_added_collection: ansible.builtin

client_key:
    aliases:
    - tls_client_key
    - key_path
    description:
    - Path to the client's TLS key file.
    - If the value is not specified in the task and the environment variable C(DOCKER_CERT_PATH)
      is set, the file C(key.pem) from the directory specified in the environment variable
      C(DOCKER_CERT_PATH) will be used.
    type: path

dockerfile:
    description:
    - Use with state C(present) and source C(build) to provide an alternate name for the
      Dockerfile to use when building an image.
    - This can also include a relative path (relative to I(path)).
    - Please use I(build.dockerfile) instead. This option will be removed in Ansible 2.12.
    type: str
    version_added: '2.0'
    version_added_collection: ansible.builtin

repository:
    description:
    - Full path to a repository. Use with state C(present) to tag the image into the repository.
      Expects format I(repository:tag). If no tag is provided, will use the value of the
      C(tag) parameter or I(latest).
    type: str
    version_added: '2.1'
    version_added_collection: ansible.builtin

api_version:
    aliases:
    - docker_api_version
    default: auto
    description:
    - The version of the Docker API running on the Docker Host.
    - Defaults to the latest version of the API supported by Docker SDK for Python and
      the docker daemon.
    - If the value is not specified in the task, the value of environment variable C(DOCKER_API_VERSION)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: str

client_cert:
    aliases:
    - tls_client_cert
    - cert_path
    description:
    - Path to the client's TLS certificate file.
    - If the value is not specified in the task and the environment variable C(DOCKER_CERT_PATH)
      is set, the file C(cert.pem) from the directory specified in the environment variable
      C(DOCKER_CERT_PATH) will be used.
    type: path

docker_host:
    aliases:
    - docker_url
    default: unix://var/run/docker.sock
    description:
    - 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, C(tcp://192.0.2.23:2376).
      If TLS is used to encrypt the connection, the module will automatically replace
      C(tcp) in the connection URL with C(https).
    - If the value is not specified in the task, the value of environment variable C(DOCKER_HOST)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: str

ssl_version:
    description:
    - 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 C(DOCKER_SSL_VERSION)
      will be used instead.
    type: str

archive_path:
    description:
    - Use with state C(present) to archive an image to a .tar file.
    type: path
    version_added: '2.1'
    version_added_collection: ansible.builtin

force_absent:
    default: false
    description:
    - Use with state I(absent) to un-tag and remove all images matching the specified
      name.
    type: bool
    version_added: '2.8'
    version_added_collection: ansible.builtin

force_source:
    default: false
    description:
    - Use with state C(present) to build, load or pull an image (depending on the value
      of the I(source) option) when the image already exists.
    type: bool
    version_added: '2.8'
    version_added_collection: ansible.builtin

http_timeout:
    description:
    - Timeout for HTTP requests during the image build operation. Provide a positive integer
      value for the number of seconds.
    - Please use I(build.http_timeout) instead. This option will be removed in Ansible
      2.12.
    type: int
    version_added: '2.1'
    version_added_collection: ansible.builtin

tls_hostname:
    default: localhost
    description:
    - 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 C(DOCKER_TLS_HOSTNAME)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: str

use_ssh_client:
    default: false
    description:
    - For SSH transports, use the C(ssh) CLI tool instead of paramiko.
    - Requires Docker SDK for Python 4.4.0 or newer.
    type: bool
    version_added: 1.5.0
    version_added_collection: community.docker

validate_certs:
    aliases:
    - tls_verify
    default: false
    description:
    - 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 C(DOCKER_TLS_VERIFY)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: bool

container_limits:
    description:
    - A dictionary of limits applied to each container created by the build process.
    - Please use I(build.container_limits) instead. This option will be removed in Ansible
      2.12.
    suboptions:
      cpusetcpus:
        description:
        - CPUs in which to allow execution, e.g., "0-3", "0,1".
        type: str
      cpushares:
        description:
        - CPU shares (relative weight).
        type: int
      memory:
        description:
        - Set memory limit for build.
        type: int
      memswap:
        description:
        - Total memory (memory + swap), -1 to disable swap.
        type: int
    type: dict
    version_added: '2.1'
    version_added_collection: ansible.builtin

Outputs

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