Thursday, 30 January 2025

2 - Docker Building and Orchestration

 

Day 2: Building and Orchestrating with Docker - Lecture Notes

Yesterday, we laid the groundwork for Docker. Today, we'll learn how to build our own images and orchestrate multiple containers.

Session 5: Building Docker Images (1 hour)

Dockerfiles:

A Dockerfile is a text file containing instructions for building a Docker image. It's like a recipe for creating your container.

Key Dockerfile Instructions:

  • FROM: Specifies the base image (where to start). Example: FROM ubuntu:latest
  • RUN: Executes commands inside the image during the build process. Example: RUN apt-get update && apt-get install -y nginx
  • COPY: Copies files from your host machine into the image. Example: COPY my-app /app
  • ADD: Similar to COPY, but can also handle URLs and tar files.
  • CMD: The default command to run when a container is started from the image. Example: CMD ["nginx", "-g", "daemon off;"]
  • WORKDIR: Sets the working directory inside the image.
  • EXPOSE: Declares which ports the container will listen on. Example: EXPOSE 80

Best Practices:

  • Use a minimal base image.
  • Combine RUN commands to reduce image layers.
  • Use .dockerignore to exclude unnecessary files from the image.

Hands-on:

  1. Create a Dockerfile: Create a new directory, my-web-app, and inside it, create a file named Dockerfile (no extension).
  2. Simple Web App: Create an index.html file in the same directory with some basic HTML content (e.g., "Hello from my Dockerized web app!").
  3. Dockerfile Contents: Add the following to your Dockerfile:
Dockerfile
FROM nginx:latest
COPY index.html /usr/share/nginx/html
EXPOSE 80
  1. Build the Image: In the terminal, navigate to the my-web-app directory and run:
Bash
docker build -t my-web-app-image .
  • -t my-web-app-image: Tags the image with the name my-web-app-image.
  • .: Specifies the build context (the current directory).
  1. Run the Container:
Bash
docker run -p 8080:80 my-web-app-image
  • -p 8080:80: Maps port 8080 on your host machine to port 80 in the container (where Nginx is listening).
  1. Test: Open your web browser and go to http://localhost:8080. You should see your "Hello from my Dockerized web app!" message.

Session 6: Docker Compose (1 hour)

Introduction to Docker Compose:

Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file (docker-compose.yml) to1 configure the services, networks, and volumes for your application.2

docker-compose.yml:

This file defines the services that make up your application.

Example: A web application with a Redis database.

YAML
version: "3.9"  # Specify the Compose file version

services:
  web:
    build: .  # Build the image from the current directory (where the Dockerfile is)
    ports:
      - "80:80"
    depends_on:
      - redis

  redis:
    image: "redis:latest"

Hands-on:

  1. Create docker-compose.yml: Create a docker-compose.yml file in the same directory as your Dockerfile. Add the example above.
  2. Run with Compose:
Bash
docker-compose up -d  # -d runs the containers in detached mode (background)
  1. Access the Web App: Go to http://localhost in your browser.
  2. Stop with Compose:
Bash
docker-compose down

Session 7: Docker Networking (1 hour)

Container Networking:

Containers can communicate with each other and with the outside world through Docker networks.

Network Types:

  • Bridge: The default network. Containers connected to a bridge network can communicate with each other and the outside world through port mapping.
  • Host: The container shares the host's network namespace. This gives the container direct access to the host's network interfaces. Use with caution!
  • None: The container has no network access.

User-defined Bridge Networks:

You can create custom bridge networks to isolate your containers.

Hands-on (Continuing from the Docker Compose example):

  1. Explicitly Define a Network: Add a networks section to your docker-compose.yml:
YAML
version: "3.9"

services:
  web:
    build: .
    ports:
      - "80:80"
    depends_on:
      - redis
    networks:
      - my-network

  redis:
    image: "redis:latest"
    networks:
      - my-network

networks:
  my-network:  # Define the custom bridge network
  1. Run with Compose: docker-compose up -d
  2. Inspect the Network: docker network ls (lists Docker networks). docker network inspect my-network (shows details about the network).

Session 8: Docker Volumes (1 hour)

Data Persistence:

By default, data inside a container is lost when the container is stopped or removed. Volumes provide a way to persist data outside of the container.

Volume Types:

  • Bind Mounts: Mount a directory from the host machine into the container.
  • Named Volumes: Volumes managed by Docker. Easier to back up and restore.

Hands-on (Continuing from the Docker Compose example):

  1. Add a Named Volume: Modify your docker-compose.yml to use a named volume for Redis data:
YAML
version: "3.9"

services:
  web:
    build: .
    ports:
      - "80:80"
    depends_on:
      - redis
    networks:
      - my-network

  redis:
    image: "redis:latest"
    networks:
      - my-network
    volumes:
      - redis-data:/data  # Mount a named volume at /data inside the container

volumes:
  redis-data:  # Define the named volume
  1. Run with Compose: docker-compose up -d
  2. Check the Volume: docker volume ls (lists Docker volumes).

Now, even if you stop and remove the Redis container, the data will be preserved in the redis-data volume.

This concludes Day 2. You've learned how to build images, orchestrate containers with Compose, manage networking, and persist data with volumes. These are essential skills for working with Docker. Practice these concepts and you'll be well on your way to becoming a Docker pro!

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...