Thursday, 30 January 2025

1 - Docker Intro

 

Day 1: Foundations of Docker - Lecture Notes

Welcome to the world of Docker! Today, we're diving into the fundamentals of this powerful tool that's revolutionizing how we build, ship, and run software.

Session 1: Introduction to Docker (1 hour)

What is Docker?

Imagine you've built a fantastic web application. It works perfectly on your laptop. But when you try to run it on a different computer, it breaks! Why? Because the other machine might have different software versions, missing libraries, or a different operating system. This is the classic "works on my machine" problem.

Docker solves this by packaging your application and all its dependencies (libraries, system tools, code, runtime, etc.) into a single unit called a container. Think of it like a shipping container. It doesn't matter what's on the ship or at the destination port; the container keeps its contents safe and intact. Your application runs consistently, regardless of the underlying system.

Real-World Example: A food delivery app. The developers use specific versions of databases, programming languages, and libraries. Docker ensures the app runs the same way on their machines, the testing servers, and the actual production servers handling customer orders.

Docker vs. Virtual Machines:

You might have heard of Virtual Machines (VMs). They also help with consistency, but they're heavyweight. VMs emulate an entire computer, including the operating system. Docker, on the other hand, shares the host operating system's kernel, making it much more lightweight and efficient.

FeatureVirtual Machines (VMs)Docker Containers
Operating SystemGuest OS per VMShares Host OS
SizeGigabytesMegabytes
Boot TimeMinutesSeconds
Resource UsageHighLow

Docker Architecture:

Docker uses a client-server architecture:

  • Docker Client: The command-line tool you use to interact with Docker.
  • Docker Engine: The core service that runs containers. It includes the Docker daemon (dockerd) which manages containers, images, networks, and volumes.
  • Images: Read-only templates used to create containers.
  • Containers: Running instances of images.
  • Registry: A storage and distribution system for Docker images (e.g., Docker Hub).

Hands-on:

  1. Check Docker Version: Open your terminal and type docker version. This confirms Docker is installed. If not, follow the installation instructions for your OS (provided separately).
  2. Docker Info: Type docker info. This shows detailed information about your Docker installation.

Session 2: Docker Installation and First Container (1 hour)

(Installation instructions will be OS-specific and provided separately. Focus on the general principles here.)

Your First Container: hello-world

Let's run a simple container:

Bash
docker run hello-world

What happens?

  1. Docker checks if you have the hello-world image locally. If not, it pulls it from Docker Hub (a public registry).
  2. Docker creates a container from that image.
  3. Docker starts the container.
  4. The container executes a program that prints "Hello from Docker!"
  5. The container stops.

Anatomy of docker run:

  • docker run: The command to run a container.
  • hello-world: The name of the image.

Session 3: Working with Containers (1 hour)

Container Lifecycle:

Containers have a lifecycle:

  • create: A container is created but not running.
  • start: A created container is started.
  • stop: A running container is stopped.
  • restart: A stopped container is started again.
  • kill: A running container is forcefully stopped.
  • remove: A stopped container is deleted.

Hands-on:

  1. List Running Containers: docker ps (shows currently running containers). -a shows all containers (running and stopped).
  2. Stop a Container: docker stop <container_id_or_name> (replace with a container ID or name from docker ps).
  3. Remove a Container: docker rm <container_id_or_name>.
  4. Run an Interactive Container: docker run -it ubuntu bash
    • -i: Interactive (keeps STDIN open).
    • -t: TTY (allocates a pseudo-TTY connected to STDIN and STDOUT).
    • ubuntu: The image name.
    • bash: The command to run inside the container (opens a bash shell).

Exploring the Container:

Once inside the interactive container, you can use regular Linux commands (like ls, cd, pwd) to explore its file system. Notice it's a minimal environment. Type exit to leave the container.

Session 4: Docker Images (1 hour)

What are Docker Images?

Images are the blueprints for containers. They are made up of layers. Each layer represents a change to the file system. Images are read-only. When you run a container, a writable layer is added on top of the image.

Working with Images:

Hands-on:

  1. List Images: docker images (shows all downloaded images).
  2. Pull an Image: docker pull ubuntu:latest (downloads the ubuntu image with the latest tag. Tags are like versions). If you omit the tag, latest is assumed.
  3. Remove an Image: docker rmi <image_id_or_name>.

Image Size:

Be mindful of image size. Smaller images are faster to download and deploy. We'll discuss best practices for minimizing image size later.

This concludes Day 1. Make sure to practice the hands-on exercises. Understanding these fundamentals is crucial for the rest of the Docker journey!

References:

https://www.docker.com/101-tutorial/

https://people.irisa.fr/Anthony.Baire/docker-tutorial.pdf

https://www.freecodecamp.org/news/the-docker-handbook/

https://github.com/sidpalas/devops-directive-docker-course/blob/main/README.md

No comments:

Post a Comment

Dockerize Agent -Document Processing

 Since you are using Flask in Agent.py , I will modify the Dockerfile and steps accordingly. [WIP] 1. Update the Dockerfile Create a Do...