Understanding Docker Architecture: An In-depth Overview of Docker Components and Usage

Praveen Dandu
6 min readAug 8, 2023

--

Introduction:

Docker has revolutionized the way we build, ship, and run applications. Its architecture provides a lightweight and efficient platform for deploying applications in isolated containers. In this blog post, we’ll dive into the core components of Docker and explore their functionalities through detailed explanations and illustrative code examples.

Table of Contents:

  1. What is Docker?
  2. Docker Architecture Overview
  • Docker Engine
  • Images
  • Containers
  • Registries

3. Exploring Docker Components in Detail

  • Docker Engine
  • Images and Containers
  • Registries

4. Getting Hands-on: Step-by-step Examples

  • Installing Docker
  • Pulling and Running Docker Images
  • Building Custom Docker Images
  • Creating and Managing Containers
  • Working with Docker Registries

5. Best Practices for Docker Usage

6. Conclusion

1. What is Docker?

Docker is a platform that enables developers to automate the deployment of applications within lightweight, portable containers. These containers package an application and its dependencies, ensuring consistent behavior across different environments.

2. Docker Architecture Overview:

Docker Engine: The core of Docker architecture, responsible for building, running, and managing containers.
Images: Immutable templates used to create containers. Images consist of a base image and layers containing changes.
Containers: Running instances of Docker images. Containers are isolated from one another and from the host system.
Registries: Repositories for storing and distributing Docker images, facilitating collaboration and sharing.

3. Exploring Docker Components in Detail:

Docker Engine:

The Docker Engine comprises three main components:

Docker Daemon: The background service responsible for managing Docker containers on the host system.
REST API: An interface for interacting with Docker and managing containers programmatically.
Command Line Interface (CLI): A user-friendly tool for interacting with Docker through commands.

Images and Containers:

Docker images are created from Dockerfiles, which contain instructions for building an image layer by layer. Containers are instances of images, providing an isolated environment for running applications. Layers within images enable efficient storage and sharing of common components.

Let’s walk through a practical example of creating a Docker image and running a container based on that image.

Example: Creating a Simple Nginx Web Server Container

In this example, we’ll create a Docker image that contains a simple Nginx web server. We’ll then run a container using that image.

Step 1: Writing a Dockerfile

Create a file named Dockerfile (with no file extension) in a directory of your choice. This file contains instructions for building the Docker image.

# Use an official Nginx base image
FROM nginx:alpine

# Copy a custom HTML file to the container
COPY index.html /usr/share/nginx/html/

Step 2: Create an HTML File

Create an index.html file in the same directory as the Dockerfile. This will be the content displayed by the Nginx web server.

<!DOCTYPE html>
<html>
<head>
<title>Hello Docker!</title>
</head>
<body>
<h1>Hello, Docker!</h1>
<p>This is a simple Docker container running an Nginx web server.</p>
</body>
</html>

Step 3: Build the Docker Image

Open a terminal, navigate to the directory containing the Dockerfile and index.html, and run the following command to build the Docker image:

docker build -t my-nginx-image .

The -t flag assigns a name (my-nginx-image) to the image. The . at the end specifies the build context (current directory).

Step 4: Run a Container from the Image

Now that we have our Docker image, let’s run a container based on it. Run the following command:

docker run -d -p 8080:80 my-nginx-image
  • The -d flag runs the container in detached mode.
  • The -p flag maps port 8080 on your host machine to port 80 on the container.
  • my-nginx-image is the name of the image we created.

Step 5: Access the Web Server

Open a web browser and navigate to http://localhost:8080. You should see the contents of the index.html file displayed by the Nginx web server running inside the Docker container.

Step 6: Stop and Remove the Container

To stop the container, run:

docker stop <container_id_or_name>

Replace <container_id_or_name> with the actual ID or name of the container.

To remove the container, run:

docker rm <container_id_or_name>

Registries:

Docker images can be stored in public or private registries. Docker Hub is a popular public registry. Private registries allow organizations to control image distribution and access.

Let’s extend the previous example by pushing the Docker image we created to a Docker registry and then pulling and running it on a different machine.

Example: Pushing and Pulling Docker Images to/from a Registry

In this example, we’ll push the Nginx Docker image we created earlier to Docker Hub, a popular public Docker registry. Then, we’ll pull the image from the registry and run it on a different machine.

Step 1: Push the Docker Image to Docker Hub

  1. Create an account on Docker Hub (https://hub.docker.com/) if you don’t have one.
  2. Log in to Docker Hub using the command-line interface:
docker login

Follow the prompts to provide your Docker Hub username and password.

  1. Tag the Docker image with your Docker Hub username and a repository name:
docker tag my-nginx-image:latest yourusername/my-nginx-image:latest

Replace yourusername with your actual Docker Hub username.

  1. Push the tagged image to Docker Hub:
docker push yourusername/my-nginx-image:latest

Step 2: Pull and Run the Docker Image on Another Machine

On a different machine (or after cleaning up the previous environment):

  1. Log in to Docker Hub if you haven’t already:
docker login
  1. Pull the image from Docker Hub:
docker pull yourusername/my-nginx-image:latest
  1. Run a container using the pulled image:
docker run -d -p 8080:80 yourusername/my-nginx-image:latest

Step 3: Access the Web Server

Open a web browser and navigate to http://localhost:8080. You should see the same contents as before, indicating that you've successfully pulled and run the Docker image from the registry.

Step 4: Stop and Remove the Container

To stop and remove the container, follow the same steps as in the previous example:

docker stop <container_id_or_name>
docker rm <container_id_or_name>

4. Getting Hands-on: Step-by-step Examples:

Installing Docker: Follow the official documentation to install Docker on your system.

Pulling and Running Docker Images: Use the docker pull command to fetch images from a registry. Run containers using the docker run command.

Building Custom Docker Images: Create a Dockerfile with instructions to build a custom image. Use the docker build command to generate the image.

Creating and Managing Containers: Use docker create to create a container from an image. Manage containers using commands like docker start, docker stop, and docker exec.

Working with Docker Registries: Push your custom image to a registry using docker push. Pull images from registries using docker pull.

5. Best Practices for Docker Usage:

  • Use official base images to ensure security and reliability.
  • Keep images small by minimizing layers and removing unnecessary components.
  • Implement container orchestration tools like Kubernetes for managing complex deployments.
  • Regularly update and patch images to address security vulnerabilities.

6. Conclusion:

Docker’s architecture provides a powerful solution for packaging and deploying applications in containers. Understanding its components empowers developers to build, share, and run applications efficiently across various environments. By following best practices, you can harness the full potential of Docker for your software development and deployment needs.

In this blog post, we’ve explored Docker’s architecture, key components, and provided hands-on examples to get you started on your Docker journey. With Docker’s ability to simplify application deployment and management, it’s a must-have tool in the modern developer’s toolkit.

Thank you for reading my blog! If you enjoyed the content and want to receive timely updates whenever I publish a new article, make sure to follow me on Medium. It’s quick and easy! Just log in to your Medium account (or create one if you haven’t already), visit my profile, and click the “Follow” button. Stay informed and never miss a new post! Your support means a lot to me, and I’m excited to continue sharing valuable insights with you. Happy reading! 🚀

--

--

Praveen Dandu
Praveen Dandu

Written by Praveen Dandu

🚀 DevOps Engineer | Automating Infrastructure, Streamlining Deployments | Continuous Integration & Delivery Specialist https://www.linkedin.com/in/pravin24/

No responses yet