Working with Docker Images and Layers (Beginner-Friendly Guide with Examples)

To use Docker effectively, you must understand Docker Images and how they are built using layers. This concept is very important because it directly affects performance, storage, and deployment speed.

This guide explains images and layers in a simple way with examples, commands, and real-world usage. :contentReference[oaicite:0]{index=0}

Related internal topics: /docker-architecture, /docker-cli-commands, /dockerfile-tutorial, /docker-installation.

1. What is a Docker Image?

A Docker image is a read-only template used to create containers. It contains everything required to run an application:

  • Application code
  • Runtime (Java, Node.js, Python)
  • Libraries and dependencies
  • Environment settings

Simple Example

docker pull nginx

Explanation:

  • Downloads nginx image from Docker Hub
  • Stores it locally

Java Analogy

Class Car {
   // blueprint
}

Image = Blueprint (Class)

2. What is a Docker Container?

A container is a running instance of an image.

Example

docker run nginx

Explanation:

  • Creates container from image
  • Runs the application

Java Analogy

Car myCar = new Car();

Container = Object (Instance)

3. Understanding Docker Layers

Docker images are not a single file. They are made of multiple layers. Each layer represents a change or instruction.

Layer Diagram

[ Writable Container Layer ]
-----------------------------
[ Application Code Layer ]
-----------------------------
[ Dependencies Layer ]
-----------------------------
[ Runtime Layer (Java/Node) ]
-----------------------------
[ Base OS Layer ]

Each step in a Dockerfile creates a new layer.

Example Dockerfile

FROM openjdk:17
COPY app.jar app.jar
RUN echo "Setup done"
CMD ["java", "-jar", "app.jar"]

Layer Explanation:

  • FROM → Base OS + Java layer
  • COPY → Adds application code layer
  • RUN → Adds setup layer
  • CMD → Final execution layer

4. Copy-on-Write (Important Concept)

When a container starts, Docker adds a writable layer on top of image layers.

Flow

Image Layers (Read-Only)
        |
        v
Writable Container Layer
        |
        v
Changes stored here only

Meaning:

  • Original image remains unchanged
  • Changes are temporary
  • Deleting container removes changes

5. Useful Docker Image Commands

Pull Image

docker pull nginx

Downloads image from registry.

List Images

docker images

Shows all images.

Inspect Image

docker inspect nginx

Shows detailed information.

Remove Image

docker rmi nginx

Deletes image.

View Image Layers

docker history nginx

Shows how image was built layer by layer.

6. Practical Example

Run a Java application using Docker image:

docker run -d -p 8080:8080 my-java-app

Explanation:

  • -d → Run in background
  • -p → Map ports
  • my-java-app → Image name

Open browser: http://localhost:8080

7. Real-World Use Cases

1. Faster Builds using Layer Caching

If layers do not change, Docker reuses them, saving time.

2. Microservices Optimization

Multiple services can share same base layers.

3. Storage Efficiency

Common layers are stored only once.

4. Consistent Deployment

Same image works in all environments.

8. Flow Chart: Image to Container

Build Image
     |
     v
Store Image Locally
     |
     v
Run Container
     |
     v
Writable Layer Created
     |
     v
Stop/Delete Container

9. Common Mistakes to Avoid

1. Too Many Layers

Too many instructions increase image size.

2. Storing Secrets

Never store passwords in Dockerfile.

3. Not Cleaning Cache

Temporary files increase size.

4. Modifying Running Containers

Always update image instead.

5. Not Using Layer Optimization

Poor ordering slows build.

10. Interview Notes

Difference between Image and Container?

Image = template, Container = running instance.

What is Docker Layer?

Each instruction creates a layer.

What is Writable Layer?

Top layer used for runtime changes.

How Docker saves space?

By sharing layers across images.

What is Layer Caching?

Reuse unchanged layers to speed build.

11. Learning Path

  • /dockerfile-tutorial
  • /docker-compose-guide
  • /kubernetes-introduction

12. Summary

Docker images are made of multiple layers. Each layer represents a step in building the application. Containers run on top of these layers with a writable layer.

This layered architecture helps in faster builds, efficient storage, and reusable components. Understanding this concept is essential for writing optimized Dockerfiles and building scalable applications.

Next step: Learn how to write efficient Dockerfiles in /dockerfile-tutorial.