1. Home
Docker

Understanding Docker: A Comprehensive Overview

Discover comprehensive Docker tutorials designed for all skill levels. Learn containerization, deployment, and best practices to enhance your development workflow.

  • 11
  • 4
right-top-arrow
9

Docker Image: A Detailed Guide

Updated on 30/08/2024460 Views

Introduction

A Docker Image includes everything needed to run a specific application. It tells the container what software to run and how to set it up. 

A Docker Container is like a mini-computer that runs an application using this package. It includes all the necessary parts to make the application work smoothly, no matter where it's being used.

Let us begin by understanding What a Docker Image is.

What is a Docker Image?

A Docker image is developed by using the DockerFile, which comprises a set of instructions needed to containerize an application. The Docker image includes a number of pieces of software. It is a platform-independent image built in a Windows environment that can be pushed in the Docker Hub and pulled by others with various OS environments, like Linux.

  • Application code
  • Runtime
  • Libraries
  • Environmental Tools

Now, let us understand the Layers in a Docker Image.

Docker Image

Source: https://medium.com/@mrdevsecops/docker-objects-e561f0ce3365

What are the Layers in a Docker Image?

Docker images have multiple layers, each building on the previous one but with some differences. These layers make Docker builds faster and more efficient by reusing parts that haven't changed, saving disk space, and avoiding unnecessary data transfers.

Each layer in an image is read-only. When a container is build, a writable layer is added, allowing you to make changes.

When discussing disk space in Docker, it's important to understand the difference between size and virtual size. Size refers to the space used by a container's writable layer. Virtual size includes both the writable layer and the shared read-only layers from the image. Multiple containers can share these read-only layers from the same image.

Now, moving ahead, let us understand the use cases of Docker Image.

Docker Images: Real-life Use Cases

Below are the use cases of Docker Image:

  1. We can quickly and effectively run a container with docker images.
  2. Each code, configuration settings, environmental variables, and runtime are included in a Docker image.
  3. A layer is a building block for an image.

Now, let us understand the difference between Docker Images and Docker Container.

Difference Between Docker Images vs Docker Container

The below differences give you a clear picture of Docker Image and Docker Container.

Docker Image

Docker Container

The docker image is the source of the docker container

Whereas, the Docker contains an instance of a Docker image

Dockerfile is a priority in a Docker Image

Docker Image is a prerequisite in any Docker Container

Docker Images are shared between users via a Docker registry

Docker Containers cannot be shared between users

Modifications must be made to a Dockerfile to make changes to any Docker Image

Changes can be made directly to a Docker Container by integrating with it.

Now, let us understand the Subcommands of a Docker Image.

What are the SubCommands in a Docker Image?

 Here are some of the subcommands commonly used with Docker Image:

  • Docker image build: This command builds an image from the Dockerfile.
  • Docker image history: It shows the history of the Docker image.
  • Docker image inspection: It displays detailed information on one or more images.
  • Docker image prune: This command removes unused images not associated with containers.
  • Docker image save: It saves Docker images into tar archive files.
  • Docker image tag: This command creates a tag for the target image that refers to the source image.

Docker Prune

The `docker image prune` command removes unused Docker images from the Docker host.

docker image prune

Unused images, or dangling images, are not associated with any containers.

Docker Image Build

The following command 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: Assigns a name and, optionally, a tag to the image you're creating.

-f path/to/Dockerfile: Specifies the location of the Dockerfile. Provide the correct path if it's not in the current directory. The dot (.) represents the current working directory.

Docker Image Tag

Docker tags are labels for container images used to differentiate versions and variants of an image during development and deployment. These tags help you identify and distinguish between different versions of Docker images. Using Docker tags enables efficient version control and management, facilitating quick and reliable continuous deployment.

Now, moving further, let us understand the Structure of Docker Image.

What is the Structure of Docker Image?

The software layers that make up a Docker image simplify the configuration of dependencies needed to run a container.

  1. Base Image: The base image is the start point for most Dockerfiles and can be created from scratch.
  2. Parent Image: Our Docker image is built upon the parent image. In the Dockerfile, we refer to the parent image using the `FROM` command, and each subsequent instruction modifies the parent image.
  3. Layers: Docker images consist of multiple layers. Each layer builds upon the one prior, creating intermediary images.

Now, moving forward, let us create a Docker Image. 

How to create a Docker Image?

To generate a Docker Image and launch it as a container, follow these steps:

Step 1: Make a Dockerfile with the building instructions for the image.

Step 2: To create the Docker image with all required dependencies, type the following command into the terminal:

build-t docker :.

The process of creating an image begins with this command.

Step 3: Move on to the following step after the Dockerfile and associated image have been successfully produced.

Step 4: To launch the program and build a running container with all necessary dependencies, type the following command into the terminal:

Docker run -p : at 9000:80

The port for accessing the application is '9000' in this command, whereas the port that the container exposes for host access is '80'.

Moving ahead, let us understand how to build Docker Python Images.

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

Step 1: Create A Dockerfile

Make a Dockerfile specifically for Python programs to start. Provide instructions on how to use the CMD directive to launch the Python application. This is an example of a Dockerfile:

Dockerfile

As a basis, use the official Python image from Python:3.9-thin

In the container, set the working directory.

WORKDIR/application

Move the contents of the current directory to the container located at /app.

COPY. /application

Install any dependencies needed, as listed in requirements.txt.

USE pip install -r requirements.txt -no-cache-dir

Open port 80 to the outside world from this container.

DISCLOSE 80

Explain the environment variable.

World ENV NAME

When the container launches CMD ["python," "app.py"], execute app.py.

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

Next, include an app.py file with the Python program and a requirements.txt file with the names of the necessary applications. Here is an example of what's in these files:

requirements.txt:

plaintext

Flask==2.1.0 app.py: import Python from Flask Flask __name__ = Flask(@app.route('/'))

return 'Hello, World!' from function hello_world()

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

Step 3: Build a Docker Image

To create the Docker Python image from the Dockerfile, use the following command:

build -t my python-app:v1 with bash docker image.

Step 4: Verify the Docker Image

Check the successful build of the Docker application with the following command:

bash

docker images

Step 5: Run a Python-based Docker Container

Run a container using the Docker Python image with the following command:

bash

docker run -dit -p 80:80 --name mycontainer1 my python-app:v1

Step 6: Access the Docker Container

You can access the Docker container with the public IP of the host system and the exposed port to view the successful run of the containerized Python application.

Docker Image Commands

List Docker Images

Use the below command to list Docker images:

bash

docker images

Pull a Docker Image From a Registry

Pull a Docker image from a registry by using the command:

bash

docker image pull <image-name>

Docker Images Prune

Remove unused Docker images not associated with any containers with the command:

bash

docker image prune

Docker Images Filter

Filter specific images by labels or meta-information tags using the command:

bash

docker image ls -f "reference=python-app"

Remove an Image from the Docker

Remove a Docker image from the local registry using the command:

bash

docker rmi <id-of-image>

Specific Image Search on Docker Hub

Search for a specific image on Docker Hub with the command:

bash

docker search ubuntu

Wrapping Up!

In conclusion, Docker images and containers revolutionize applications' deployment and management. Docker images package all necessary components, including code, dependencies, and runtime, making them portable and easy to deploy across various environments. 

Docker containers leverage these images to create lightweight, isolated environments that ensure consistency and reliability in application execution. With Docker, developers can streamline the development and deployment process, increasing efficiency and scalability in modern software development practices.

Docker Image: FAQs

1. What is a Docker image?

A Docker image is like a box containing everything your computer program needs to run. It includes the program, all the extra stuff it needs, and even the settings. This box makes moving your program from one computer to another easy.

2. What is the purpose of Docker?

Docker simplifies building, sending, and using computer programs. It wraps up your program and all its parts into one neat package, making it easier to run your program on different computers without worrying about setup problems.

3. Is a Docker image a Dockerfile?

Nope! A Docker image is the finished package with everything your program needs to run. A Dockerfile is more like a recipe that tells Docker how to assemble that package.

4. How do I build a Docker image?

To build a Docker image, you write a simple set of instructions called a Dockerfile. Then, you tell Docker to follow those instructions and make the image. It's like giving directions to build a LEGO set.

5. What is a container image?

A container image is just another name for a Docker image. It's like a lunchbox that holds your program and all its stuff, ready to be opened and used whenever needed.

6. How do I run a Docker image?

To run a Docker image, you use a special command that tells Docker to open the lunchbox and start your program. It's like saying, "Hey, Docker, let's use this lunchbox now!"

7. What are the benefits of Docker?

Docker makes it easy to share and run programs by packaging everything neatly. It keeps programs running smoothly, no matter where they go and saves time by ensuring everything works the same everywhere.

8. What is Docker and Kubernetes?

Docker is like a chef that packs your program into a lunchbox. Kubernetes is like a waiter who helps serve many lunchboxes at once, ensuring each gets to the right place and works well.

9. Where are Docker images stored?

Docker images are kept in a special online storage called a registry. It's like a library where you can put your lunchboxes so others can use them. The most popular one is Docker Hub, but you can also have your private storage.

Abhimita Debnath

Abhimita Debnath

Abhimita Debnath is one of the students in UpGrad Big Data Engineering program with BITS Pilani. She's a Senior Software Engineer in Infosys. She…Read More

Talk to Career Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...