# Named volume (Docker-managed)
docker run -v mydata:/app/data image
# Bind mount (host path)
docker run -v $(pwd)/config:/app/config:ro image
# tmpfs mount (in-memory, no persistence)
docker run --tmpfs /app/tmp image
Networking
Command
Description
docker network ls
List networks
docker network create mynet
Create a bridge network
docker network inspect mynet
Network details
docker network connect mynet container
Attach container to network
docker network disconnect mynet container
Detach container from network
docker network rm mynet
Remove a network
# Containers on the same user-defined network can resolve each other by name
docker network create app-net
docker run -d --name api --network app-net my-api
docker run -d --name web --network app-net my-web
# "web" can reach "api" via http://api:3000
Dockerfile Reference
# Multi-stage build — keeps final image small
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "dist/index.js"]
Instruction
Purpose
FROM
Base image (use alpine variants for smaller size)
WORKDIR
Set working directory inside container
COPY
Copy files from build context
RUN
Execute command during build (each creates a layer)
# Database dump
docker exec db pg_dump -U user dbname > backup.sql
# Run migration
docker compose exec web npm run migrate
# Quick debug container in same network
docker run --rm -it --network app-net alpine sh
Resource limits
docker run -d --name api \
--memory=512m \
--cpus=1.5 \
--restart=unless-stopped \
my-api
Troubleshooting
Problem
Solution
Permission denied on socket
sudo usermod -aG docker $USER then log out/in
Port already in use
ss -tulnp | grep :PORT to find and kill the process
Container exits immediately
docker logs container — check for missing env vars or config errors
Out of disk space
docker system prune -a --volumes to reclaim space
DNS not working in container
Check /etc/docker/daemon.json for custom DNS, or use --dns 8.8.8.8
Build cache stale
docker build --no-cache -t name .
Can't connect between containers
Ensure they're on the same user-defined network (not default bridge)
# Useful debug commands
docker inspect --format='{{.State.ExitCode}}' container
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container
docker system df # Show Docker disk usage
docker events --since 10m # Recent Docker events