Docker Tutorial

Docker Installation

How to use Docker

Docker Instance

Docker Container Lifecycle Command

Docker Container Operation Command

Docker Container rootfs Command

Docker Mirror Repository Command

Docker Local Image Management Command

Docker info|version Command

Docker image usage

The docker image command is a management command for Docker images, providing various subcommands to list, remove, inspect, and perform other operations on images. This tutorial will guide you through the basics of using the docker image command and its subcommands.

  1. Prerequisites:

    • Install Docker on your system
    • Have at least one image on your system. If you don't have any images, you can pull an example image using the following command:
    docker pull ubuntu:20.04
    

    This command will download the ubuntu:20.04 image from Docker Hub.

  2. List Docker images:

    To display a list of Docker images on your system, run:

    docker image ls
    

    This will display a table with the same columns as the docker images command: REPOSITORY, TAG, IMAGE ID, CREATED, and SIZE.

  3. Remove Docker images:

    To remove an image from your system, use the docker image rm or docker rmi command:

    docker image rm IMAGE
    

    Replace IMAGE with the image ID, repository name, or repository name with tag.

    For example, to remove the ubuntu:20.04 image, use:

    docker image rm ubuntu:20.04
    
  4. Inspect Docker images:

    To inspect an image and display its metadata and configuration in JSON format, use the docker image inspect command:

    docker image inspect IMAGE
    

    Replace IMAGE with the image ID, repository name, or repository name with tag.

    For example, to inspect the ubuntu:20.04 image, use:

    docker image inspect ubuntu:20.04
    
  5. Display image history:

    To display the history of an image, use the docker image history command:

    docker image history IMAGE
    

    Replace IMAGE with the image ID, repository name, or repository name with tag.

    For example, to display the history of the ubuntu:20.04 image, use:

    docker image history ubuntu:20.04
    
  6. Pull Docker images:

    To pull an image from a registry, use the docker image pull command:

    docker image pull REPOSITORY[:TAG]
    

    Replace REPOSITORY with the image repository name and TAG with the desired image tag. If no tag is provided, Docker will pull the "latest" tag by default.

    For example, to pull the alpine:3.14 image, use:

    docker image pull alpine:3.14
    
  7. Push Docker images:

    To push an image to a registry, use the docker image push command:

    docker image push REPOSITORY[:TAG]
    

    Replace REPOSITORY with the image repository name and TAG with the desired image tag. You need to be logged in to the registry using docker login before you can push images.

    For example, to push the myrepo/myimage:1.0 image to Docker Hub, use:

    docker image push myrepo/myimage:1.0
    

In this tutorial, we covered the basics of using the docker image command and its subcommands for managing Docker images. These commands offer a consistent interface for working with images, from listing and inspecting to pulling and pushing images to registries.

  1. How to Use Docker Images:

    • Description: The docker images command lists the Docker images available on your local machine, providing information about repositories, tags, and sizes.
    • Code Example:
      docker images
      
  2. Docker Image Pull and Push Commands:

    • Description: Use docker pull to download images from a registry, and docker push to upload images to a registry.
    • Code Example:
      docker pull ubuntu:latest
      docker push myrepository/myimage:tag
      
  3. Tagging and Versioning Docker Images:

    • Description: Tagging helps identify specific versions of an image. It's essential for versioning and maintaining a clear image history.
    • Code Example:
      docker tag myimage:latest myimage:v1.0
      
  4. Optimizing Docker Image Size:

    • Description: Optimize image size by minimizing layers, removing unnecessary files, and using smaller base images.
    • Code Example:
      FROM alpine:latest
      RUN apk --no-cache add my-dependency