Docker Image

Nidhi Ashtikar
8 min readApr 22, 2024

--

We have already looked at Docker Containers, Today let’s see Docker Images.

What is Docker Images?

Docker images are like blueprints for containers. They contain everything needed to run a container, including the code, runtime, libraries, environment variables, and configuration files.

You can think of them as snapshots of a container’s filesystem.

Docker images are stored in repositories, which can be either public (like Docker Hub) or private (hosted on your own infrastructure or a private repository service). You can pull images from repositories using the docker pull command and push images to repositories using the docker push command.

To learn Docker File Click Here……

Docker Image Commands:

1. Building Images:

#docker build: Builds a Docker image from a Dockerfile.

docker build -t <image_name> <path_to_Dockerfile>
#Build an image from a Dockerfile in the current directory:

docker build .
#Build an image with a specific tag

docker build -t <image_name>:<tag> .
#Specify a Dockerfile path

docker build -f /path/to/Dockerfile .
#Build an image from a specific Git repository

docker build -t <image_name> https://github.com/username/repo.git
#Build an image from a specific Git branch

docker build -t <image_name> https://github.com/username/repo.git#branch_name
#Build an image with build-time arguments

docker build --build-arg <key>=<value> .
#Set a build context (directory containing the Dockerfile)

docker build -f /path/to/Dockerfile -t <image_name> /path/to/build/context
#Build an image with no cache (forcing a fresh build without using any cached layers)


docker build --no-cache .
#Build an image with a custom build context (specify a different directory to be used as the build context)

docker build -f /path/to/Dockerfile -t <image_name> - < /path/to/build/context
#Build an image using a Dockerfile from a URL

docker build -t <image_name> https://example.com/Dockerfile

2. Listing Images:

#List all images 

docker images
#List images with specific repository and tag

docker images <repository>:<tag>
#List images in quiet mode (display only image IDs)

docker images -q
#List images with their size

docker images --format "{{.Repository}}:{{.Tag}} Size: {{.Size}}"
#List images sorted by creation time

docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}\t{{.Size}}"
#List images with no truncation of output

docker images --format "{{json .}}"
#List dangling images (unreferenced images)

docker images -f dangling=true
#List images by name using grep\

docker images | grep <image_name>
#List images with specific name and tag using awk

docker images | awk '$1 == "<image_name>" && $2 == "<tag>"'

3. Pulling / Pushing Images:

#docker pull: Pulls an image from a registry (e.g. Docker Hub)
#Pull an image with the latest tag

docker pull <image_name>


#docker push: Pushes an image to a registry (e.g., Docker Hub)
#Push an image with the latest tag

docker push <image_name>
#Pull an image with a specific tag

docker pull <image_name>:<tag>

#Push an image with a specific tag:

docker push <image_name>:<tag>
#Pull multiple images at once

docker pull <image1> <image2> <image3>

#Push multiple images at once

docker push <image1> <image2> <image3>
#Pull an image from a different registry

docker pull <registry_url>/<image_name>:<tag>

#Push an image to a different registry

docker push <registry_url>/<image_name>:<tag>
#Pull an image using a specific authentication method

docker login <registry_url>
docker pull <image_name>

#Push an image with a specific authentication method

docker login <registry_url>
docker push <image_name>
#Pull an image with a specific architecture

docker pull --platform <architecture> <image_name>

#Push an image with a specific architecture

docker push --platform <architecture> <image_name>
#Pull an image with a specific OS

docker pull --platform <OS> <image_name>


#Push an image with a specific OS:

docker push --platform <OS> <image_name>
#Pull an image with a specific version (if supported by the registry)

docker pull <image_name>@<digest>

#Push an image with a specific version (if supported by the registry):

docker push <image_name>@<digest>
#Pull an image without extracting or running it

docker pull --quiet <image_name>

#Push an image without extracting or running it

docker push --quiet <image_name>
#Pull an image with additional verbose output

docker pull --verbose <image_name>

#Push an image with additional verbose output

docker push --verbose <image_name>

#provide additional information about the execution of the command.
#This might include details about the communication with the Docker registry,
#progress indicators for the download process,
#and other diagnostic messages.

4. Tagging Images:

#docker tag: Tags an existing image with a new name and/or tag

docker tag <source_image> <target_image>
#Tag an image with a new tag

docker tag <image_id> <new_image_name>:<new_tag>
#Tag an image with the latest tag

docker tag <image_id> <new_image_name>:latest
#Tag an image with a specific repository

docker tag <image_id> <repository>/<image_name>:<tag>
#Tag an image without specifying the image ID

docker tag <existing_image_name>:<existing_tag> <new_image_name>:<new_tag>
#Tag an image with a different registry

docker tag <image_id> <registry_url>/<new_image_name>:<tag>
#Tag an image with the same repository but different tag

docker tag <existing_image_name>:<existing_tag> <existing_image_name>:<new_tag>
#Tag an image with a specific architecture

docker tag --platform <architecture> <image_id> <new_image_name>:<new_tag>
#Tag an image with a specific OS

docker tag --platform <OS> <image_id> <new_image_name>:<new_tag>
#Tag an image with multiple tags

docker tag <image_id> <new_image_name>:<tag1> && docker tag <image_id> <new_image_name>:<tag2>

5. Removing Images:

#docker rmi: Removes one or more Docker images
#Remove a single image by name

docker rmi <image_name>

#Remove a single image by ID

docker rmi <image_id>
#Remove multiple images at once

docker rmi <image1> <image2> <image3>
#Remove all images

docker rmi $(docker images -q)
#Remove dangling (untagged) images

docker rmi $(docker images -f dangling=true -q)
#Force removal of an image (even if it is being used by containers)

docker rmi -f <image_name>
#Remove all images except for those used by containers

docker image prune -a

#pruning Unused Images

docker image prune
#Remove all images matching a certain pattern

docker rmi $(docker images <pattern> -q)
#Remove images based on a filter

docker rmi $(docker images --filter "before=<timestamp>" -q)
#Remove images with a specific tag

docker rmi $(docker images <image_name>:<tag> -q)

6. Inspecting Images:

docker inspect <image_name>

7. Searching for Images:

#Search for images by name:

docker search <search_term>

#Limit the number of search results:

docker search --limit <number_of_results> <search_term>

#Display only official images:

docker search --filter=is-official=true <search_term>

#Display only automated builds:
#Display only images with specific stars count:

docker search --filter=stars=3 <search_term>

#Display only images with specific minimum stars count:

docker search --filter=stars=3+ <search_term>

8. Exporting and Importing Images:

Exporting Images:

#Export a single image to a TAR archive:

docker save -o <output_path.tar> <image_name>

#Export multiple images to a TAR archive:

docker save -o <output_path.tar> <image_name1> <image_name2> ...

#Export all images to a TAR archive:

#docker save -o <output_path.tar> $(docker images -q)

Importing Images:

#Import an image from a TAR archive:

docker load -i <input_path.tar>

#Import an image from a TAR archive and tag it:

docker load -i <input_path.tar>
docker tag <image_id> <new_image_name>:<tag>

#Import an image from a TAR archive and rename it:

docker load -i <input_path.tar>
docker tag <image_id> <new_image_name>

Exporting Containers as Images: If you want to export a container along with its dependencies as an image, you can use docker export followed by docker import. However, note that this method does not preserve metadata such as tags and other image attributes.

#Export a container to a TAR archive:

docker export <container_id> -o <output_path.tar>

#Import the exported container as an image:

docker import <output_path.tar> <new_image_name>:<tag>

These commands allow you to export Docker images to TAR archives and import them back into Docker environments. Make sure to adjust paths and image names according to your requirements.

9. Viewing Build Cache:

#docker history: Displays the history of an image, 
#showing the layers and commands used during the build.
#View detailed build cache information:


docker history <image_name>
#View build cache as an image is being built:

docker build --no-cache --pull .
#View build cache statistics:

docker system df -v
#View build cache usage:

docker system df
#View low-level information about the build cache:

docker builder inspect
#View intermediate containers created during builds:

docker ps -a --filter "ancestor=<image_name>"

10. Running Containers from Images:

#docker run: Runs a container from a Docker image.

docker run <image_name>

docker run -d <image_name> #Run a container in detached mode (background)
docker run --name <container_name> <image_name> #Run a container with a specific name
docker run -p <host_port>:<container_port> <image_name> #Run a container with port mapping
docker run <image_name> <command> #Run a container with a specific command
docker run -e <key>=<value> <image_name> #Run a container with environment variables
docker run -v <host_path>:<container_path> <image_name> #Run a container with volume mounting
docker run --cpu-shares <value> --memory <value> <image_name> #Run a container with resource constraints
docker run -i <image_name> #Run a container with interactive mode (attach to STDIN)
docker run -t <image_name> #Run a container with a TTY for input/output
docker run -u <username> <image_name> #Run a container with a specific user
docker run --network=<network_name> <image_name> #Run a container with a specific network

Advanced Commands for Docker Images:

Building Images with Build Arguments:
docker build: Builds a Docker image with build-time arguments specified in the Dockerfile.

docker build --build-arg <key>=<value> -t <image_name> <path_to_Dockerfile>

Building Images with Build Context:
docker build: Builds a Docker image with a specific build context (directory containing the Dockerfile and associated files).

docker build -t <image_name> -f <Dockerfile> <build_context_directory>

Multi-stage Builds:
docker build: Uses multiple build stages to create an optimized Docker image, reducing the final image size.

docker build -t <image_name> --target <stage_name> <path_to_Dockerfile>

Building Images in Parallel:
docker buildx: Builds Docker images in parallel across multiple platforms using BuildKit.

docker buildx build --platform <platforms> -t <image_name> <path_to_Dockerfile>

Building Images from Git Repositories:
docker buildx: Builds Docker images directly from a Git repository.

docker buildx build --platform <platforms> -t <image_name> <git_repository_url>

Build Caching Strategies:
--cache-from: Specifies a list of images to consider as a cache source during the build process.

docker build --cache-from <image_name> -t <new_image_name> .

Building Images with Squash:
--squash: Squashes all the image layers into a single layer, reducing the number of layers in the final image.

docker build --squash -t <image_name> <path_to_Dockerfile>

Building Images with Security Scanning:
docker scan: Scans Docker images for vulnerabilities and security issues.

docker scan <image_name>

Building Images with Custom BuildKit Frontends:
docker build: Utilizes custom BuildKit frontends for advanced build workflows.

DOCKER_BUILDKIT=1 docker build --frontend <frontend_name> -t <image_name> <path_to_Dockerfile>

Building Images with External Build Contexts:
docker build: Specifies an external build context URL for building Docker images.

docker build -t <image_name> <external_build_context_url>

Building Images with BuildKit Features:
DOCKER_BUILDKIT=1: Enables BuildKit features during the Docker build process.

DOCKER_BUILDKIT=1 docker build -t <image_name> <path_to_Dockerfile>

If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!

Git Hub : github.com/nidhi-ashtikar

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

No responses yet