The Wayback Machine - https://web.archive.org/web/20241013190058/https://www.geeksforgeeks.org/what-is-docker-image/
Open In App

What is Docker Image?

Last Updated : 10 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the dependencies required to run the application. The application runs quickly and reliably from one computing environment to another.

Docker Flow

What is Docker Image?

Docker images are built using the Dockerfile which consists of a set of instructions that are required to containerize an application. The docker image includes the following to run a piece of software. A docker image is a platform-independent image that can be built in the Windows environment and it can be pushed to the docker hub and pulled by others with different OS environments like Linux.

  • Application ode.
  • Runtime.
  • Libraries
  • Environmentaltools.

Docker image is very light in weight so can be portable to different platforms very easily. To explore more about creating and managing Docker images within a DevOps pipeline, the DevOps Engineering – Planning to Production course covers Docker image creation, optimization, and deployment.

Components of Docker Image

The following are the terminologies and components related to Docker Image:

  • Layers: Immutable filesystem layers stacked to form a complete image.
  • Base Image: The foundational layer, often a minimal OS or runtime environment.
  • Dockerfile: A text file containing instructions to build a Docker image.
  • Image ID: A unique identifier for each Docker image.
  • Tags: Labels used to manage and version Docker images.

SubCommands of Docker Image

The following are the some of the sub commands that are used with Docker Image:

Command

Description

docker image build

This command is used for building an image from the Dockerfile

docker image history

It is used for knowing the history of the docker image

docker image inspect

It is used for displaying the detailed information on one or more images

docker image prune

It used for removing unused images that are not associated with any containers

docker image save

This command helps in saving the docker images into a tar archived files

docker image tag

It helps in crating a tag to the target image that refers to the source image.

Docker Image Prune

Docker image prune is a command used in the docker host to remove the images that are not used or Docker image prune command is used to remove the unused docker images.

docker image prune
docker_image_prune

  • All the unused images are also know as dangling images which re not associated with any containers

Docker Image Build

The Following is the command which is used to build the docker image.

docker build -t your_image_name:tag -f path/to/Dockerfile .

  • Docker build: Initiates the build process.
  • -t your_image_name:tag: Gives the image you’re creating a name and, if desired, a tag.
  • path/to/Dockerfile . : Gives the location of the Dockerfile. Give the right path if it’s not in the current directory. “(.) DOT” represents the current wordir.

Docker Image Tag

Docker tags are labels for container images, used to differentiate versions and variants of an image during development and deployment. Docker tags will help you identify the various versions of docker images and help distinguish between them. Docker image will help us to build continuous deployment very quickly

Uses of Docker Images

The following are the uses of Docker Images:

  1. We can easily and effectively run the containers with the aid of docker images.
  2. All the code, configuration settings, environmental variables, libraries, and runtime are included in a Docker image.
  3. Docker images are platform-independent. 
  4. Layers are the building blocks of an image. 
  5. WithWhen using the build command, the user has the option of completely starting from scratch or using an existing image for the first layer.

Difference between Docker Image Vs Docker Container

The following are the difference between Docker Image and Docker Container:

Docker image Docker container 
The Docker image is the Docker container’s source code. The Docker container is the instance of the Docker image.
Dockerfile is a prerequisite to Docker Image. Docker Image is a pre-requisite to Docker Container.
Docker images can be shared between users with the help of the Docker Registry.  Docker containers can’t be shared between the users.
To make changes in the docker image we need to make changes in Dockerfile. We can directly interact with the container and can make the changes required.

Structure Of Docker Image 

The layers of software that make up a Docker image make it easier to configure the dependencies needed to execute the container. 

  • Base Image: The basic image will be the starting point for the majority of Dockerfiles, and it can be made from scratch.
  • Parent Image: The parent image is the image that our image is based on. We can refer to the parent image in the Dockerfile using the FROM command, and each declaration after that affects the parent image.
  • Layers: Docker images have numerous layers. To create a sequence of intermediary images, each layer is created on top of the one before it. 
  • Docker Registry: Refer to this page on the Docker Registry for further information.

How To Create A Docker Image And Run It As Container?

Follow the below steps to create a Docker Image and run a Container:

Step 1: Create a Dockerfile.

Step 2: Run the following command in the terminal and it will create a docker image of the application and download all the necessary dependencies needed for the application to run successfully.

docker build -t <name>:<tag> 

This will start building the image.

Step 3:  We have successfully created a Dockerfile and a respective Docker image for the same.

Step 4: Run the following command in the terminal and it will create a running container with all the needed dependencies and start the application.

docker run -p 9000:80 <image-name>:<tag> 

The 9000 is the port we want to access our application on. 80 is the port the container is exposing for the host to access.

How to Build Docker Python Images? A Step-By-Step Guide

Step 1: Create A Dockerfile

  • Firstly write a dockerfile for supporting python applications and try to python application running instruction with the CMD options for the default run. The following is the sample dockerfile that we are using here:
# Use the official Python image as a base
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]
Dockerfile

Step 2: Create requirements.txt and app.py Files

  • Try on provide the requirements.txt with specifying the names of required software names and app.py file with writing the python application. The following are the sample code of those requirements.txt and app.py.

Filename: requirements.txt

Flask==2.1.0
Werkzeug==2.1.2 # Ensure this is compatible with Flask 2.1.0
requirementstxt

Filename: app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, World!'

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)
app_py

Step 3: Build a Docker Image

  • The following command is used for building the docker python image from the Dockerfile:
 docker image build -t mypython-app:v1 .
  • The following shows it practically of building of python based docker image from the Dockerfile:
docker_image_build_t_mypython-app-v1

Step 4: Verify the Docker Image

  • Use the following command to verify the success build of docker application:
docker images
  • The following screenshot shows the success build of python based docker image.
docker_images

Step 5: Run a Python based Docker Container

  • Run a container to that docker python image with the following command:
docker run -dit -p 80:80  --name  mycontainer1 mypython-app:v1
  • The following command is the successful run of that python based docker container:
Run_a_Python_based_Docker_Container

Step 6: Access the Docker Container

  • Now, try on access the docker container with public IP of the host system with the exposed port to see the successful run of the containerized python application. The following screenshot illustrates the successful accessing of it.
HelloWorld

Docker Image commands

The following are the some of the Docker Image Commands that are widely used:

List Docker Images

docker images

Example

$ docker ls

REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 0d9c6c5575f5 4 days ago 126MB
ubuntu 18.04 47b199b0cb85 2 weeks ago 64.2MB

Pull an Docker Image From a Registry

docker image pull <image-name>

Example

$ docker pull alpine:3.11

3.11: Pulling from library/alpine
Digest: sha256:9f11a34ef1c67e073069f13b09fb76dc8f1a16f7067eebafc68a5049bb0a072f
Status: Downloaded newer image for alpine:3.11

Docker Images Prune

  • The Docker Image Prune will remove the unused Docker Images that are not associated with any containers. The Command looks as follows:
docker image prune

Docker Images Filter

  • When we want to filter some specific images we can filter out them using its labels or its meta information tags with the option –filter. The command looks as follows:
docker image ls -f "reference=mypython-app"

Remove an Image from Docker

  • The following command is used for removing the docker image from the local registry:
docker rmi <id-of-image>

Example

$ docker rmi <image_id>

Untagged: <image_id>
Deleted: sha256:<image_id>

 Searching for a specific image on Docker Hub

docker search ubuntu

Example

$ docker search ubuntu

NAME                             DESCRIPTION                                                          STARS                 OFFICIAL                          AUTOMATED

ubuntu                           Ubuntu is a Debian-based Linux operating s…                4458                    [OK]               

ubuntu-upstart              Upstart is an event-based replacement for …                  62                         [OK]               

tutum/ubuntu                 Simple Ubuntu docker images with ssh access             49                          [OK]               

ansible/ubuntu14.04-ansible

Best Practices for Docker Images

The following are the some of the best practices of using Docker Images:

  • Optimize Image Size: Try to use multi-stage builds and minimal base images to reduce the image size.
  • Leverage Caching: By ordering the instructions such as less changing instructions first and frequently changing one on bottom, we can maximize caching and speed up the builds.
  • Keep Image Secure: On regularly updating the images and scannng for vulnerabilities, we can make the image secured.
  • Use Tags Wisely: By practicing to use specific tags to images instead of specifying latest, we can ensure the stability and consistency.

The following are the some of the troubleshooting common issues related to docker images:

  • Large Image Sizes: Use multi-stage builds and clean up unnecessary files to reduce image size.
  • Dependency Conflicts: Ensure compatibility by specifying exact versions of dependencies in the Dockerfile.
  • Build Failures: Check Dockerfile syntax and command correctness, and verify all required files are included.
  • Slow Performance: Optimize Dockerfile instructions order and leverage caching to improve build times.

Docker Images – FAQs

What is DockerHub?

Docker Hub is a repository service and it is a cloud-based service where people push their Docker Container Images and also pull the Docker Container Images from the Docker Hub anytime or anywhere via the internet. It provides features such as you can push your images as private or public.

How to use Docker Run Command?

Docker run command will run the docker image as an container. docker ps: Docker run will list all the containers which are running in the docker host.

How to list Docker Images?

The command to list all the docker images was mentioned below.

docker images

Where are my docker images stored?

The docker images are stored in the default location based on your operating system such as
/var/lib/docker on Linux distributions, C:\ProgramData\docker on Windows and /Users/<username>/Library/Containers/com.docker.docker/Data on MacOS.

How do I find the Docker Images?

You can find the docker images by listing the docker images using the following docker command:

docker image ls

How to Store Docker Images Locally?

The Docker Images are stored locally by default when you build or pull them. You can save them into tar archive files using the following command:

docker image save

How do I change the Docker Image location?

We can change the default location of the docker images from where it is storing by changing the docker daemon configuration settings through the file /etc/docker/daemon.json by specifying a different location value for the data-root key. Then restart the docker daemon to update the changes to take effect.



Previous Article
Next Article

Similar Reads

Docker: How To Use Bash With An Alpine Based Docker Image?
Docker is a tool that is used to encapsulate the application with all its dependencies, called Docker containers. On the other hand, Alpine Linux is a lightweight and minimal Linux distribution. Here in this guide, I will first discuss what Docker is. Then I will discuss what Alpine is. After this, I will walk you through the different steps to ins
5 min read
How to Use Docker For Fault Tolerance with Docker Replicas?
Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. A cluster of docker engines is called a swarm. Docker Swarm is a cluster management and orchestration feature embedded in the Docker Engine. Docker ReplicasDocker replicas refer to running multiple instances of a doc
4 min read
How to Use Docker For Cross-Platform Containerization with Docker Buildx?
Docker has completely modified the manner in which software program is evolved and deployed, with the aid of introducing containerization generation. In order to package programs with their dependencies and make certain consistency of conduct across many contexts, packing containers offer a compact and portable solution. Deploying containerized app
7 min read
Docker Compose vs Docker Swarm
Docker is one of the most widely used container-based software on the market. Docker is an open-source platform for creating, deploying, and managing containerized applications. Docker allows us to simply bundle our apps into containers and can be deployable on any platform that supports docker software acting as a platform independent. What Is Doc
6 min read
Docker CLI vs Docker Desktop
Docker is an open-source platform. It is used to containerize applications. This is done by packaging applications along with their dependencies into containers that can be easily deployed. Users interact with Docker primarily using either the Docker CLI or the Docker Desktop Application. In this article, we'll be looking into each one in detail an
5 min read
Docker - Deploying WebApps on Docker
Docker applies abstraction at the software layer of the operating system to create an instant and portable environment that includes everything you need: a Linux kernel, libraries, system tools like grep or sshfs, plus applications (web servers/services) and all dependencies needed by those applications (e.g., database engines). When deployed, thes
9 min read
How To Use Docker Security Tools To Secure Docker Container Images
Docker is a tool that is used to package the application and its dependencies into compact units called Docker containers. In this guide, I will first discuss what a docker is. Then I will discuss a security tool called Trivy, which is used to scan and detect Docker image vulnerabilities. After this, I will walk you through the different steps to d
11 min read
Docker - Using Public Repositories To Host Docker Images
Docker is a software platform for creating isolated virtualized environments for building, deploying, and testing applications with ease. In this tutorial, we will learn how to host public repositories on docker hub which is a hosted repository service provided by Docker for finding and sharing container images. Just like GitHub allows the hosting
10 min read
Docker - Docker Container for Node.js
Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its dependencies, making it highly scalable and maintainable.
12 min read
Getting started with Spring Boot 3, Kafka over docker with docker-compose.yaml
We will create two Spring Boot 3.x applications (Producer and Consumer) requiring Java 17 to get started. We need a queuing service to pass messages from the producer Spring service to the consumer Spring service. Here, we will use Kafka. Confluent provides Docker images that are easy to use for setting up the Kafka environment. Since we will use a
5 min read
Docker Logs Inside the Docker Container
While running containers, it is important to know about various information like running processes generated by the container. Docker logs allow us to get this information regarding the containers. While it's pretty straighforward to check logs directly from Docker CLI, here we will see how to check logs from within a container. What is Docker?Dock
5 min read
Docker Tips : about /var/run/docker.sock
Docker has thus gained lots of usage today in software development. It allows developers to create, deploy, and run their applications in isolated environments. Such isolation will guarantee consistency between development, testing, and production environments and, finally, app security and scalability. A central part of the functionalities of Dock
8 min read
How To Use Docker Desktop To Deploy Docker Containerized Applications
Docker Desktop is an user-friendly tool that simplifies the deployment of Docker containerized applications on your local machine. It provides a graphical interface for managing containers, images, and volumes, making it accessible for developers and DevOps professionals. In this guide, we will walk you through the essential steps of using Docker D
9 min read
Docker Start vs Docker Run
Docker is a platform as a service(PaaS) that allows engineers to use OS-level virtualization to automate the deployment of applications inside lightweight and portable packages called containers. The containers have a very big advantage as they allow apps to run on different platforms effortlessly, and reduce the old headache of "it runs on my mach
7 min read
How to Use Docker Content Trust to Verify Docker Container Images
The world of containerized applications is based on trust. You rely on Docker images to be exactly what they say they are: secure, reliable, robust, and built with the right elements. But just like you can not just blindly trust any random ingredient in your kitchen, similarly the Docker image needs a kind of verification so we don't need to be con
12 min read
Docker - Publishing Images to Docker Hub
Docker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it public it gives users more flexibility in creating e
8 min read
Docker - Search Image in Dockerhub through CLI
There are tons of pre-built Docker Images that you can find on Docker's official repository called DockerHub. In fact, you can also push your own customized Image on DockerHub. These Images can then be pulled using the Docker pull command and can be used as Base Images. In this article, we will discuss how you can search a Docker Image on DockerHub
2 min read
Microsoft Azure - Running an App inside a Docker Container Image
In this article, we'll learn how to run an app inside of a container with Docker. For this, you need to set up Docker on your local Dev machine, by going to docker.com, and installing the Docker desktop application for your specific operating system. We can use the Docker Pokemon to pull an image from the Docker Hub onto your local machine. Here, w
2 min read
How to Push a Container Image to a Docker Repository?
In this article we will look into how you can push a container image to a Docker Repo. We're going to use Docker Hub as a container registry, that we're going to push our Docker image to. Follow the below steps to push container Image to Docker repository: Step 1: The first thing you need to do is make sure that you've signed up for Docker account.
2 min read
How to Publish Docker Image to Dockerhub Using Github Actions?
Pre-requisites: GitHub Actions, Docker Hub Publishing Docker images to Docker Hub using GitHub Actions is a simple and efficient process that can automate your image-building and publishing process. What are Github Actions?GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) platform integrated with GitHub. It enables developers
3 min read
How to Push Docker Image to AWS ECS Fargate Using Terraform?
ECS helps to manage containers and this service is provided by AWS, it allows users to easily manage and scale docker containers on a cluster of ec2 instances, without having to manage the underlying infrastructure. What is ECS Fargate?Fargate provides a way to run containerized applications on a serverless infrastructure, which means you only pay
8 min read
Docker Desktop Sample Image
Before talking about the docker desktop let’s understand what is docker. Docker is a service that allows you to containerize your application into a single unit, which allows you to run your application from one computer environment to another. This makes application portable between systems and solves the “it works on my computer” problem. What is
5 min read
Spring Boot - Creating docker image using Gradle
Spring boot is a backed-end framework used for developing stand-alone Java applications rapidly. It minimizes configuration so developers can focus only on developing business logic. What is Docker?Docker is an open-source platform designed for developing, transporting, and strolling applications. Docker has containers that's a standalone, executa
5 min read
How To See Docker Image Contents?
In the evolving world of containerization, Docker has emerged as a tool for packaging and deploying applications. While creating and sharing Docker images has streamlined the development and deployment process. Contents of a Docker image are important for troubleshooting, optimizing, and ensuring security in your containerized applications. This ar
3 min read
How To Add A CA Root Certificate Inside A Docker Image ?
The CA Root Certificate is a digital certificate that is used to only trust software and applications. Using this inside the Docker images establishes trust between the running applications inside the containers and the external host system. It facilitates secure communication by verifying the authentication with SSL/TLS certificates provided by ex
6 min read
How To Build Docker Image In GitHub Actions ?
GitHub Actions is a powerful tool provided by GitHub to automate tasks directly within your repositories. It plays a crucial role in Continuous Integration/Continuous Deployment (CI/CD) pipelines, enabling developers to automate workflows, tests, and deployments seamlessly. With GitHub Actions, workflows are defined using simple YAML files called w
5 min read
How to Install ""ifconfig"" Command in My Ubuntu Docker Image?
Docker images are templates that include all the code, libraries, dependencies, and runtime required to run an application. They guarantee consistency and portability across several systems, which makes them indispensable in containerized contexts. Docker images make it simple to scale, deploy, and manage applications, which promotes quicker develo
6 min read
How To Optimize Docker Image ?
Docker images are small executable packages that can be used to run a program along with its libraries, dependencies, code, and runtime. Docker images form the basis of Docker containers, which allow software to be deployed continuously across several environments. We will be talking more about Docker image optimization in this blog. To find out mo
10 min read
Best Practices for Docker Image Management and Versioning
In today's fast-paced development world, Docker containers have become a game-changer. They offer a lightweight, portable, and isolated environment to run applications. But with great power comes great responsibility (and sometimes, a lot of images!). This guide dives into best practices for Docker image management and versioning. We'll explore str
4 min read
Creating a Docker Image with Git Installed
Version control is one of the most important aspects of any software development project and when we talk about version control, there is no better tool than Git. The majority of the developers depend upon Git to manage and share their project components among the team members.  Even if you are running your project on Docker, you can still access y
10 min read