Introduction-to-Kubernetes-and-Cloud-Native-Technologies

Module 3

Containers and Docker Basics

In Module 2, we explored Kubernetes’ core concepts—pods, nodes, and clusters—and used kubectl to interact with a cluster. Now, we’ll zoom in on containers, the building blocks that Kubernetes manages. We’ll also introduce Docker, the tool used to create and run containers. By the end of this module, you’ll understand what containers are, how they work, and how to run a simple container yourself.

Learning Objectives:

What Are Containers?

Imagine you’re moving to a new city, and you need to pack your belongings. Instead of dragging your entire house (furniture, appliances, and all), you pack just what you need into a lightweight, portable box. In the world of software, a container is like that box—it holds everything an application needs to run, such as:

Unlike a full virtual machine (VM), which is like moving the entire house (including the operating system), containers are lightweight because they share the host’s operating system. This makes them fast, portable, and efficient.

Why Containers?

Analogy: Think of a container as a lunchbox. It holds your sandwich (the app), condiments (libraries), and a napkin (configuration). You can carry it anywhere, and it’s ready to use without needing a full kitchen.

What is Docker?

Docker is the most popular tool for creating, running, and managing containers. It’s like the chef who prepares and serves those lunchboxes. Docker provides:

Kubernetes uses Docker (or other runtimes like containerd) to run containers inside pods.

How Containers Work with Kubernetes

In Kubernetes, containers don’t run alone—they’re housed in pods (from Module 2). Here’s how it fits together:

For example, if you want to run a website, you’d:

  1. Package the website’s code and server (e.g., Nginx) into a container using Docker.
  2. Tell Kubernetes to create a pod with that container.
  3. Kubernetes schedules the pod to a node and ensures it keeps running.

Hands-On Exercise: Running Your First Container

Let’s try running a container! You have two options: use an online sandbox (no setup required) or install Docker on your computer. For beginners, the sandbox is easier, so we’ll start there.

Option 1: Use Play with Docker (Online Sandbox)

  1. Go to Play with Docker (https://labs.play-with-docker.com/).
  2. Click “Start” and log in (free with a Docker Hub account, or create one).
  3. Click “Add New Instance” to get a terminal.
  4. Run this command to start an Nginx web server container:
docker run -d -p 80:80 nginx
  1. Click the “80” link that appears in the Play with Docker interface to see the Nginx welcome page.
  2. Check running containers:
    docker ps
    
    • What to expect: You’ll see your Nginx container listed with its ID and status.
  3. Stop the container:
    docker stop <container-id>
    

Option 2: Install Docker Locally (Optional)

If you prefer to try this on your computer:

  1. Install Docker Desktop (https://www.docker.com/products/docker-desktop/) for Windows or macOS, or Docker for Linux (follow instructions for your OS).
  2. Open a terminal and run the same docker run -d -p 80:80 nginx command.
  3. Visit http://localhost in your browser to see the Nginx page.
  4. Use docker ps and docker stop as above.

Note for Beginners: If you’re not ready to run commands, just follow along. The sandbox is free and requires no setup, so give it a try if you can! We’ll use .these skills in Kubernetes later.

Understanding the Docker Workflow

Here’s what happened in the exercise:

In the next module, we’ll use Docker to create containers and deploy them to Kubernetes with kubectl.

Quiz

  1. What is a container?
  1. What does Docker do?
  1. What command runs a container in Docker?

Answers: 1-B, 2-B, 3-B

Further Reading

Docker Get Started: https://docs.docker.com/get-started/

Docker Hub: https://hub.docker.com/ (explore public images like nginx)

Kubernetes Documentation: Containers (https://kubernetes.io/docs/concepts/containers/)

What’s Next?

In Module 4, we’ll combine what you’ve learned about containers and Kubernetes by deploying applications with Kubernetes. You’ll use kubectl to deploy a simple app (like the Nginx web server) to a Kubernetes cluster and see it in action.

Proceed to Module 4