Docker Fundamentals
A beginner-friendly guide to Docker — containers, images, Dockerfiles, and Docker Compose for modern development workflows.
Docker Fundamentals
Docker enables you to package applications and their dependencies into portable containers that run consistently across any environment.
Core Concepts
- Image: Blueprint/template for a container
- Container: Running instance of an image
- Dockerfile: Instructions to build an image
- Registry: Storage for images (Docker Hub)
Basic Commands
# Pull an image
docker pull node:18-alpine
# Run a container
docker run -d -p 3000:3000 --name myapp node:18-alpine
# List running containers
docker ps
# View logs
docker logs myapp
# Stop and remove
docker stop myapp
docker rm myapp
Dockerfile
# Use Node.js base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy source code
COPY . .
# Expose port
EXPOSE 3000
# Start the application
CMD ["node", "server.js"]
Docker Compose
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://db:5432/mydb
depends_on:
- db
db:
image: postgres:15-alpine
volumes:
- pgdata:/var/lib/postgresql/data
environment:
POSTGRES_DB: mydb
POSTGRES_PASSWORD: secret
volumes:
pgdata:
Best Practices
- Use multi-stage builds to reduce image size
- Use
.dockerignoreto exclude unnecessary files - Run as non-root user for security
- Use specific tags (not
latest) for reproducibility - Layer caching: Order instructions from least to most frequently changing
Related Articles
🚀 devops
CI/CD with GitHub Actions
Set up continuous integration and deployment pipelines using GitHub Actions for automated testing and deployments.
🐍 python
Variables and Data Types
Learn about Python variables, data types, type casting, and dynamic typing. A comprehensive guide for beginners.
⚡ cpp
Introduction to C++
Getting started with C++ programming — compiler setup, basic syntax, variables, and your first program.