Docker lets you package apps and dependencies into containersthat run the same everywhere. This guide covers the essentials to get you started fast.
1. What is Docker?
Docker is a containerization platform. Containers share the OS kernel, so they are lighter and faster than full virtual machines.
- Portability: run the same in any environment.
- Consistency: dev, test and prod match.
- Isolation: apps don't conflict.
- Efficiency: fewer resources than VMs.
2. Core concepts
- Image: read-only template with app and dependencies.
- Container: running instance of an image.
- Dockerfile: recipe to build images.
3. Install Docker
Windows and macOS: install Docker Desktop from the official site.
Linux (Ubuntu/Debian):
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce
docker --version4. Essential commands
Images:
docker pull nginx
docker images
docker rmi nginxContainers:
docker run -d -p 8080:80 nginx
docker ps
docker ps -a
docker stop <container_id>
docker rm <container_id>
docker exec -it <container_id> sh5. Your first Docker project (Node.js)
Folder:
mkdir my-first-docker
cd my-first-dockerpackage.json:
{
"name": "my-first-docker",
"version": "1.0.0",
"scripts": { "start": "node server.js" },
"dependencies": { "express": "^4.18.0" }
}server.js:
const express = require("express");
const app = express();
const PORT = process.env.PORT || 3000;
app.get("/", (req, res) => {
res.send("<h1>My first Docker app!</h1>");
});
app.listen(PORT, () => console.log("Running on port " + PORT));Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]Build and run:
docker build -t my-app .
docker run -d -p 3000:3000 --name my-container my-app
curl http://localhost:3000.dockerignore:
node_modules
npm-debug.log
.git
.DS_Store6. Docker Compose
For multiple services, create a docker-compose.yml:
version: "3.8"
services:
web:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
depends_on:
- db
db:
image: postgres:13
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:Run:
docker compose up -d
docker compose down7. Best practices
- Prefer official images.
- Use
.dockerignoreto reduce image size. - Combine layers where possible.
- Use multi-stage builds for larger apps.
- Avoid running as root.
- Add health checks.
Next steps
Explore image optimization, CI/CD with Docker, and orchestration with Docker Swarm or Kubernetes.