Docker Core Concepts
Term |
Description |
Image |
A read-only template created by docker build , containing everything needed to run an application. |
Container |
A runnable instance of an image, representing the actual running application process. |
Volume |
A mechanism for persisting and sharing data between containers and the host filesystem. |
Dockerfile |
A “recipe” that defines how to build your image, specifying base image, dependencies, build steps, and startup command. |
.dockerignore |
A file that lists patterns for files and directories to exclude from the build context. |
docker-compose |
A tool for defining and running multi-container Docker applications via a docker-compose.yml file. |
Common Commands Quick Reference
# Build an image from the current directory’s Dockerfile
docker build -t my-next-app .
# Run a container (map host port 3000 to container port 3000)
docker run -p 3000:3000 my-next-app
# List all containers (running and stopped)
docker ps -a
# Start a stopped container
docker start <container-name-or-id>
# Stop a running container
docker stop <container-name-or-id>
# List all images on your system
docker images
# Remove a container
docker rm <container-id>
# Remove an image
docker rmi <image-id>
# Remove unused data (dangling images, stopped containers, unused networks)
docker system prune
Dockerfile
FROM node:22-alpine AS base
# Stage 1: Install dependencies
FROM base AS deps
WORKDIR /app
COPY package*.json ./
RUN npm install
# Stage 2: Build the application
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
# Stage 3: Run the application
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
EXPOSE 3000
CMD ["node", "server.js"]
.dockerignore
# Git
.git
.gitignore
# Dependencies
node_modules/
# Local env files
.env
.env.*.local
# Next.js
.next/
out/
dist/
# TypeScript
next-env.d.ts
# IDE
.vscode/
.idea/
# OS metadata
.DS_Store
Thumbs.db
# Logs
logs/
*.log
# Docker files
Dockerfile*
.dockerignore
# Documentation
README*.md
LICENSE
CONTRIBUTING.md