Thursday, February 6, 2025

twtech Docker Containerization


Double-click on the image to zoom-out ...Larger.

To return to Home page: Refresh Page or Take ESC Button on Keyboard.

Docker Containerization

Docker is an open-source platform for developing, shipping, and running applications in containers. It allows developers to package applications with their dependencies, ensuring consistency across different environments.

 Key Concepts of Docker

1.1. What is a Container?

  • A lightweight, isolated environment that includes the application, libraries, and dependencies.
  • Runs consistently across different systems (laptops, servers, clouds).

1.2. Docker vs. Virtual Machines (VMs)

FeatureDocker ContainersVirtual Machines (VMs)
SpeedFast startup (~seconds)Slow startup (~minutes)
SizeLightweight (MBs)Heavy (GBs)
IsolationProcess-level isolationFull OS-level isolation
Resource UsageShares host OS kernelRequires full OS per VM
PortabilityRuns anywhereLimited to host OS compatibility

2. Benefits of Docker

Portability – Works on any system with Docker installed.
Consistency – Eliminates "it works on my machine" issues.
Efficiency – Uses fewer resources than VMs.
Scalability – Easily deployable in Kubernetes.
Faster CI/CD – Speeds up development and testing.

3. Docker Architecture

  • Docker Client: The CLI tool used to interact with Docker.
  • Docker Daemon (Engine): Runs containers and manages system resources.
  • Docker Images: Pre-packaged application blueprints.
  • Docker Containers: Running instances of Docker images.
  • Docker Hub: Public/private registry for sharing images.

4. Essential Docker Commands

4.1. Install & Verify Docker

docker --version # Check Docker version docker info # Get system info

4.2. Working with Images

docker pull nginx # Download an image from Docker Hub docker images # List downloaded images docker rmi <image_id> # Remove an image

4.3. Running Containers

docker run hello-world # Run a test container docker run -d -p 8080:80 twtech-springapp # Run Nginx in detached mode, exposing port 80 on 8080 docker ps # List running containers docker ps -a # List all containers (including stopped ones) docker stop <container_id> # Stop a running container docker rm <container_id> # Remove a stopped container

4.4. Building Custom Docker Images

docker build -t twtech-webapp . # Build an image from a Dockerfile docker tag twtech-webapp:v1 devopspatemf2021/twtech-webapp:v1 # Tag an image for a repository docker push devopspatemf2021/twtech-webapp:v1 # Push image to Docker Hub

4.5. Container Management

docker logs <container_id> # View logs of a container docker exec -it <container_id> bash # Access container shell docker inspect <container_id> # Get detailed container info

5. Dockerfile (Creating Custom Images)

A Dockerfile is a script with instructions to create a Docker image.

Example:

# Dockerfile

# Use base image FROM node:18 # Set working directory WORKDIR /app # Copy files and install dependencies COPY package.json . RUN npm install # Copy the rest of the app COPY . . # Expose port and define the startup command EXPOSE 80 CMD ["node", "server.js"]

To build and run:

docker build -t twtech-springapp . docker run -d -p 3000:80 twtech-springapp

6. Docker Networking

docker network create mynetwork # Create a custom network docker network ls # List networks docker network inspect mynetwork # Inspect a network

7. Docker Volumes (Data Persistence)

docker volume create mydata # Create a volume docker run -v twtech-data:/app/data twtech-springapp # Attach volume to a container docker volume ls # List volumes

8. Kubernetes Integration

  • Docker containers can be deployed in Kubernetes for orchestration and scaling.
  • Kubernetes pods use Docker images to run applications.

9. Best Practices for Docker

Use lightweight base images (Alpine, Distroless).
Keep Docker images small by minimizing unnecessary dependencies.
Use .dockerignore to exclude files from image builds.
Tag images properly (e.g., myapp:v1, latest).
Scan images for security vulnerabilities (docker scan).

No comments:

Post a Comment

AWS DynamoDB | Integration With S3 Bucket.

  AWS DynamoDB ↔ S3 integration , View: What DynamoDB ↔ S3 integration is,   How to use DynamoDB ↔ S3 integration,   Why uses DynamoDB ↔  S3...