Sunday 18 December 2022

Exploring Docker commands: docker image




image is a management command. To get a list of all its commands we can run 

$ docker image 

or

$ docker image --help

Usage:  docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.


---

docker image history

% docker image history --help

Usage:  docker image history [OPTIONS] IMAGE

Show the history of an image

Options:
      --format string   Pretty-print images using a Go template
  -H, --human           Print sizes and dates in human readable format (default true)
      --no-trunc        Don't truncate output
  -q, --quiet           Only show image IDs

Shows the history of the image. This is actually a list of image's layers, and the commands used to create them.

$ docker image history <image_name>:<image_tag>

If image_tag is omitted, latest is used by default.

Another format:

$ docker image history <image_id>


This command is basically reconstructing image's Dockerfile and therefore can be used to find what is this Docker image going to run, what is its base image, what commands make the image large etc...


Example:

% docker history mysql
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
7484689f290f   11 days ago   /bin/sh -c #(nop)  CMD ["mysqld"]               0B        
<missing>      11 days ago   /bin/sh -c #(nop)  EXPOSE 3306 33060            0B        
<missing>      11 days ago   /bin/sh -c #(nop)  ENTRYPOINT ["docker-entry…   0B        
<missing>      11 days ago   /bin/sh -c ln -s usr/local/bin/docker-entryp…   34B       
<missing>      11 days ago   /bin/sh -c #(nop) COPY file:e9c22353a1133b89…   14.2kB    
<missing>      11 days ago   /bin/sh -c #(nop)  VOLUME [/var/lib/mysql]      0B        
<missing>      11 days ago   /bin/sh -c set -eux;  microdnf install -y "m…   237MB     
<missing>      11 days ago   /bin/sh -c #(nop)  ENV MYSQL_SHELL_VERSION=8…   0B        
<missing>      11 days ago   /bin/sh -c set -eu;  . /etc/os-release;  {  …   215B      
<missing>      11 days ago   /bin/sh -c set -eux;  microdnf install -y "m…   178MB     
<missing>      11 days ago   /bin/sh -c set -eu;  . /etc/os-release;  {  …   225B      
<missing>      11 days ago   /bin/sh -c #(nop)  ENV MYSQL_VERSION=8.0.31-…   0B        
<missing>      11 days ago   /bin/sh -c #(nop)  ENV MYSQL_MAJOR=8.0          0B        
<missing>      11 days ago   /bin/sh -c set -eux;  key='859BE8D7C586F5384…   3.17kB    
<missing>      11 days ago   /bin/sh -c set -eux;  microdnf install -y   …   13.6MB    
<missing>      11 days ago   /bin/sh -c set -eux;  arch="$(uname -m)";  c…   2.29MB    
<missing>      11 days ago   /bin/sh -c #(nop)  ENV GOSU_VERSION=1.14        0B        
<missing>      11 days ago   /bin/sh -c set -eux;  groupadd --system --gi…   2.77kB    
<missing>      11 days ago   /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B        
<missing>      11 days ago   /bin/sh -c #(nop) ADD file:04d9ae26c2acac414…   106MB   

To suppress truncating of the commands, we can use --no-trunc:

% docker history --no-trunc mysql

% docker history --no-trunc mysql | grep ENTRYPOINT
<missing>                                                                 11 days ago   /bin/sh -c #(nop)  ENTRYPOINT ["docker-entrypoint.sh"]                             

docker image inspect

% docker image inspect --help

Usage:  docker image inspect [OPTIONS] IMAGE [IMAGE...]

Display detailed information on one or more images

Options:
  -f, --format string   Format the output using the given Go template

We can use this command to see:
  • which ports will be exposed in container
  • which environment variables will be set in container
  • volumes
  • command/entrypoint - what will get executed when we run the container
  • OS and architecture

Example:

% docker image inspect mysql
[
    {
        "Id": "sha256:7484689f290f1defe06b65befc54cb6ad91a667cf0af59a265ffe76c46bd0478",
        "RepoTags": [
            "mysql:latest"
        ],
        "RepoDigests": [
            "mysql@sha256:3d7ae561cf6095f6aca8eb7830e1d14734227b1fb4748092f2be2cfbccf7d614"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2022-12-07T02:23:31.34588089Z",
        "Container": "39e37396c270e5234472eb14feb4abbdc8212ac30b984481f79756836a2926eb",
        "ContainerConfig": {
            "Hostname": "39e37396c270",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.14",
                "MYSQL_MAJOR=8.0",
                "MYSQL_VERSION=8.0.31-1.el8",
                "MYSQL_SHELL_VERSION=8.0.31-1.el8"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"mysqld\"]"
            ],
            "Image": "sha256:38354afaf9d5d657e0608e62e7ad59f6b8746d3d6c1776c956f250034041626a",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "DockerVersion": "20.10.12",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "3306/tcp": {},
                "33060/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.14",
                "MYSQL_MAJOR=8.0",
                "MYSQL_VERSION=8.0.31-1.el8",
                "MYSQL_SHELL_VERSION=8.0.31-1.el8"
            ],
            "Cmd": [
                "mysqld"
            ],
            "Image": "sha256:38354afaf9d5d657e0608e62e7ad59f6b8746d3d6c1776c956f250034041626a",
            "Volumes": {
                "/var/lib/mysql": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": null,
            "Labels": null
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 538012982,
        "VirtualSize": 538012982,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/166118dbdfd603f6c6a005d36be4ba4d51ef997a6b50d70224208a266f70090e/diff:/var/lib/docker/overlay2/5c6d7d8a1b25af02b5c2bbbe65aceb827e65607d025118f6727c52bd8f7bbc3c/diff:/var/lib/docker/overlay2/8c86f30101e519ae95fff720288754c0685cb000b488243bbca96ad12359001c/diff:/var/lib/docker/overlay2/2b6a8383ab2345fd999f30b75b20ea3e54c96fb13b6c03b0499dfd9fac723bfb/diff:/var/lib/docker/overlay2/493bbbc0b022918b2d463513851deb0357ce094b598a2bb6dc76d4e34be33c19/diff:/var/lib/docker/overlay2/867b29054d700c0d71574adb91e1c28c145082cad242b9ab627c88fec963a8e3/diff:/var/lib/docker/overlay2/04bfc900db8627bc6b346259a0273019508bb7369d573b85a835e42dd3483aa2/diff:/var/lib/docker/overlay2/76f0b8fcc14c3e25dc9470c61f7c2f91324246082c41f408dd39af9725a8a42d/diff:/var/lib/docker/overlay2/33ea513815166ece2e9548d960e5aae3c0ca7acd1ba58cc4143142ff24d6cb72/diff:/var/lib/docker/overlay2/1b45b35a8d681bd00e5c3c59e6353a01267ffc658fa9b173d8a68e957416de10/diff",
                "MergedDir": "/var/lib/docker/overlay2/2cf0a924d050e57b34a1a8c5548a48cfb7c2303c156c7c917df0a53e386a2305/merged",
                "UpperDir": "/var/lib/docker/overlay2/2cf0a924d050e57b34a1a8c5548a48cfb7c2303c156c7c917df0a53e386a2305/diff",
                "WorkDir": "/var/lib/docker/overlay2/2cf0a924d050e57b34a1a8c5548a48cfb7c2303c156c7c917df0a53e386a2305/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:d3cc7b6aa7bc15725c1a856ce06fe436da3fbccf0c9c06b04e45f79b3439c154",
                "sha256:a7f421510691bf6a7b344d1efb738b3d343e252e7dde114a0dd86d432ef6000c",
                "sha256:6ac2db160c6cf3dcf1aff0ced069aa98da28c50cff5cd3c8881c04f42e3ef1fe",
                "sha256:7fe65049a2a940ab927d3f5b2cf0687ecffbdf9d7e9df1daaeddb83bc601f3cb",
                "sha256:da1824686db37bbf1ffbffea53295aa853731531a14e70bca24eeb6d91fd6327",
                "sha256:d410d4efd0e75456011f265fa113b206dd4da9dccf5151bca714ef6c69a3b8cd",
                "sha256:60c4dab21dc337e912d518acc56e5b776e3de4f1d277d074831bb678089b87a6",
                "sha256:d00057f8969283fed84044f6103036e18a9d776579d705a85472535ba321df25",
                "sha256:2f42ce9d7b80a286af13410c0b64e94c90eca3e7597f7fd82a783aa1f68c2373",
                "sha256:8408fed6a9d685236cb024ceea39692743b6c52ea6c4c068b22a6475f742e24a",
                "sha256:336175ddf157a8f50c0aae8c0726b1462fd41f30f0b7f84caf6bf5cd02f8de77"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

docker image ls


$ docker image ls --help

Usage:  docker image ls [OPTIONS] [REPOSITORY[:TAG]]

List images

Aliases:
  ls, images, list

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs


To list all images:

$ docker image ls

or 

$ docker images


Example:

% docker images

REPOSITORY                   TAG             IMAGE ID       CREATED        SIZE

mysql                        latest          7484689f290f   11 days ago    538MB

in28min/hello-world-python   0.0.1.RELEASE   6d1dfe87a934   6 months ago   91MB


% docker image ls

REPOSITORY                   TAG             IMAGE ID       CREATED        SIZE

mysql                        latest          7484689f290f   11 days ago    538MB

in28min/hello-world-python   0.0.1.RELEASE   6d1dfe87a934   6 months ago   91MB



To list IDs of all images:

$ docker image ls -aq


docker image rm (docker image remove)

Removes an image from the local machine.

$ docker image rm --help

Usage:  docker image rm [OPTIONS] IMAGE [IMAGE...]

Remove one or more images

Aliases:
  rm, rmi, remove

Options:
  -f, --force      Force removal of the image
      --no-prune   Do not delete untagged parents


To remove some image:

docker image rm <image_id>

or

docker image rm <image_name>:<image_label>


To delete all images:

$ docker image rm $(docker image ls -aq)


Image can't be removed if it's referenced by some container. We first need to remove the container:

$ docker container rm <container_id>

---

No comments: