Skip to main content

Docker Containers: A Practical Getting Started Guide

· 2 min read
Goel Academy
DevOps & Cloud Learning Hub

Docker has revolutionized how we build, ship, and run applications. This practical guide will get you started with Docker containers and help you understand why they're essential for modern development.

What is Docker?

Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, standalone packages that include everything needed to run an application.

Containers vs Virtual Machines

AspectContainersVirtual Machines
SizeMegabytesGigabytes
StartupSecondsMinutes
IsolationProcess-levelFull OS
PerformanceNear-nativeOverhead

Key Docker Concepts

  • Image: A read-only template with instructions for creating a container
  • Container: A runnable instance of an image
  • Dockerfile: A text file with instructions to build an image
  • Registry: A repository for Docker images (like Docker Hub)

Getting Started

Installation

Download and install Docker Desktop from docker.com.

# Verify installation
docker --version
docker run hello-world

Your First Dockerfile

Create a Dockerfile for a Node.js application:

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

Basic Commands

# Build an image
docker build -t myapp:1.0 .

# Run a container
docker run -p 3000:3000 myapp:1.0

# List running containers
docker ps

# Stop a container
docker stop <container_id>

# Remove a container
docker rm <container_id>

Docker Compose

For multi-container applications, use Docker Compose:

version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
depends_on:
- db
db:
image: postgres:14
environment:
POSTGRES_PASSWORD: secret

Run with:

docker-compose up -d

Best Practices

  1. Use official base images: They're maintained and secure
  2. Keep images small: Use Alpine-based images when possible
  3. Use .dockerignore: Exclude unnecessary files
  4. Don't run as root: Use a non-root user in containers
  5. Layer caching: Order Dockerfile commands for optimal caching

Conclusion

Docker containers provide a consistent environment from development to production. Master these basics, and you'll be well-equipped to containerize any application.