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.
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.