Basics

Docker Basics

Docker is a containerization platform that allows developers to package applications and their dependencies into lightweight, portable containers. Containers ensure consistent behavior across different environments, from development to production.

What is Docker?

Docker enables you to run applications in isolated environments called containers. Unlike virtual machines, containers share the host OS kernel, making them more efficient and faster to start.

Key Benefits

  • Consistency: Applications run the same way everywhere
  • Isolation: Each container runs independently
  • Portability: Containers work across different platforms
  • Efficiency: Lower resource overhead than VMs

Core Concepts

Images vs Containers

ComponentDescriptionExample
ImageRead-only template containing application codenginx:latest
ContainerRunning instance of an imageActive web server
DockerfileText file with instructions to build imagesRecipe for creating images

Docker Architecture

graph TB A[Docker Client] --> B[Docker Daemon]; B --> C[Images]; B --> D[Containers]; B --> E[Networks]; B --> F[Volumes]; C --> G[Docker Registry/Hub];

Essential Commands

Image Management

  • docker pull <image> - Download an image
  • docker images - List local images
  • docker build -t <name> . - Build image from Dockerfile

Container Operations

  • docker run <image> - Create and start a new container
  • docker ps - List running containers
  • docker ps -a - List all containers (including stopped)
  • docker stop <container> - Stop a running container
  • docker rm <container> - Remove a container

Common Run Options

  • -d - Run in background (detached mode)
  • -p 8080:80 - Map host port 8080 to container port 80
  • -v /host/path:/container/path - Mount volume
  • --name myapp - Assign custom name

Docker Workflow

sequenceDiagram participant Dev as Developer participant DF as Dockerfile participant Img as Image participant Con as Container Dev->>DF: 1. Write Dockerfile Dev->>Img: 2. docker build Dev->>Con: 3. docker run Con->>Dev: 4. Application running

Dockerfile Basics

A Dockerfile contains step-by-step instructions to build an image:

FROM node:16
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Common Instructions

  • FROM - Base image to start from
  • WORKDIR - Set working directory
  • COPY - Copy files from host to container
  • RUN - Execute commands during build
  • EXPOSE - Document which ports the app uses
  • CMD - Default command when container starts

Best Practices

Security & Efficiency

  • Use official base images when possible
  • Keep images small by using minimal base images
  • Don't run containers as root user
  • Use .dockerignore to exclude unnecessary files

Development Workflow

  1. Write application code
  2. Create Dockerfile
  3. Build image locally
  4. Test container functionality
  5. Push to registry for deployment

Docker fundamentally changes how applications are packaged and deployed, providing consistency and reliability across the entire software delivery pipeline.


Backlinks