Docker Fundamentals - Complete Interview Guide

Learn Docker fundamentals including containerization, Docker architecture, images, containers, Dockerfile, networking, storage, registries, and essential Docker commands for developers and DevOps engineers.

Introduction

Docker is an open-source containerization platform that allows developers to package applications along with their dependencies into lightweight, portable, and isolated containers.

Containers solve the classic "It works on my machine" problem by ensuring applications run consistently across development, testing, staging, and production environments.

Today Docker is widely used with Spring Boot, Microservices, Kubernetes, CI/CD pipelines, and every major cloud platform.


Learning Objectives

After completing this chapter, you should understand:

  • What is Docker?
  • Why Docker?
  • Virtual Machines vs Containers
  • Containerization
  • Docker Architecture
  • Docker Components
  • Docker Engine
  • Docker Client
  • Docker Daemon
  • Docker Images
  • Docker Containers
  • Dockerfile
  • Docker Registry
  • Docker Hub
  • Image Lifecycle
  • Container Lifecycle
  • Volumes
  • Bind Mounts
  • Docker Networking
  • Environment Variables
  • Port Mapping
  • Image Layers
  • Common Docker Commands
  • Enterprise Best Practices

What is Docker?

Docker is a platform that packages an application and all its dependencies into a container.

Containers are:

  • Lightweight
  • Portable
  • Fast
  • Consistent
  • Isolated
  • Platform Independent

Why Docker?

Without Docker:

  • Different environments
  • Dependency conflicts
  • Manual installations
  • Difficult deployments
  • Environment inconsistencies

With Docker:

  • Consistent environments
  • Faster deployment
  • Easy scaling
  • Better resource utilization
  • Portable applications

What is Containerization?

Containerization packages:

  • Application
  • Runtime
  • Libraries
  • Dependencies
  • Configuration

into a single executable unit called a Container.


Evolution of Application Deployment

Traditional Server

Application
Application
Application

↓

Virtual Machines

VM
Guest OS
Application

↓

Containers

Container
Application
Libraries

↓

Docker

Virtual Machine vs Container

Virtual Machine Container
Includes Guest OS Shares Host Kernel
Heavyweight Lightweight
Slow Startup Fast Startup
High Memory Usage Low Memory Usage
Large Size Small Size
Hypervisor Required Docker Engine Required

Docker Architecture

Developer

      │

Docker CLI

      │

Docker Engine

      │

Docker Daemon

      │

Docker Images

      │

Docker Containers

Docker Components

Main components:

  • Docker Client
  • Docker Engine
  • Docker Daemon
  • Docker Images
  • Docker Containers
  • Docker Registry
  • Docker Hub

Docker Client

The Docker Client is the command-line interface.

Example commands:

docker build

docker run

docker ps

docker images

Docker Daemon

The Docker Daemon:

  • Builds images
  • Runs containers
  • Manages networks
  • Manages storage
  • Pulls images
  • Pushes images

Docker Engine

Docker Engine consists of:

  • Docker CLI
  • Docker Daemon
  • REST API

It is responsible for managing containers.


Docker Registry

A Docker Registry stores Docker Images.

Examples:

  • Docker Hub
  • Amazon ECR
  • Azure Container Registry
  • Google Artifact Registry
  • Harbor
  • JFrog Artifactory

Docker Hub

Docker Hub is the default public image repository.

Popular Images:

  • nginx
  • mysql
  • postgres
  • redis
  • eclipse-temurin
  • openjdk
  • ubuntu

Docker Image

A Docker Image is a read-only template used to create containers.

An image contains:

  • Application
  • Runtime
  • Libraries
  • Dependencies
  • Configuration

Docker Container

A Container is a running instance of a Docker Image.

Example:

Image

↓

Container

One image can create multiple containers.


Docker Image Lifecycle

Dockerfile

↓

Build Image

↓

Store Image

↓

Push Image

↓

Pull Image

↓

Run Container

Docker Container Lifecycle

Create

↓

Start

↓

Running

↓

Stop

↓

Restart

↓

Remove

Dockerfile

A Dockerfile contains instructions to build a Docker Image.

Common Instructions:

  • FROM
  • RUN
  • COPY
  • ADD
  • WORKDIR
  • CMD
  • ENTRYPOINT
  • EXPOSE
  • ENV
  • ARG
  • LABEL
  • USER

Basic Dockerfile

FROM eclipse-temurin:21-jdk

WORKDIR /app

COPY target/app.jar app.jar

EXPOSE 8080

ENTRYPOINT ["java","-jar","app.jar"]

Building an Image

docker build -t payment-service .

Listing Images

docker images

Running a Container

docker run payment-service

Detached Mode

docker run -d payment-service

Listing Containers

Running Containers

docker ps

All Containers

docker ps -a

Stopping a Container

docker stop <container-id>

Starting a Container

docker start <container-id>

Restarting a Container

docker restart <container-id>

Removing a Container

docker rm <container-id>

Removing an Image

docker rmi <image-id>

Viewing Logs

docker logs <container-id>

Follow logs

docker logs -f <container-id>

Executing Commands Inside a Container

docker exec -it <container-id> bash

Inspecting a Container

docker inspect <container-id>

Docker Image Layers

Docker images are built using layers.

Application

↓

Dependencies

↓

JDK

↓

Operating System

↓

Base Image

Benefits:

  • Faster builds
  • Smaller downloads
  • Layer caching
  • Reusability

Docker Volumes

Volumes provide persistent storage.

Container

↓

Volume

↓

Host Storage

Benefits:

  • Persistent data
  • Easy backup
  • Shared storage

Bind Mount

A Bind Mount maps an existing host directory into a container.

Host Folder

↓

Container Folder

Useful during development.


Docker Networking

Docker supports multiple network drivers.

  • Bridge
  • Host
  • None
  • Overlay
  • Macvlan

Bridge Network

Default Docker network.

Container A

↓

Bridge Network

↓

Container B

Port Mapping

Expose application ports.

Example:

docker run -p 8080:8080 payment-service

Meaning:

Host Port

↓

Container Port

Environment Variables

Pass configuration during runtime.

Example:

docker run -e SPRING_PROFILES_ACTIVE=prod payment-service

Docker Registry Workflow

Developer

↓

Docker Build

↓

Docker Image

↓

Docker Hub

↓

Production Server

↓

Docker Pull

↓

Container

Common Docker Commands

Command Purpose
docker version Docker version
docker info Docker information
docker images List images
docker ps Running containers
docker ps -a All containers
docker build Build image
docker run Run container
docker stop Stop container
docker start Start container
docker restart Restart container
docker logs View logs
docker exec Execute command
docker inspect Inspect resources
docker rm Remove container
docker rmi Remove image
docker pull Download image
docker push Upload image

Docker Workflow

Write Dockerfile

↓

Build Image

↓

Run Container

↓

Test

↓

Push Image

↓

Deploy

Enterprise Docker Workflow

Developer

↓

Git

↓

CI Pipeline

↓

Docker Build

↓

Docker Image

↓

Container Registry

↓

Kubernetes

↓

Production

Common Use Cases

Docker is commonly used for:

  • Spring Boot Applications
  • Java Microservices
  • REST APIs
  • Databases
  • Message Brokers
  • CI/CD Pipelines
  • Cloud Deployments
  • Kubernetes Workloads

Best Practices

  • Use official base images.
  • Keep images small.
  • Use specific image versions instead of latest.
  • Write efficient Dockerfiles.
  • Use .dockerignore.
  • Store secrets outside images.
  • Use volumes for persistent data.
  • Run containers as non-root users.
  • Remove unused images and containers.
  • Scan images for vulnerabilities.

Interview Summary

After completing this chapter, you should understand:

  • Docker
  • Containerization
  • Containers vs Virtual Machines
  • Docker Architecture
  • Docker Engine
  • Docker Daemon
  • Docker Client
  • Docker Images
  • Docker Containers
  • Dockerfile
  • Image Layers
  • Container Lifecycle
  • Docker Registry
  • Docker Hub
  • Volumes
  • Bind Mounts
  • Networking
  • Port Mapping
  • Environment Variables
  • Docker Commands
  • Enterprise Docker Workflow

Next Chapter

➡️ Docker Advanced

Topics include:

  • Docker Internals
  • OCI Runtime
  • containerd
  • runc
  • Namespaces
  • cgroups
  • Multi-stage Builds
  • Docker Compose
  • BuildKit
  • OverlayFS
  • Resource Limits
  • Health Checks
  • Security
  • Rootless Containers
  • Registry Management
  • CI/CD Integration
  • Production Best Practices