DevOps Sessions - Week 12 - Containers

devops containers docker microservices 17-10-2024 ​​

DevOps Sessions - Week 12 - Containers

Welcome to Week 12 of our “Becoming a DevOps Engineer” series! This week, we will focus on containers, a transformative technology that enables developers to package applications and their dependencies into a standardized unit for software development. Containers offer consistency across multiple environments and streamline the deployment process. We will explore the basics of containers, key concepts, and popular tools like Docker and Kubernetes. Let’s dive in!

Session Overview

1. Introduction to Containers

2. Docker

3. Kubernetes

4. Container Orchestration

5. Practical Examples

6. Best Practices and Tools

1. Introduction to Containers

What are Containers?

Containers are lightweight, portable, and self-sufficient units that include an application and all its dependencies. They run consistently across various environments, from a developer’s local machine to production.

Benefits of Containers in DevOps

2. Docker

Overview of Docker

Docker is an open-source platform that automates the deployment, scaling, and management of applications in containers. It simplifies the creation and management of containers.

Setting Up Docker

  1. Install Docker:

  2. Verify Installation:

    docker --version

Creating and Managing Containers with Docker

  1. Create a Dockerfile:

    # Use an official Python runtime as a parent image
    FROM python:3.8-slim
    
    # Set the working directory in the container
    WORKDIR /app
    
    # Copy the current directory contents into the container at /app
    COPY . /app
    
    # Install any needed packages specified in requirements.txt
    RUN pip install --no-cache-dir -r requirements.txt
    
    # Make port 80 available to the world outside this container
    EXPOSE 80
    
    # Define environment variable
    ENV NAME World
    
    # Run app.py when the container launches
    CMD ["python", "app.py"]
  2. Build the Docker Image:

    docker build -t my-python-app .
  3. Run the Docker Container:

    docker run -p 4000:80 my-python-app
  4. Manage Containers:

    docker ps          # List running containers
    docker stop <ID>   # Stop a running container
    docker rm <ID>     # Remove a container

3. Kubernetes

Overview of Kubernetes

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It groups containers into logical units for easy management and discovery.

Setting Up Kubernetes

  1. Install Minikube: Minikube is a tool that sets up a local Kubernetes cluster on your machine.

  2. Start Minikube:

    minikube start

Managing Containers with Kubernetes

  1. Create a Deployment:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-python-app
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: my-python-app
      template:
        metadata:
          labels:
            app: my-python-app
        spec:
          containers:
          - name: my-python-app
            image: my-python-app
            ports:
            - containerPort: 80
  2. Apply the Deployment:

    kubectl apply -f deployment.yaml
  3. Expose the Deployment:

    kubectl expose deployment my-python-app --type=LoadBalancer --port=4000 --target-port=80
  4. Manage Kubernetes Resources:

    kubectl get pods          # List running pods
    kubectl delete pod <NAME> # Delete a pod
    kubectl get services      # List services

4. Container Orchestration

What is Container Orchestration?

Container orchestration automates the deployment, management, scaling, and networking of containers. It ensures applications run efficiently in a distributed environment.

Importance in DevOps

5. Practical Examples

Building a Docker Image

  1. Create a Dockerfile:

    FROM node:14
    WORKDIR /app
    COPY package*.json ./
    RUN npm install
    COPY . .
    EXPOSE 8080
    CMD ["node", "server.js"]
  2. Build the Image:

    docker build -t my-node-app .
  3. Run the Container:

    docker run -p 8080:8080 my-node-app

Deploying a Containerized Application with Kubernetes

  1. Create a Deployment:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: my-node-app
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: my-node-app
      template:
        metadata:
          labels:
            app: my-node-app
        spec:
          containers:
          - name: my-node-app
            image: my-node-app
            ports:
            - containerPort: 8080
  2. Apply the Deployment:

    kubectl apply -f deployment.yaml
  3. Expose the Deployment:

    kubectl expose deployment my-node-app --type=LoadBalancer --port=8080 --target-port=8080

6. Best Practices and Tools

Best Practices for Using Containers


By mastering containers with tools like Docker and Kubernetes, you can ensure your applications are portable, scalable, and consistent across environments. Stay tuned for next week’s session, where we will explore container orchestration. Happy containerizing!

Author's photo

Nihit Jain

Architecting DevOps 🏗️ with Data, AI, Security, & IoT on Cloud ☁️




See other articles:

Sessions