๐Ÿ› ๏ธ Docker Architecture

โš™๏ธ Docker Engine

Docker Engine is responsible for building, running, and managing containers.

It consists of:

  • Docker Daemon (dockerd) โ€“ Runs in the background and listens for API requests
  • Docker CLI (docker) โ€“ Used to interact with the daemon
  • REST API โ€“ Allows communication between the Docker CLI and daemon

๐Ÿ”Œ Default Docker Ports

PortPurpose
2376Secure (TLS/SSL) communication between Docker hosts and clients
2375Insecure, non-TLS communication

๐ŸŒ Remote Docker Engine

Used to execute Docker commands on a remote machine.

Example: Running an Nginx container on a remote Docker host:

docker -H=<Remote_IP>:2375 run nginx

Use 2376 for secure communication with TLS encryption.

๐Ÿ“‚ Dockerโ€™s Layered Architecture

  • Docker uses a layered file system, where each layer is immutable
  • If a build fails at a certain layer, Docker can cache previous layers and restart from the failed layer

๐Ÿ›‘ Key Benefits of Layered Architecture

โœ… Improved build efficiency with caching
โœ… Reduces storage requirements
โœ… Enables portability across different environments
โœ… If two Dockerfiles have the same set of instructions as start except for the source code, Docker reuses the cache existing layers from the first build, reducing redundancy and speeding up the build process

๐Ÿ› ๏ธ Container Layer (Writable Layer)

  • When a container runs from an image, Docker adds a thin writable layer on top of the imageโ€™s layers
  • This writable layer allows modifications like file creation or configuration changes
  • Once the container is stopped, the writable layer is deleted unless explicitly saved
+--------------------+
| Writable Layer OR  |
| Container Layer    |
+--------------------+
|  Layer 3 (RUN)     |
+--------------------+
|  Layer 2 (COPY)    |
+--------------------+
|  Layer 1 (FROM)    |
+--------------------+
|  Base Image (OS)   |
+--------------------+

๐Ÿ”„ Copy-on-Write (CoW) Mechanism

  • Docker images are immutable, meaning they cannot be modified once created
  • When a container is started from an image, a read-write layer (container layer) is added on top of the existing read-only image layers
  • If a file inside the container needs modification, Docker copies it from the image layer to the writable container layer before making changes
  • This approach ensures that the original image remains unchanged, allowing multiple containers to share the same image efficiently

โš ๏ธ Data Persistence in Containers

  • The writable container layer is temporaryโ€”any changes made inside the container are lost once the container is stopped or deleted
  • To persist data, use volumes, which store data outside the containerโ€™s ephemeral storage

๐Ÿ’พ Creating and Using Docker Volumes

1๏ธโƒฃ Create a volume:

docker volume create data_volume

2๏ธโƒฃ Mount the volume to a container:

docker run -v data_volume:/app/data my_container

This ensures that data remains intact even if the container is stopped or removed.