Bootstrap FreeKB - Ansible - Get info on Docker image using the docker_image_info module
Ansible - Get info on Docker image using the docker_image_info module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

Before you can use the docker_container module, the Python docker module must be installed on the managed node, which can be done using PIP. The dnf module can be used to install PIP and then the pip module can be used to install the Python docker module.

---
- hosts: all
  tasks:
  - name: install pip
    dnf:
      name: pip
      state: present

  - name: pip install docker
    pip:
      name: docker
      state: latest
...

 

Here is how the docker_image_info module could be used to get info on the "nginx" container. This can be very useful in determining if the nginx container exists.

---
- hosts: all
  tasks:
  - name: get info on Docker image
    docker_image_info:
      name: my_image
    register: out

  - debug:
      var: out
...

 

Something like this should be returned.

ok: [local_docker_1] => {
    "out": {
        "changed": false,
        "failed": false,
        "images": [
            {
                "Architecture": "amd64",
                "Author": "",
                "Comment": "",
                "Config": {
                    "AttachStderr": false,
                    "AttachStdin": false,
                    "AttachStdout": false,
                    "Cmd": [
                        "/start.sh"
                    ],
                    "Domainname": "",
                    "Entrypoint": [
                        "/entrypoint.sh"
                    ],
                    "Env": [
                        "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "LANG=C.UTF-8",
                        "GPG_KEY=A035C8C19219BA821ECEA86B64E628F8D684696D",
                        "PYTHON_VERSION=3.11.1",
                        "PYTHON_PIP_VERSION=22.3.1",
                        "PYTHON_SETUPTOOLS_VERSION=65.5.1",
                        "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/66030fa03382b4914d4c4d0896961a0bdeeeb274/public/get-pip.py",
                        "PYTHON_GET_PIP_SHA256=1e501cf004eac1b7eb1f97266d28f995ae835d30250bec7f8850562703067dc6",
                        "UWSGI_INI=/app/uwsgi.ini",
                        "UWSGI_CHEAPER=2",
                        "UWSGI_PROCESSES=16",
                        "NGINX_MAX_UPLOAD=0",
                        "NGINX_WORKER_PROCESSES=1",
                        "LISTEN_PORT=80",
                        "STATIC_URL=/static",
                        "STATIC_PATH=/app/static",
                        "STATIC_INDEX=0",
                        "PYTHONPATH=/app"
                    ],
                    "ExposedPorts": {
                        "443/tcp": {},
                        "80/tcp": {}
                    },
                    "Hostname": "",
                    "Image": "sha256:da6df49973f2ed222b249db115fb5e2733164eecb4988b9ccdf9e48a1f0b208b",
                    "Labels": {
                        "maintainer": "Sebastian Ramirez <tiangolo@gmail.com>"
                    },
                    "OnBuild": null,
                    "OpenStdin": false,
                    "StdinOnce": false,
                    "Tty": false,
                    "User": "",
                    "Volumes": null,
                    "WorkingDir": "/app"
                },
                "Container": "a57125457d6f6cb95b09c84287ff02338c4cf276b0f6288ac47613aaf96ff426",
                "ContainerConfig": {
                    "AttachStderr": false,
                    "AttachStdin": false,
                    "AttachStdout": false,
                    "Cmd": [
                        "/bin/sh",
                        "-c",
                        "pip install -r /usr/requirements.txt"
                    ],
                    "Domainname": "",
                    "Entrypoint": null,
                    "Env": [
                        "PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                        "LANG=C.UTF-8",
                        "GPG_KEY=A035C8C19219BA821ECEA86B64E628F8D684696D",
                        "PYTHON_VERSION=3.11.1",
                        "PYTHON_PIP_VERSION=22.3.1",
                        "PYTHON_SETUPTOOLS_VERSION=65.5.1",
                        "PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/66030fa03382b4914d4c4d0896961a0bdeeeb274/public/get-pip.py",
                        "PYTHON_GET_PIP_SHA256=1e501cf004eac1b7eb1f97266d28f995ae835d30250bec7f8850562703067dc6",
                        "UWSGI_INI=/app/uwsgi.ini",
                        "UWSGI_CHEAPER=2",
                        "UWSGI_PROCESSES=16",
                        "NGINX_MAX_UPLOAD=0",
                        "NGINX_WORKER_PROCESSES=1",
                        "LISTEN_PORT=80",
                        "STATIC_URL=/static",
                        "STATIC_PATH=/app/static",
                        "STATIC_INDEX=0",
                        "PYTHONPATH=/app"
                    ],
                    "ExposedPorts": {
                        "443/tcp": {},
                        "80/tcp": {}
                    },
                    "Hostname": "",
                    "Image": "sha256:da6df49973f2ed222b249db115fb5e2733164eecb4988b9ccdf9e48a1f0b208b",
                    "Labels": {
                        "maintainer": "Sebastian Ramirez <tiangolo@gmail.com>"
                    },
                    "OnBuild": null,
                    "OpenStdin": false,
                    "StdinOnce": false,
                    "Tty": false,
                    "User": "",
                    "Volumes": null,
                    "WorkingDir": "/app"
                },
                "Created": "2023-06-23T23:17:11.729054936Z",
                "DockerVersion": "20.10.16",
                "GraphDriver": {
                    "Data": {
                        "LowerDir": "/var/lib/docker/overlay2/e406aaec913177815309ba6f065596470702cf4d253a5315ed484cca934824e4/diff",
                        "MergedDir": "/var/lib/docker/overlay2/1942ab2bd33759fb3d5627fabe081efce0221a61a81bbd6e4be71926bcbf2f85/merged",
                        "UpperDir": "/var/lib/docker/overlay2/1942ab2bd33759fb3d5627fabe081efce0221a61a81bbd6e4be71926bcbf2f85/diff",
                        "WorkDir": "/var/lib/docker/overlay2/1942ab2bd33759fb3d5627fabe081efce0221a61a81bbd6e4be71926bcbf2f85/work"
                    },
                    "Name": "overlay2"
                },
                "Id": "sha256:6320c703668ef733312926345e85274708b38e4b8b8fcbe90ed6259e4a59be9d",
                "Metadata": {
                    "LastTagTime": "2023-06-23T18:17:11.911734883-05:00"
                },
                "Os": "linux",
                "Parent": "sha256:da6df49973f2ed222b249db115fb5e2733164eecb4988b9ccdf9e48a1f0b208b",
                "RepoDigests": [],
                "RepoTags": [
                    "my-image:latest"
                ],
                "RootFS": {
                    "Layers": [
                        "sha256:740f4b89a0a1f7eb32c0b417914a34cd81e28db4b3a41de18a49ab8f3908bd6e"
                    ],
                    "Type": "layers"
                },
                "Size": 1036334012,
                "VirtualSize": 1036334012
            }
        ]
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter fffc78 in the box below so that we can be sure you are a human.