- Blog Categories
- Project Management
- Agile Management
- IT Service Management
- Cloud Computing
- Business Management
- Business Intelligence
- Quality Engineer
- Cyber Security
- Career
- Big Data
- Programming
- Most Popular Blogs
- PMP Exam Schedule for 2024: Check PMP Exam Date
- Top 60+ PMP Exam Questions and Answers for 2024
- PMP Cheat Sheet and PMP Formulas To Use in 2024
- What is PMP Process? A Complete List of 49 Processes of PMP
- Top 15+ Project Management Case Studies with Examples 2024
- Top Picks by Authors
- Top 170 Project Management Research Topics
- What is Effective Communication: Definition
- How to Create a Project Plan in Excel in 2024?
- PMP Certification Exam Eligibility in 2024 [A Complete Checklist]
- PMP Certification Fees - All Aspects of PMP Certification Fee
- Most Popular Blogs
- CSM vs PSM: Which Certification to Choose in 2024?
- How Much Does Scrum Master Certification Cost in 2024?
- CSPO vs PSPO Certification: What to Choose in 2024?
- 8 Best Scrum Master Certifications to Pursue in 2024
- Safe Agilist Exam: A Complete Study Guide 2024
- Top Picks by Authors
- SAFe vs Agile: Difference Between Scaled Agile and Agile
- Top 21 Scrum Best Practices for Efficient Agile Workflow
- 30 User Story Examples and Templates to Use in 2024
- State of Agile: Things You Need to Know
- Top 24 Career Benefits of a Certifed Scrum Master
- Most Popular Blogs
- ITIL Certification Cost in 2024 [Exam Fee & Other Expenses]
- Top 17 Required Skills for System Administrator in 2024
- How Effective Is Itil Certification for a Job Switch?
- IT Service Management (ITSM) Role and Responsibilities
- Top 25 Service Based Companies in India in 2024
- Top Picks by Authors
- What is Escalation Matrix & How Does It Work? [Types, Process]
- ITIL Service Operation: Phases, Functions, Best Practices
- 10 Best Facility Management Software in 2024
- What is Service Request Management in ITIL? Example, Steps, Tips
- An Introduction To ITIL® Exam
- Most Popular Blogs
- A Complete AWS Cheat Sheet: Important Topics Covered
- Top AWS Solution Architect Projects in 2024
- 15 Best Azure Certifications 2024: Which one to Choose?
- Top 22 Cloud Computing Project Ideas in 2024 [Source Code]
- How to Become an Azure Data Engineer? 2024 Roadmap
- Top Picks by Authors
- Top 40 IoT Project Ideas and Topics in 2024 [Source Code]
- The Future of AWS: Top Trends & Predictions in 2024
- AWS Solutions Architect vs AWS Developer [Key Differences]
- Top 20 Azure Data Engineering Projects in 2024 [Source Code]
- 25 Best Cloud Computing Tools in 2024
- Most Popular Blogs
- Company Analysis Report: Examples, Templates, Components
- 400 Trending Business Management Research Topics
- Business Analysis Body of Knowledge (BABOK): Guide
- ECBA Certification: Is it Worth it?
- How to Become Business Analyst in 2024? Step-by-Step
- Top Picks by Authors
- Top 20 Business Analytics Project in 2024 [With Source Code]
- ECBA Certification Cost Across Countries
- Top 9 Free Business Requirements Document (BRD) Templates
- Business Analyst Job Description in 2024 [Key Responsibility]
- Business Analysis Framework: Elements, Process, Techniques
- Most Popular Blogs
- Best Career options after BA [2024]
- Top Career Options after BCom to Know in 2024
- Top 10 Power Bi Books of 2024 [Beginners to Experienced]
- Power BI Skills in Demand: How to Stand Out in the Job Market
- Top 15 Power BI Project Ideas
- Top Picks by Authors
- 10 Limitations of Power BI: You Must Know in 2024
- Top 45 Career Options After BBA in 2024 [With Salary]
- Top Power BI Dashboard Templates of 2024
- What is Power BI Used For - Practical Applications Of Power BI
- SSRS Vs Power BI - What are the Key Differences?
- Most Popular Blogs
- Data Collection Plan For Six Sigma: How to Create One?
- Quality Engineer Resume for 2024 [Examples + Tips]
- 20 Best Quality Management Certifications That Pay Well in 2024
- Six Sigma in Operations Management [A Brief Introduction]
- Top Picks by Authors
- Six Sigma Green Belt vs PMP: What's the Difference
- Quality Management: Definition, Importance, Components
- Adding Green Belt Certifications to Your Resume
- Six Sigma Green Belt in Healthcare: Concepts, Benefits and Examples
- Most Popular Blogs
- Latest CISSP Exam Dumps of 2024 [Free CISSP Dumps]
- CISSP vs Security+ Certifications: Which is Best in 2024?
- Best CISSP Study Guides for 2024 + CISSP Study Plan
- How to Become an Ethical Hacker in 2024?
- Top Picks by Authors
- CISSP vs Master's Degree: Which One to Choose in 2024?
- CISSP Endorsement Process: Requirements & Example
- OSCP vs CISSP | Top Cybersecurity Certifications
- How to Pass the CISSP Exam on Your 1st Attempt in 2024?
- Most Popular Blogs
- Best Career options after BA [2024]
- Top Picks by Authors
- Top Career Options & Courses After 12th Commerce in 2024
- Recommended Blogs
- 30 Best Answers for Your 'Reason for Job Change' in 2024
- Recommended Blogs
- Time Management Skills: How it Affects your Career
- Most Popular Blogs
- Top 28 Big Data Companies to Know in 2024
- Top Picks by Authors
- Top Big Data Tools You Need to Know in 2024
- Most Popular Blogs
- Web Development Using PHP And MySQL
- Top Picks by Authors
- Top 30 Software Engineering Projects in 2024 [Source Code]
- More
- Tutorials
- Practise Tests
- Interview Questions
- Free Courses
- Agile & PMP Practice Tests
- Agile Testing
- Agile Scrum Practice Exam
- CAPM Practice Test
- PRINCE2 Foundation Exam
- PMP Practice Exam
- Cloud Related Practice Test
- Azure Infrastructure Solutions
- AWS Solutions Architect
- AWS Developer Associate
- IT Related Pratice Test
- ITIL Practice Test
- Devops Practice Test
- TOGAF® Practice Test
- Other Practice Test
- Oracle Primavera P6 V8
- MS Project Practice Test
- Project Management & Agile
- Project Management Interview Questions
- Release Train Engineer Interview Questions
- Agile Coach Interview Questions
- Scrum Interview Questions
- IT Project Manager Interview Questions
- Cloud & Data
- Azure Databricks Interview Questions
- AWS architect Interview Questions
- Cloud Computing Interview Questions
- AWS Interview Questions
- Kubernetes Interview Questions
- Web Development
- CSS3 Free Course with Certificates
- Basics of Spring Core and MVC
- Javascript Free Course with Certificate
- React Free Course with Certificate
- Node JS Free Certification Course
- Data Science
- Python Machine Learning Course
- Python for Data Science Free Course
- NLP Free Course with Certificate
- Data Analysis Using SQL
Docker Image Commands [with Examples & Use Cases]
Updated on 05 November, 2022
14.58K+ views
• 10 min read
Table of Contents
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