Explore Courses
course iconScrum AllianceCertified ScrumMaster (CSM) Certification
  • 16 Hours
Best seller
course iconScrum AllianceCertified Scrum Product Owner (CSPO) Certification
  • 16 Hours
Best seller
course iconScaled AgileLeading SAFe 6.0 Certification
  • 16 Hours
Trending
course iconScrum.orgProfessional Scrum Master (PSM) Certification
  • 16 Hours
course iconScaled AgileSAFe 6.0 Scrum Master (SSM) Certification
  • 16 Hours
course iconScaled Agile, Inc.Implementing SAFe 6.0 (SPC) Certification
  • 32 Hours
Recommended
course iconScaled Agile, Inc.SAFe 6.0 Release Train Engineer (RTE) Certification
  • 24 Hours
course iconScaled Agile, Inc.SAFe® 6.0 Product Owner/Product Manager (POPM)
  • 16 Hours
Trending
course iconKanban UniversityKMP I: Kanban System Design Course
  • 16 Hours
course iconIC AgileICP Agile Certified Coaching (ICP-ACC)
  • 24 Hours
course iconScrum.orgProfessional Scrum Product Owner I (PSPO I) Training
  • 16 Hours
course iconAgile Management Master's Program
  • 32 Hours
Trending
course iconAgile Excellence Master's Program
  • 32 Hours
Agile and ScrumScrum MasterProduct OwnerSAFe AgilistAgile CoachFull Stack Developer BootcampData Science BootcampCloud Masters BootcampReactNode JsKubernetesCertified Ethical HackingAWS Solutions Artchitct AssociateAzure Data Engineercourse iconPMIProject Management Professional (PMP) Certification
  • 36 Hours
Best seller
course iconAxelosPRINCE2 Foundation & Practitioner Certificationn
  • 32 Hours
course iconAxelosPRINCE2 Foundation Certification
  • 16 Hours
course iconAxelosPRINCE2 Practitioner Certification
  • 16 Hours
Change ManagementProject Management TechniquesCertified Associate in Project Management (CAPM) CertificationOracle Primavera P6 CertificationMicrosoft Projectcourse iconJob OrientedProject Management Master's Program
  • 45 Hours
Trending
course iconProject Management Master's Program
  • 45 Hours
Trending
PRINCE2 Practitioner CoursePRINCE2 Foundation CoursePMP® Exam PrepProject ManagerProgram Management ProfessionalPortfolio Management Professionalcourse iconAWSAWS Certified Solutions Architect - Associate
  • 32 Hours
Best seller
course iconAWSAWS Cloud Practitioner Certification
  • 32 Hours
course iconAWSAWS DevOps Certification
  • 24 Hours
course iconMicrosoftAzure Fundamentals Certification
  • 16 Hours
course iconMicrosoftAzure Administrator Certification
  • 24 Hours
Best seller
course iconMicrosoftAzure Data Engineer Certification
  • 45 Hours
Recommended
course iconMicrosoftAzure Solution Architect Certification
  • 32 Hours
course iconMicrosoftAzure Devops Certification
  • 40 Hours
course iconAWSSystems Operations on AWS Certification Training
  • 24 Hours
course iconAWSArchitecting on AWS
  • 32 Hours
course iconAWSDeveloping on AWS
  • 24 Hours
course iconJob OrientedAWS Cloud Architect Masters Program
  • 48 Hours
New
course iconCareer KickstarterCloud Engineer Bootcamp
  • 100 Hours
Trending
Cloud EngineerCloud ArchitectAWS Certified Developer Associate - Complete GuideAWS Certified DevOps EngineerAWS Certified Solutions Architect AssociateMicrosoft Certified Azure Data Engineer AssociateMicrosoft Azure Administrator (AZ-104) CourseAWS Certified SysOps Administrator AssociateMicrosoft Certified Azure Developer AssociateAWS Certified Cloud Practitionercourse iconAxelosITIL 4 Foundation Certification
  • 16 Hours
Best seller
course iconAxelosITIL Practitioner Certification
  • 16 Hours
course iconPeopleCertISO 14001 Foundation Certification
  • 16 Hours
course iconPeopleCertISO 20000 Certification
  • 16 Hours
course iconPeopleCertISO 27000 Foundation Certification
  • 24 Hours
course iconAxelosITIL 4 Specialist: Create, Deliver and Support Training
  • 24 Hours
course iconAxelosITIL 4 Specialist: Drive Stakeholder Value Training
  • 24 Hours
course iconAxelosITIL 4 Strategist Direct, Plan and Improve Training
  • 16 Hours
ITIL 4 Specialist: Create, Deliver and Support ExamITIL 4 Specialist: Drive Stakeholder Value (DSV) CourseITIL 4 Strategist: Direct, Plan, and ImproveITIL 4 Foundationcourse iconJob OrientedData Science Bootcamp
  • 6 Months
Trending
course iconJob OrientedData Engineer Bootcamp
  • 289 Hours
course iconJob OrientedData Analyst Bootcamp
  • 6 Months
course iconJob OrientedAI Engineer Bootcamp
  • 288 Hours
New
Data Science with PythonMachine Learning with PythonData Science with RMachine Learning with RPython for Data ScienceDeep Learning Certification TrainingNatural Language Processing (NLP)TensorflowSQL For Data Analyticscourse iconIIIT BangaloreExecutive PG Program in Data Science from IIIT-Bangalore
  • 12 Months
course iconMaryland UniversityExecutive PG Program in DS & ML
  • 12 Months
course iconMaryland UniversityCertificate Program in DS and BA
  • 31 Weeks
course iconIIIT BangaloreAdvanced Certificate Program in Data Science
  • 8+ Months
course iconLiverpool John Moores UniversityMaster of Science in ML and AI
  • 750+ Hours
course iconIIIT BangaloreExecutive PGP in ML and AI
  • 600+ Hours
Data ScientistData AnalystData EngineerAI EngineerData Analysis Using ExcelDeep Learning with Keras and TensorFlowDeployment of Machine Learning ModelsFundamentals of Reinforcement LearningIntroduction to Cutting-Edge AI with TransformersMachine Learning with PythonMaster Python: Advance Data Analysis with PythonMaths and Stats FoundationNatural Language Processing (NLP) with PythonPython for Data ScienceSQL for Data Analytics CoursesAI Advanced: Computer Vision for AI ProfessionalsMaster Applied Machine LearningMaster Time Series Forecasting Using Pythoncourse iconDevOps InstituteDevOps Foundation Certification
  • 16 Hours
Best seller
course iconCNCFCertified Kubernetes Administrator
  • 32 Hours
New
course iconDevops InstituteDevops Leader
  • 16 Hours
KubernetesDocker with KubernetesDockerJenkinsOpenstackAnsibleChefPuppetDevOps EngineerDevOps ExpertCI/CD with Jenkins XDevOps Using JenkinsCI-CD and DevOpsDocker & KubernetesDevOps Fundamentals Crash CourseMicrosoft Certified DevOps Engineer ExperteAnsible for Beginners: The Complete Crash CourseContainer Orchestration Using KubernetesContainerization Using DockerMaster Infrastructure Provisioning with Terraformcourse iconTableau Certification
  • 24 Hours
Recommended
course iconData Visualisation with Tableau Certification
  • 24 Hours
course iconMicrosoftMicrosoft Power BI Certification
  • 24 Hours
Best seller
course iconTIBCO Spotfire Training
  • 36 Hours
course iconData Visualization with QlikView Certification
  • 30 Hours
course iconSisense BI Certification
  • 16 Hours
Data Visualization Using Tableau TrainingData Analysis Using Excelcourse iconEC-CouncilCertified Ethical Hacker (CEH v12) Certification
  • 40 Hours
course iconISACACertified Information Systems Auditor (CISA) Certification
  • 22 Hours
course iconISACACertified Information Security Manager (CISM) Certification
  • 40 Hours
course icon(ISC)²Certified Information Systems Security Professional (CISSP)
  • 40 Hours
course icon(ISC)²Certified Cloud Security Professional (CCSP) Certification
  • 40 Hours
course iconCertified Information Privacy Professional - Europe (CIPP-E) Certification
  • 16 Hours
course iconISACACOBIT5 Foundation
  • 16 Hours
course iconPayment Card Industry Security Standards (PCI-DSS) Certification
  • 16 Hours
course iconIntroduction to Forensic
  • 40 Hours
course iconPurdue UniversityCybersecurity Certificate Program
  • 8 Months
CISSPcourse iconCareer KickstarterFull-Stack Developer Bootcamp
  • 6 Months
Best seller
course iconJob OrientedUI/UX Design Bootcamp
  • 3 Months
Best seller
course iconEnterprise RecommendedJava Full Stack Developer Bootcamp
  • 6 Months
course iconCareer KickstarterFront-End Development Bootcamp
  • 490+ Hours
course iconCareer AcceleratorBackend Development Bootcamp (Node JS)
  • 4 Months
ReactNode JSAngularJavascriptPHP and MySQLcourse iconPurdue UniversityCloud Back-End Development Certificate Program
  • 8 Months
course iconPurdue UniversityFull Stack Development Certificate Program
  • 9 Months
course iconIIIT BangaloreExecutive Post Graduate Program in Software Development - Specialisation in FSD
  • 13 Months
Angular TrainingBasics of Spring Core and MVCFront-End Development BootcampReact JS TrainingSpring Boot and Spring CloudMongoDB Developer Coursecourse iconBlockchain Professional Certification
  • 40 Hours
course iconBlockchain Solutions Architect Certification
  • 32 Hours
course iconBlockchain Security Engineer Certification
  • 32 Hours
course iconBlockchain Quality Engineer Certification
  • 24 Hours
course iconBlockchain 101 Certification
  • 5+ Hours
NFT Essentials 101: A Beginner's GuideIntroduction to DeFiPython CertificationAdvanced Python CourseR Programming LanguageAdvanced R CourseJavaJava Deep DiveScalaAdvanced ScalaC# TrainingMicrosoft .Net Frameworkcourse iconSalary Hike GuaranteedSoftware Engineer Interview Prep
  • 3 Months
Data Structures and Algorithms with JavaScriptData Structures and Algorithms with Java: The Practical GuideLinux Essentials for Developers: The Complete MasterclassMaster Git and GitHubMaster Java Programming LanguageProgramming Essentials for BeginnersComplete Python Programming CourseSoftware Engineering Fundamentals and Lifecycle (SEFLC) CourseTest-Driven Development for Java ProgrammersTypeScript: Beginner to Advanced

Docker Image Commands [with Examples & Use Cases]

Updated on 05 November, 2022

14.58K+ views
10 min read

Docker images play a pivotal role in the Docker ecosystem. In this article, we will cover an in-depth overview of Docker images, Docker hub and Docker images command. If you are looking for an in-depth course on Docker, do check the Docker course online.

As defined by Docker’s official documentation, “Docker provides the ability to package and run an application in a loosely isolated environment called a container. The isolation and security allow us to run many containers simultaneously on a given host. Containers are lightweight because they do not need the extra load of a hypervisor but run directly within the host machine’s kernel. This means we can run more containers on a hardware combination than virtual machines.”

What is a Docker Image? 

Before we learn about containers, we need to first understand the ‘image’ term in Docker. If we look at the diagram above, the top layer of apps and libs/bins above the Docker daemon is encapsulated in the container. The app is packaged with libraries and binaries required by it. But how does Docker achieve this packaging?

Docker provides the facility to create a custom image on top of the Linux kernel with your app and its libraries. If we take this from an object-oriented programming point of view, an image is a class, where all the requirements are defined and declared. A container is an instance of the image. These images are stored somewhere in the cloud and pulled as needed.

List of Docker Image Commands

Docker uses a set of keywords to carry out these activities related to image and the list below shows all Docker image commands with their descriptions.

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

If you are interested in mastering DevOps and Docker in particular, do check out the best online courses for DevOps for detailed information.

Docker Image Commands: Examples 

Build an image from a Dockerfile 
Docker image build -t <image_name> . 

Assuming a Dockerfile is in the current directory:

Docker image build -t myimage .

The option -t allows you to tag the image. In this case, the image named myimage is tagged latest as no tag was specified. The tag can be specified as:

Docker image build -t myimage:first . 

The period at the end of the docker image command is essential. It references the application path. You can also use -f <file> if you didn’t name your build file “Dockerfile”, e.g., if an alternative file name was used:

Docker image build -f mybuildfile.txt -t myimage . 

Tag a built image

Docker tag <image_name> [user/repository:tag] 

Example: tag the myimage image with first in the Docker Hub myrepository repository and the username is myname:

Docker tag myimage myname/myrepository:first 

You do not need to make the Docker Hub explicitly. Running this command will create a repository named myrepository itself.

Push tagged images to Docker Hub

Docker push [user/repository] 

Example:

Docker push  myname/myrepository 

You can also push with tag:

Docker push  myname/myrepository:first 
  • List Docker images
  • Docker image ls
  • Docker images
  • View all images, both active and dangling:
Docker image ls -a 

PRACTICE 

  • We have a simple Dockerfile and another file named mybuildfile.txt in our directory. The process is as follows:
  • Build an image from Dockerfile named myimage.
  • Build an image from mybuildfile.txt named mybuildimage.
  • Add the latest tag to any of the images built above. The repository name should be helloworld.
  • View the images tagged and built.
  • Push the tagged image(s).
  • Verify on your Docker Hub account  that the image(s) has been pushed

Dockerfile Commands 

  • The FROM instruction in a Dockerfile specifies the base image for the new image you will build. It is usually the first instruction in a Dockerfile and a best practice is to use images from official repos on this line.
  • The RUN instruction in a Dockerfile allows you to run commands inside the image. Each RUN instruction creates a single new layer.
  • The COPY instruction in a Dockerfile adds files into the image as a new layer. It is common to use the COPY instruction to copy your application code into an image.
  • The EXPOSE instruction in a Dockerfile documents the network port that the application uses.
  • The ENTRYPOINT instruction in a Dockerfile sets the default application to run when the image is started as a container.

Other Dockerfile instructions include LABEL, ENV, ONBUILD, HEALTHCHECK, CMD, and more.

Docker hub Commands 

Log into Docker Hub

Docker login 

Or login to another registry:

Docker login <url> 
Docker login -u <id> -p <password> <url> 

Search Docker Hub

Search the Docker Hub image repository:

Docker search [options] <keyword> 

Example: find up to five Docker Hub images that reference php ordered by stars rating:

Docker search --limit 5 php 

Pull a Docker Hub image

Download one or more images from Docker Hub:

Docker pull <image> 

Example: pull the latest Node.js Long-Term-Support Alpine Linux image:

Docker pull node:lts-alpine 

Docker Image Use Cases 

A. Listing Locally Stored Docker Images 

The below command is used to list the locally stored Docker images, d:

Docker image list

The below command is used to list Docker images along with the long image ID

$ Docker image list --no-trunc 

B. Listing Docker Intermediary or Bad Images 

The below command is used to list all the unused Docker images on your Docker host:

$ Docker image list --filter dangling=true 

Below command is used to list only the image IDs of the unused Docker images on our Docker host,

$ Docker image list --quiet --filter dangling=true 

C. Listing Only Docker Image IDs 

Below command is used to list only image IDs on Docker, we have to use the “Docker images” command with the “–quiet” option to suppress all other columns.

$ Docker images --quiet 
$ Docker images -q 

Anatomy of a Docker Image Command

A Docker image is made up of a collection of files that bundle together all the essentials – such as installations, application code, and dependencies – required to configure a fully operational container environment. You can create a Docker image by using one of two methods:

  • Interactive: By running a container from an existing Docker image, manually changing that container environment through a series of live steps, and saving the resulting state as a new image.
  • Dockerfile: By constructing a plain-text file, known as a Dockerfile, which provides the specifications for creating a Docker image.

Docker and Kubernetes go hand in hand and are the most sought-out tech stack in DevOps space. If you are looking to enhance your Docker and Kubernetes, do check Kubernetes and Docker training.

Docker Image Repositories 

We store images in centralized places called image registries. This makes it easy to share and access them.

The most common registry is Docker Hub. Other registries exist, including 3rd party registries and secure on-premises registries. However, the Docker client is opinionated and defaults to using Docker Hub. We will be using Docker Hub for henceforth.

The output of the following docker image command is snipped, but you can see that Docker is configured to use https://index.Docker.io/v1/ as its default registry when pushing and pulling images (this redirects to v2).

Docker Hub has the concept of official repositories and unofficial repositories.

As the name suggests, official repositories are the home to images that have been vetted and curated by Docker, Inc. This means they should contain up-to-date and high-quality code that is secure, well-documented, and in line with best practices.

Unofficial repositories can be like the wild west. You should not assume they are safe, well-documented, or built according to the best practices. That is not saying everything in unofficial repositories is bad. You just need to be very careful before trusting the code. To be honest, you should always be careful when trusting software from the internet, even images from official repositories.

How to Create a Docker Image? 

Prerequisite: ensure Docker is installed in your local system.

  • A Docker image is created using the Dockerfile and Docker command utilities. 
  • The Dockerfile file contains a set of instructions for the Docker to create the container. 

Let us create a basic image for a container that displays a “hello knowledgehut” message when it is run.

FROM debian:11 
CMD ["echo", "hello knowledgehut "] 

To create an image from theDockerfile file, we need to run the build command.

Docker build -t hello_KH 

The Docker build command just created an image named hello_KH. The image is stored locally in the system, 1

Docker run --rm hello 

What is a DockerFile? 

A Dockerfile is the starting point for creating a container image. It describes an application and tells Docker how to build it into an image.

The directory containing the application and dependencies is referred to as the build context. It is a common practice to keep your Dockerfile in the root directory of the build context. It is also important that Dockerfile starts with a capital “D” and is one word. “dockerfile” and “Docker file” are not valid.

Do not underestimate the impact of the Dockerfile as a form of documentation. It is a great document to bridge the gap between development and operation. It also has the power to speed up the onboarding of new developers, etc. The file accurately describes the application and its dependencies in an easy-to-read format. You should treat it like you would treat source code and check it into a version control system.

Summary

Docker provides a great means of managing fundamental system components, such as the operating system and the application server. Moreover, Docker makes it easier to create replica environments for your applications. Docker image commands help you to effectively build, manage, and utilize Docker images. 

Finally, you can package up to two million lines of application code into a single Docker image—so it is an increasingly popular tool in the DevOps toolkit.

Frequently Asked Questions (FAQs)

1. What is Docker images command?

Docker images command used to give detailed descriptions images in the system.

2. How do I view images in Docker?

With Docker images. command you can view and manipulate the images in the system. 

3. How do I run a Docker image from the command line?

With Docker run <image_name> command you can view the images in the system. 

4. How do I run a Docker image locally?

By modifying the Docker environment variables 

For example: 

Setting below environments to ensure the Docker image runs locally

set userprofile = <USER_PROFILE_PATH>  # set your user proile  set Docker_cert_path = <DOCKER_CERT_PATH> # set Docker certification path  set Docker_host= <IP_ADDRESS> # use your ip and start it  set Docker_machine_name = <DOCKER_MACHINE_NAME> # set Docker machine name  set Docker_tls_verify= 1 # verify TLS connection