community.docker.docker_image (3.8.1) — module

Manage docker images

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

Install collection

Install with ansible-galaxy collection install community.docker:==3.8.1


Add to requirements.yml

  collections:
    - name: community.docker
      version: 3.8.1

Description

Build, load or pull an image, making the image available for creating containers. Also supports tagging an image, pushing an image, and archiving an image to a C(.tar) file.


Requirements

Usage examples

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

- name: Pull an image
  community.docker.docker_image:
    name: pacur/centos-7
    source: pull
    # Select platform for pulling. If not specified, will pull whatever docker prefers.
    pull:
      platform: amd64
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Tag and push to docker hub
  community.docker.docker_image:
    name: pacur/centos-7:56
    repository: dcoppenhagan/myimage:7.56
    push: true
    source: local
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Tag and push to local registry
  community.docker.docker_image:
    # Image will be centos:7
    name: centos
    # Will be pushed to localhost:5000/centos:7
    repository: localhost:5000/centos
    tag: 7
    push: true
    source: local
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Add tag latest to image
  community.docker.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: true
    source: local
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove image
  community.docker.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
  community.docker.docker_image:
    build:
      path: ./sinatra
    name: registry.ansible.com/chouseknecht/sinatra
    tag: v1
    push: true
    source: build
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Archive image
  community.docker.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
  community.docker.docker_image:
    name: localhost:5000/myimages/sinatra
    tag: v1
    push: true
    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
  community.docker.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
  community.docker.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

    
tag:
    default: latest
    description:
    - Used to select an image when pulling. Will be added to the image when pushing, tagging
      or building. Defaults to V(latest).
    - If O(name) parameter format is C(name:tag), then tag value from O(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 O(validate_certs) is set to V(true) as well,
      it will take precedence.
    - If the value is not specified in the task, the value of environment variable E(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: C(name), C(repository/name), C(registry_server:port/name).
      When pushing or pulling an image the name can optionally include the tag by appending
      C(:tag_name).'
    - Note that image IDs (hashes) are only supported for O(state=absent), for O(state=present)
      with O(source=load), and for O(state=present) with O(source=local).
    required: true
    type: str

pull:
    description:
    - Specifies options used for pulling images.
    suboptions:
      platform:
        description:
        - When pulling an image, ask for this specific platform.
        - Note that this value is not used to determine whether the image needs to be
          pulled. This might change in the future in a minor release, though.
        type: str
    type: dict
    version_added: 1.3.0
    version_added_collection: community.docker

push:
    default: false
    description:
    - Push the image to the registry. Specify the registry as part of the O(name) or O(repository)
      parameter.
    type: bool

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.
        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.
            - For example, V(0-3) or V(0,1).
            type: str
          cpushares:
            description:
            - CPU shares (relative weight).
            type: int
          memory:
            description:
            - Memory limit for build in format C(<number>[<unit>]). Number is a positive
              integer. Unit can be V(B) (byte), V(K) (kibibyte, 1024B), V(M) (mebibyte),
              V(G) (gibibyte), V(T) (tebibyte), or V(P) (pebibyte).
            - Omitting the unit defaults to bytes.
            - Before community.docker 3.6.0, no units were allowed.
            type: str
          memswap:
            description:
            - Total memory limit (memory + swap) for build in format C(<number>[<unit>]),
              or the special values V(unlimited) or V(-1) for unlimited swap usage. Number
              is a positive integer. Unit can be V(B) (byte), V(K) (kibibyte, 1024B),
              V(M) (mebibyte), V(G) (gibibyte), V(T) (tebibyte), or V(P) (pebibyte).
            - Omitting the unit defaults to bytes.
            - Before community.docker 3.6.0, no units were allowed, and neither was the
              special value V(unlimited).
            type: str
        type: dict
      dockerfile:
        description:
        - Use with O(state=present) and O(source=build) to provide an alternate name for
          the Dockerfile to use when building an image.
        - This can also include a relative path (relative to O(build.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
      http_timeout:
        description:
        - Timeout for HTTP requests during the image build operation. Provide a positive
          integer value for the number of seconds.
        type: int
      labels:
        description:
        - Dictionary of key value pairs.
        type: dict
        version_added: 3.6.0
        version_added_collection: community.docker
      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
      platform:
        description:
        - Platform in the format C(os[/arch[/variant]]).
        type: str
        version_added: 1.1.0
        version_added_collection: community.docker
      pull:
        default: false
        description:
        - When building an image downloads any updates to the FROM image in Dockerfile.
        type: bool
      rm:
        default: true
        description:
        - Remove intermediate containers after build.
        type: bool
      shm_size:
        description:
        - Size of C(/dev/shm) in format C(<number>[<unit>]). Number is positive integer.
          Unit can be V(B) (byte), V(K) (kibibyte, 1024B), V(M) (mebibyte), V(G) (gibibyte),
          V(T) (tebibyte), or V(P) (pebibyte).
        - Omitting the unit defaults to bytes. If you omit the size entirely, Docker daemon
          uses V(64M).
        type: str
        version_added: 3.6.0
        version_added_collection: community.docker
      target:
        description:
        - When building an image specifies an intermediate build stage by name as a final
          stage for the resulting image.
        type: str
      use_config_proxy:
        description:
        - If set to V(true) 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.
        type: bool
    type: dict

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

state:
    choices:
    - absent
    - present
    default: present
    description:
    - Make assertions about the state of an image.
    - When V(absent) an image will be removed. Use the force option to un-tag and remove
      all images matching the provided name.
    - When V(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 O(source) option.
    type: str

source:
    choices:
    - build
    - load
    - pull
    - local
    description:
    - Determines where the module will try to retrieve the image from.
    - Use V(build) to build the image from a C(Dockerfile). O(build.path) must be specified
      when this value is used.
    - Use V(load) to load the image from a C(.tar) file. O(load_path) must be specified
      when this value is used.
    - Use V(pull) to pull the image from a registry.
    - Use V(local) to make sure that the image is already available on the local docker
      daemon. This means that the module does not try to build, pull or load the image.
    type: str

ca_path:
    aliases:
    - ca_cert
    - 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 E(DOCKER_CERT_PATH)
      is set, the file C(ca.pem) from the directory specified in the environment variable
      E(DOCKER_CERT_PATH) will be used.
    - This option was called O(ca_cert) and got renamed to O(ca_path) in community.docker
      3.6.0. The old name has been added as an alias and can still be used.
    type: path

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

force_tag:
    default: false
    description:
    - Use with O(state=present) to force tagging an image.
    type: bool

load_path:
    description:
    - Use with O(state=present) to load an image from a C(.tar) file.
    - Set O(source=load) if you want to load the image.
    type: path

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 E(DOCKER_CERT_PATH)
      is set, the file C(key.pem) from the directory specified in the environment variable
      E(DOCKER_CERT_PATH) will be used.
    type: path

repository:
    description:
    - Use with O(state=present) to tag the image.
    - Expects format C(repository:tag). If no tag is provided, will use the value of the
      O(tag) parameter or V(latest).
    - If O(push=true), O(repository) must either include a registry, or will be assumed
      to belong to the default registry (Docker Hub).
    type: str

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 this collection and the docker
      daemon.
    - If the value is not specified in the task, the value of environment variable E(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 E(DOCKER_CERT_PATH)
      is set, the file C(cert.pem) from the directory specified in the environment variable
      E(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, V(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 E(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 L(SSL Python module,
      https://docs.python.org/3/library/ssl.html).
    - If the value is not specified in the task, the value of environment variable E(DOCKER_SSL_VERSION)
      will be used instead.
    type: str

archive_path:
    description:
    - Use with O(state=present) to archive an image to a C(.tar) file.
    type: path

force_absent:
    default: false
    description:
    - Use with O(state=absent) to un-tag and remove all images matching the specified
      name.
    type: bool

force_source:
    default: false
    description:
    - Use with O(state=present) to build, load or pull an image (depending on the value
      of the O(source) option) when the image already exists.
    type: bool

tls_hostname:
    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 E(DOCKER_TLS_HOSTNAME)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    - Note that this option had a default value V(localhost) in older versions. It was
      removed in community.docker 3.0.0.
    type: str

use_ssh_client:
    default: false
    description:
    - For SSH transports, use the C(ssh) CLI tool instead of paramiko.
    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 E(DOCKER_TLS_VERIFY)
      will be used instead. If the environment variable is not set, the default value
      will be used.
    type: bool

Outputs

image:
  description: Image inspection results for the affected image.
  returned: success
  sample: {}
  type: dict
stdout:
  description: Docker build output when building an image.
  returned: success
  sample: ''
  type: str
  version_added: 1.0.0
  version_added_collection: community.docker

See also