Skip to content

Introduction to Docker and Kubernetes

Topics to be covered

  1. Introduction
  2. Docker Overview
    • Key Docker Concepts:
    • Practical demonstration of creating Docker images and running containers.
  3. Kubernetes Overview
    • Architecture
    • Important Terms
  4. Managing microservices using Kubernetes (K8S).

Introduction to Docker and Kubernetes

The Software Engineering Process

Before understanding Docker and Kubernetes, it's essential to see where they fit within the broader software engineering process. Typically, this process involves several stages:

  1. Requirements Gathering:
    • This is the first step, where the team collects and defines what the software should achieve, specifying user needs and system functionalities.
  2. Design:
    • Based on the requirements, the system architecture and design are created. This step defines how the different components of the system will interact.
  3. Coding:
    • Developers begin to write the software according to the design specifications. This step involves the actual programming.
    • At this point, the developer's role in the process is significant.
  4. Testing:
    • After coding, the software undergoes testing to ensure it functions as intended and is free of bugs. Testing can happen both manually and automatically.
    • This stage is crucial before deployment to catch potential issues early.
  5. Deployment:
    • The deployment process makes the feature or system live, available for end-users. This involves setting up the necessary environment (server, dependencies, configurations) to run the application.
    • A DevOps team typically handles this step, ensuring that the software runs as expected on the server.
  6. Monitoring:
    • Post-deployment, monitoring ensures that the system remains stable and performs well. This includes setting up alerts, server maintenance, and backups to handle any issues that might arise.

Important Note: While Docker and Kubernetes are primarily used in the deployment and monitoring phases, they are crucial tools for DevOps teams. They provide efficient ways to deploy, scale, and manage applications in production environments.


Understanding Docker

The Problem Without Docker

Consider the scenario where you are developing a Python project and want to deploy it on an AWS server. Without Docker, this process involves several steps, including:

  1. Installing Python and PIP:
    • You first need to ensure Python and the Python Package Installer (PIP) are installed on the AWS machine.
  2. Installing Flask and Other Dependencies:
    • For a Flask web application, you’ll need to install Flask and any other libraries the application depends on. This might involve manually running pip install flask and other packages.
  3. Deploying the Code:
    • Once all dependencies are set up, you deploy your Python code to the server.
  4. Running the Python Code:
    • You finally execute the code using a command like python run, which starts the application on the server.

While this seems straightforward, several issues may arise during this process:

  • Issue 1: Python command not running:

    • This may happen if the system packages are outdated. Solution: Run sudo apt-get update to update the package manager.
  • Issue 2: Server not visible externally:

    • If the Flask server isn't accessible from outside the server, you can add --host=0.0.0.0 to make it accessible to external requests.

Application Compatibility Issues

What happens if you need to run two different applications on the same server? For example:

  • Application 1: Requires Java 11 to run.
  • Application 2: Requires Java 17 to run.

Running both applications on the same server can lead to version conflicts. For instance, if you set up the environment for Java 11, Application 2 will not function properly, and vice versa. Without isolation, it becomes difficult to run multiple applications with different dependencies on the same server.

Docker solves this problem by allowing you to create isolated environments for each application. Each application runs in its own container, which contains all the necessary dependencies, avoiding conflicts between different versions.

Key Challenges in Traditional Deployment

  1. Environment Setup:

    • Setting up an environment manually requires running several commands, installing the correct versions of dependencies, and configuring system settings. This process is prone to human error and time-consuming.
  2. Isolation:

    • Without isolation, running multiple applications on the same server can lead to conflicts. For example, different applications might require different versions of the same software (e.g., Java 11 vs. Java 17), which can’t coexist easily on the same server.
  3. "It Worked on My Laptop" Syndrome:

    • Often, developers encounter the issue where the application runs perfectly on their local machine but fails in production due to differences in environment configurations. This discrepancy can lead to unexpected bugs and system failures during deployment.

Virtual Machines (VMs) as a Solution

  1. What Are Virtual Machines?

    • A virtual machine (VM) is essentially a computer within a computer. You can create multiple VMs on a single physical machine, each with its own operating system and isolated environment.
    • Tools like VirtualBox allow users to create and manage VMs on a physical host.
  2. Advantages of VMs:

    • VMs provide isolation between applications, meaning that you can run multiple applications, each with its own dependencies, without worrying about conflicts.
  3. Drawbacks of VMs:

    • Resource-Intensive: VMs require substantial system resources because each VM runs its own full operating system, consuming large amounts of CPU, memory, and storage.
    • Slow Startup: Starting a VM can take several minutes because it needs to boot up an entire operating system.
    • Costly: Running multiple VMs is resource-heavy, which can be costly, especially in a cloud environment.

Docker: A More Efficient Solution

Docker Overview

Docker provides a solution to the limitations of virtual machines by introducing containers. Containers offer a lightweight, efficient way to isolate applications while sharing the host system’s resources. Unlike VMs, Docker containers don’t require a full operating system for each instance, making them faster and more efficient.

How Docker Works

Docker introduces the concept of a runtime environment, which allows developers to package an application along with its dependencies, libraries, and configuration files into a Docker image. This image can be used to create one or more Docker containers, which run the application in isolation from the rest of the system.

Docker vs. Virtual Machines

Feature Docker Virtual Machines
OS Dependency Containers share the host OS kernel Each VM runs a full, separate OS
Startup Speed Extremely fast, almost instant Slow, can take minutes to start
Resource Usage Lightweight, uses fewer resources Heavy, consumes significant CPU, RAM, and storage
Application Isolation High, but more efficient than VMs High, but with greater resource overhead

Key Docker Concepts

  1. Docker Image:

    • A Docker image is a lightweight, standalone, and executable software package that includes everything needed to run an application: the code, runtime, libraries, environment variables, and configuration files. Think of it as a blueprint for creating a running environment.
  2. Docker Container:

    • A Docker container is a running instance of a Docker image. Just like an object is an instance of a class in programming, a container is an instance of an image. Multiple containers can be created from the same image.
  3. Dockerfile:

    • A Dockerfile is a text file that contains a series of commands to set up a Docker image. Each command in the Dockerfile specifies a step in the process of building the environment. For example, it can define which base image to use (like OpenJDK for a Java app), copy the application code, and define how to run the application.

Example Dockerfile:

FROM openjdk:17
COPY target/product-service.jar /app.jar
CMD ["java", "-jar", "/app.jar"]


Setting Up and Using Docker

Practical Steps to Use Docker

  1. Creating a Dockerfile:

    • The Dockerfile defines how to create the environment. You specify the base image and the steps to set up your application. For example, in the above Dockerfile, we’re using openjdk:17 as the base image, copying our application’s JAR file, and specifying the command to run the app.
  2. Building a Docker Image:

    • Once you have a Dockerfile, you can build a Docker image from it. This image contains everything needed to run the application, pre-configured and ready to use.
    • Command to build the Docker image:
      docker build -t your-username/product-service:1.0 .
      
    • The -t flag allows you to tag the image with a name, making it easy to reference later.
  3. Running a Docker Container:

    • After building the image, you can create and run a container from that image. A container is the running instance of your Docker image.
    • Command to run the container:
      docker run -d -p 8080:8080 your-username/product-service:1.0
      
    • The -p flag maps the container’s port 8080 to the host machine’s port 8080, making it accessible. The -d flag runs the container in detached mode, meaning it will run in the background.
  4. Pushing the Docker Image to Docker Hub:

    • To share the Docker image with others, you can push it to a container registry like Docker Hub. Once uploaded, anyone can pull the image and run the same container on their machines.
    • Command to push the image:
      docker push your-username/product-service:1.0
      

Benefits of Using Docker

  1. Consistency Across Environments:
    • Docker ensures that the environment remains consistent from development to testing to production. Since containers encapsulate all dependencies, there’s no risk of environment-related bugs.
  2. Scalability:
    • Once a Docker image is created, it can be used to deploy the application on any server or cloud platform, making scaling effortless.
  3. Resource Efficiency:
    • Docker containers are lightweight and use fewer system resources than traditional VMs. This means more containers can run on the same hardware, reducing costs.
  4. Fast Deployment:
    • Docker images can be built and deployed quickly. Containers start almost instantly, making it much easier to deploy and scale applications.

By using Docker, development teams can streamline their workflow, ensuring that applications run consistently in any environment while using fewer resources. Docker simplifies both the development and deployment processes, helping to eliminate common issues such as environment mismatch and resource conflicts.