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
Component | Description | Example |
---|---|---|
Image | Read-only template containing application code | nginx:latest |
Container | Running instance of an image | Active web server |
Dockerfile | Text file with instructions to build images | Recipe 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 imagedocker images
- List local imagesdocker build -t <name> .
- Build image from Dockerfile
Container Operations
docker run <image>
- Create and start a new containerdocker ps
- List running containersdocker ps -a
- List all containers (including stopped)docker stop <container>
- Stop a running containerdocker 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 fromWORKDIR
- Set working directoryCOPY
- Copy files from host to containerRUN
- Execute commands during buildEXPOSE
- Document which ports the app usesCMD
- 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
- Write application code
- Create Dockerfile
- Build image locally
- Test container functionality
- 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