Skip to content

Romanian-Space-Initiative/Containerization-Workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Sat Data Masterclass - Containerization Workshop

Welcome to the Satellite Data Masterclass workshop on containerization! This comprehensive guide will walk you through everything you need to know about Docker, Docker Compose, and Kubernetes to build, deploy, and manage containerized applications efficiently.


Table of Contents

  1. Introduction to Containerization
  2. Docker Basics
  3. Docker Advanced Usage
  4. Docker Compose
  5. Kubernetes Deployment
  6. Additional Resources

Introduction to Containerization

Containerization is a lightweight form of virtualization that packages an application and all its dependencies into a single container image. Containers run consistently across different environments, making development, testing, and deployment more reliable and efficient. Unlike traditional virtual machines, containers share the host OS kernel, allowing them to be more resource-efficient and faster to start.

Key technologies covered in this workshop:

  • Docker: The leading container platform to build, ship, and run containers.
  • Docker Compose: A tool for defining and running multi-container Docker applications.
  • Kubernetes: An orchestration system for automating deployment, scaling, and management of containerized applications.

Docker Basics

Docker allows you to package your application and its dependencies into a container image that can run anywhere Docker is installed.

Installing Docker

Verify installation by running:

docker --version

Building a Docker Image

Create a Dockerfile in your project directory:

FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .

RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Build the image with:

docker build -t hello-docker .

Running a Docker Container

Run the container, mapping port 80 inside the container to port 8080 on your host:

docker run -p 8080:8080 hello-docker
  • -d: Run container in detached mode
  • -p: Map ports
  • --name: Assign a name to the container

Common Docker Commands

  • List running containers:

    docker ps
  • List all containers (including stopped):

    docker ps -a
  • Stop a running container:

    docker stop hello-docker
  • Remove a container:

    docker rm hello-docker
  • List images:

    docker images
  • Remove an image:

    docker rmi hello-docker

Inspecting Containers and Logs

  • View container logs:

    docker logs hello-docker
  • Access a running container’s shell:

    docker exec -it hello-docker /bin/bash

Docker Advanced Usage

Tagging and Pushing Images to Docker Hub

  1. Tag your image:

    docker tag hello-docker your-dockerhub-username/web-advanced:latest
  2. Log in to Docker Hub:

    docker login
  3. Push the image:

    docker push your-dockerhub-username/web-advanced:latest

Running Containers with Environment Variables and Volume Mounts

docker run -d -p 8080:80 --name web-advanced \
  -e FLASK_ENV=development \
  -v $(pwd):/app \
  web-advanced
  • -e: Set environment variables
  • -v: Mount host directory into container

Testing Your Container

Run tests inside the container:

docker exec -it web-advanced pytest tests/

Cleaning Up Docker Resources

Remove all stopped containers:

docker container prune

Remove all unused images:

docker image prune -a

Remove all unused volumes:

docker volume prune

Remove all unused networks:

docker network prune

Remove everything (containers, images, volumes, networks):

docker system prune -a --volumes

Docker Compose

Docker Compose simplifies running multi-container applications by using a single YAML file.

Example docker-compose.yml

version: '3.8'

services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app
    environment:
      - FLASK_ENV=development
  redis:
    image: "redis:alpine"

Using Docker Compose

  • Start services:

    docker-compose up -d
  • View running services:

    docker-compose ps
  • Stop services:

    docker-compose down
  • View logs:

    docker-compose logs -f
  • Rebuild images after changes:

    docker-compose build
  • Run one-off commands inside a service container:

    docker-compose run web pytest tests/

Kubernetes Deployment

Kubernetes automates container deployment, scaling, and management.

Prerequisites

Verify kubectl installation:

kubectl version --client

Creating a Deployment

Create a file deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-advanced
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-advanced
  template:
    metadata:
      labels:
        app: web-advanced
    spec:
      containers:
      - name: web-advanced
        image: your-dockerhub-username/web-advanced:latest
        ports:
        - containerPort: 80

Apply the deployment:

kubectl apply -f deployment.yaml

Exposing the Deployment

Expose your deployment as a LoadBalancer service:

kubectl expose deployment web-advanced --type=LoadBalancer --port=80

Scaling the Deployment

Scale to 5 replicas:

kubectl scale deployment web-advanced --replicas=5

Checking Status

  • List pods:

    kubectl get pods
  • List services:

    kubectl get svc
  • Describe a pod:

    kubectl describe pod <pod-name>
  • View logs of a pod:

    kubectl logs <pod-name>

Updating the Deployment

  1. Build and push a new image version with a new tag:

    docker build -t your-dockerhub-username/web-advanced:v2 .
    docker push your-dockerhub-username/web-advanced:v2
  2. Update the deployment to use the new image:

    kubectl set image deployment/web-advanced web-advanced=your-dockerhub-username/web-advanced:v2
  3. Verify rollout status:

    kubectl rollout status deployment/web-advanced

Rolling Back

Rollback to previous revision:

kubectl rollout undo deployment/web-advanced

Cleaning Up

Delete the service and deployment:

kubectl delete service web-advanced
kubectl delete deployment web-advanced

Additional Resources


Thank you for participating in the Sat Data Masterclass containerization workshop! We hope this guide helps you build, manage, and scale your containerized applications with confidence.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published