Docker Basics — Containerization for DevOps

Table of Contents

  1. What is Docker?
  2. Understanding Containers vs Virtual Machines
  3. Why Docker for DevOps?
  4. Installing Docker
  5. Key Docker Concepts
  6. Building Docker Images
  7. Dockerizing a Simple Application (Step-by-Step)
  8. Managing Containers
  9. Best Practices for Docker Usage in DevOps
  10. Conclusion

What is Docker?

Docker is an open-source platform designed to help developers and operations teams build, package, and deploy applications using containers. A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including:

  • Code
  • Runtime
  • Libraries
  • Environment variables
  • Configuration files

Docker simplifies application delivery by making it easy to ensure consistency across development, testing, and production environments.


Understanding Containers vs Virtual Machines

Containers:

  • Share the host OS kernel
  • Lightweight and fast
  • Start in milliseconds
  • Portable and isolated

Virtual Machines (VMs):

  • Have their own OS
  • Heavier in size
  • Slower to boot
  • More resource-intensive
FeatureContainersVirtual Machines
Boot TimeMillisecondsMinutes
SizeMBsGBs
PerformanceNear-nativeOverhead of guest OS
PortabilityHighMedium

Why Docker for DevOps?

Docker is an essential tool in the DevOps toolchain because:

  • Consistency: Ensures the app runs the same everywhere.
  • Isolation: Avoids dependency conflicts.
  • Scalability: Easy to replicate containers across environments.
  • Speed: Faster setup for testing and deployment.
  • CI/CD Ready: Seamlessly integrates with pipelines.

Installing Docker

On Linux (Ubuntu):

bashCopyEditsudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker

On macOS:

On Windows:

  • Docker Desktop (with WSL2 backend recommended)

Verify Installation:

bashCopyEditdocker --version
docker run hello-world

Key Docker Concepts

  • Docker Image: A read-only template with instructions for creating a container.
  • Docker Container: A running instance of an image.
  • Dockerfile: A script with instructions to build an image.
  • Docker Hub: A public registry to store and share images.

Building Docker Images

You create a Docker image using a Dockerfile.

Example Dockerfile:

DockerfileCopyEdit# Use an official Node.js runtime as a base
FROM node:18-alpine

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy source code
COPY . .

# Expose port and define command
EXPOSE 3000
CMD ["node", "index.js"]

Build Image:

bashCopyEditdocker build -t my-node-app .

Dockerizing a Simple Application (Step-by-Step)

Let’s containerize a basic Node.js app.

Step 1: Project Structure

pgsqlCopyEdit/my-app
  ├── index.js
  ├── package.json
  └── Dockerfile

index.js:

jsCopyEditconst http = require('http');
const port = 3000;

http.createServer((req, res) => {
  res.end('Hello from Dockerized Node.js App!');
}).listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

package.json:

jsonCopyEdit{
  "name": "docker-node-app",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  }
}

Dockerfile:

(Refer to the previous Dockerfile example)

Step 2: Build and Run

bashCopyEditdocker build -t docker-node-app .
docker run -p 3000:3000 docker-node-app

Step 3: Test the App

Visit http://localhost:3000 in your browser.


Managing Containers

List all containers:

bashCopyEditdocker ps -a

Stop a container:

bashCopyEditdocker stop <container_id>

Remove a container:

bashCopyEditdocker rm <container_id>

Remove an image:

bashCopyEditdocker rmi <image_id>

Best Practices for Docker Usage in DevOps

  1. Use Small Base Images: Alpine is a good minimal choice.
  2. Leverage .dockerignore: Exclude unnecessary files from image context.
  3. Multi-stage Builds: Keep final images lightweight.
  4. Tag Meaningfully: Use semantic versioning.
  5. Scan Images for Vulnerabilities: Tools like Trivy help here.
  6. Automate Image Builds: With CI tools like Jenkins, GitHub Actions.
  7. Use Private Registries for Sensitive Apps.

Conclusion

Docker is a cornerstone of modern DevOps workflows. With Docker, you can:

  • Package and ship applications faster.
  • Ensure environment parity.
  • Simplify deployment and testing.
  • Integrate seamlessly with CI/CD and orchestration tools like Kubernetes.

In the next module, we’ll take your Docker skills further by exploring Docker Compose and multi-container environments, enabling you to manage full app stacks efficiently.