Docker Complete Guide for Beginners
Advertisement
Docker Complete Guide for Beginners
Docker revolutionized how we develop, ship, and run applications. This comprehensive guide will take you from zero to proficient with Docker in practical, hands-on steps.
Introduction
Docker enables developers to package applications with all dependencies into lightweight, portable containers. This eliminates the "it works on my machine" problem and simplifies deployment across environments.
- Docker Complete Guide for Beginners
- What is Docker?
- Installation
- Ubuntu/Debian
- macOS
- Docker Images vs Containers
- Writing Your First Dockerfile
- Building and Running Images
- Docker Registries and Docker Hub
- Container Management
- Port Mapping
- Environment Variables
- Volumes (Data Persistence)
- Common Docker Commands
- Debugging Containers
- Docker Compose Basics
- Best Practices for Beginners
- FAQ
What is Docker?
Docker is a containerization platform that packages your application code, runtime, system tools, and libraries into a standardized unit called a container. Containers are:
- Lightweight: Share the host kernel, using fewer resources than VMs
- Portable: Run consistently across development, testing, and production
- Fast: Start in milliseconds
- Isolated: Each container has its own filesystem and processes
Installation
Ubuntu/Debian
sudo apt-get update
sudo apt-get install docker.io
sudo usermod -aG docker $USER
newgrp docker
docker --version
macOS
Download Docker Desktop from docker.com. After installation:
docker --version
docker run hello-world
Docker Images vs Containers
An image is a blueprint—a read-only template. A container is a running instance of an image.
# Pull an image from Docker Hub
docker pull ubuntu:22.04
# Run a container from the image
docker run -it ubuntu:22.04 /bin/bash
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# List images
docker images
Writing Your First Dockerfile
A Dockerfile is a text file with instructions to build a Docker image.
# Use a base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy application code
COPY . .
# Expose port
EXPOSE 3000
# Run the application
CMD ["node", "server.js"]
Building and Running Images
# Build an image
docker build -t myapp:1.0 .
# Run a container
docker run -p 3000:3000 myapp:1.0
# Run in background (detached mode)
docker run -d -p 3000:3000 --name myapp myapp:1.0
# View logs
docker logs myapp
# Stop container
docker stop myapp
# Start stopped container
docker start myapp
# Remove container
docker rm myapp
Docker Registries and Docker Hub
Docker Hub is the default registry for sharing images.
# Login to Docker Hub
docker login
# Tag your image for Docker Hub
docker tag myapp:1.0 username/myapp:1.0
# Push to Docker Hub
docker push username/myapp:1.0
# Pull from Docker Hub
docker pull username/myapp:1.0
Container Management
Port Mapping
# Map single port
docker run -p 8080:3000 myapp
# Map multiple ports
docker run -p 8080:3000 -p 9000:5000 myapp
# Map all exposed ports
docker run -P myapp
Environment Variables
# Pass environment variables
docker run -e DATABASE_URL=postgres://db myapp
# From env file
docker run --env-file .env myapp
Volumes (Data Persistence)
# Mount host directory
docker run -v /host/path:/container/path myapp
# Named volume
docker run -v myvolume:/data myapp
# List volumes
docker volume ls
# Remove volume
docker volume rm myvolume
Common Docker Commands
# Inspect container details
docker inspect container_id
# Execute command in running container
docker exec -it container_id /bin/bash
# Copy files to/from container
docker cp container_id:/app/file.txt ./
# View resource usage
docker stats
# Remove unused images
docker image prune
# Remove stopped containers
docker container prune
Debugging Containers
# View logs
docker logs container_id
# Follow logs (tail -f)
docker logs -f container_id
# Get last 100 lines
docker logs --tail 100 container_id
# Show timestamps
docker logs -t container_id
# Enter running container
docker exec -it container_id /bin/bash
# Start container with shell
docker run -it myapp /bin/bash
Docker Compose Basics
For multi-container applications, use Docker Compose:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgres://db/myapp
db:
image: postgres:15
environment:
- POSTGRES_PASSWORD=secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
# Start services
docker-compose up
# Start in background
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs
Best Practices for Beginners
- Use specific base image versions (not
latest) - Minimize layers by combining RUN commands
- Use .dockerignore to exclude unnecessary files
- Run containers as non-root when possible
- Keep images small by using alpine variants
- Separate concerns with multiple containers
- Use environment variables for configuration
FAQ
Q: What's the difference between ENTRYPOINT and CMD? A: ENTRYPOINT is the command that runs; CMD provides default arguments. Use ENTRYPOINT for the main process and CMD for default parameters.
Q: How do I remove all unused images and containers? A: Use docker system prune -a to remove all unused resources, or individual prune commands for specific types.
Q: Can containers access the host filesystem? A: Yes, using volume mounts with -v flag, but be cautious with permission and security implications.
Advertisement