What is Docker and How Does It Work? A Beginner’s Guide

Rishi BakshiRishi Bakshi
6 min read

What is Docker?

Docker is an open-source platform that allows developers to build, package, and run applications in containers.

What is a container?

A container is a lightweight, portable, and isolated environment that includes everything an application needs to run, code, runtime, system tools, libraries, and settings, making it work consistently across different environments (development, testing, production).

History of Docker

Docker originated from a company called DotCloud, founded in 2008 by Solomon Hykes as a Platform-as-a-Service (PaaS) provider. In 2013, DotCloud open-sourced a project called Docker, which introduced a new way to package and run applications using containers. This innovation quickly gained traction among developers due to its simplicity, portability, and efficiency. Recognizing the potential of Docker, DotCloud rebranded itself as Docker, Inc. and shifted its focus entirely to container technology. Over the next few years, Docker played a key role in popularizing containerization, becoming a foundational tool in modern DevOps and cloud-native development. In 2019, Docker, Inc. sold its enterprise business to Mirantis to focus more on improving the developer experience, while container orchestration tools like Kubernetes took the lead in managing large-scale deployments.

Virtualization vs Containerization

Virtualization and containerization are technologies used to run multiple applications in isolated environments on a single physical machine. While they serve similar purposes, they differ significantly in how they achieve isolation, resource efficiency, and performance.

Virtualization

Virtualization involves using a hypervisor (like VMware, VirtualBox, or Hyper-V) to create and manage virtual machines (VMs). Each VM runs a full operating system (OS) along with the application and its dependencies. These VMs are completely isolated from each other and from the host system.

  • Architecture: Includes the host OS, hypervisor, guest OS (for each VM), and application.

  • Resource Usage: High, as each VM needs its own OS.

  • Boot Time: Relatively slow (can take minutes).

  • Isolation: Strong β€” each VM is completely independent.

  • Use Case: Suitable for running multiple different OS environments or legacy systems.

Containerization

Containerization uses platforms like Docker, podman, container D to run applications in containers. Unlike VMs, containers share the host OS kernel and run as isolated processes. Each container includes only the application and its dependencies, making them lightweight and fast.

  • Architecture: Host OS, container engine (like Docker), and multiple containers sharing the host OS kernel.

  • Resource Usage: Low, no need for a separate OS in each container.

  • Boot Time: Fast (within seconds).

  • Isolation: Lightweight but sufficient for most use cases.

  • Use Case: Ideal for microservices, CI/CD pipelines, and cloud-native applications.

FeatureVirtualizationContainerization
OS per InstanceFull OS per VMShared Host OS kernel
Resource UsageHighLow
Startup TimeSlow (minutes)Fast (seconds)
Isolation LevelStrong (complete OS-level separation)Moderate (process and namespace-level)
PortabilityModerateHigh
Use CasesLegacy apps, OS diversityCloud-native apps, microservices

Docker Architecture

What is Docker Engine?

Docker Engine is the core component of Docker that allows you to build, run, and manage containers on a system. It acts as the client-server application responsible for all container-related operations.

Key Components of Docker Engine:

Docker Daemon (dockerd)

  • Runs in the background.

  • Responsible for creating, running, and managing Docker containers.

  • Listens to Docker API requests and interacts with containers, images, networks, and volumes.

Docker Client (docker)

  • The command-line interface (CLI) used by users.

  • Sends commands to the Docker Daemon (e.g., docker run, docker build).

Docker REST API

  • The communication bridge between the Docker Client and Daemon.

  • Enables remote and programmatic control over Docker.

Docker CLI

The Docker CLI (Command-Line Interface) is the user-facing tool that allows you to interact with the Docker Engine through typed commands in a terminal or command prompt.

It communicates with the Docker Daemon (dockerd) via the Docker REST API to perform tasks like building images, managing containers, networks, and volumes.

CommandDescription
docker versionShows Docker client and server versions.
docker infoDisplays system-wide information.
docker pull <image>Downloads an image from Docker Hub.
docker build -t <name> .Builds a Docker image from a Dockerfile.
docker imagesLists all local Docker images.
docker run <image>Runs a container from an image.
docker psLists running containers.
docker ps -aLists all containers (including stopped).
docker stop <container>Stops a running container.
docker rm <container>Removes a container.
docker rmi <image>Removes an image.
docker exec -it <container> bashRuns an interactive shell inside a running container.

☁️ Docker on AWS EC2

1. Connect to Your EC2 Instance

chmod 400 "docker-in-one-shot.pem"

ssh -i "docker-in-one-shot.pem" ubuntu@ec2-13-222-59-215.compute-1.amazonaws.com

2. Install Docker on EC2

sudo apt-get install docker.io-y

3. Check Docker Status

sudo systemctl status docker

4. Run Docker (Handling Permission Denied Error)

If you see an error like:

permission denied while trying to connect to the Docker daemon socket

Run the following commands:

sudo usermod -aG docker $USER

newgrp docker

Then retry:

docker ps

πŸ“¦ Docker Workflow Summary

# Login to Docker Hub
docker login -u <username>

# Pull an image
docker pull <image_name>

# Run a test container
docker run hello-world

# Run in detached mode
docker run -d hello-world

# Stop a running container
docker stop <container_id>

πŸ“„ Dockerfile Example (Java App)

# Pull a base image with required libraries
FROM openjdk:17-jdk-alpine

# Create a working directory inside the container
WORKDIR /app

# Copy Java source code from host to container
COPY src/Main.java /app/Main.java

# Compile the Java source code
RUN javac Main.java

# Run the compiled Java application
CMD ["java", "Main"]

Build and Run the Java App

docker build -t javaapp .

  • docker build: Tells Docker to create a new image from a Dockerfile.

  • -t javaapp: Tags (names) the image as javaapp, so you can refer to it easily later.

  • . (dot): Specifies the current directory as the build context, where Docker will look for the Dockerfile and any files needed for the build.

docker run javaapp

What it does:

  • docker run: Tells Docker to create and start a new container from a specified image.

  • javaapp: The name of the image to run (which we built in the previous step).

πŸš€ Run a Flask App with Port Binding

docker run -p 80:80 flaskapp

What it does:

  • docker run: Starts a new container from an image.

  • -p 80:80: Maps ports between the host machine and the Docker container.

    • The first 80 (before the colon) is the host port β€” the port on your computer.

    • The second 80 (after the colon) is the container port β€” the port exposed by the Flask app inside the container.

  • flaskapp: The name of the Docker image you want to run (it must be built already).

0
Subscribe to my newsletter

Read articles from Rishi Bakshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rishi Bakshi
Rishi Bakshi