Docker and Containers
Docker is a tool that packages applications into lightweight, independent containers, allowing them to run consistently anywhere.
1. Why Docker?
- Environment Consistency: Solves the "It works on my machine" problem. Perfectly aligns development, testing, and production environments.
- Isolation: Safely run apps using different library versions on a single server.
- Deployment Speed: Use images to skip infrastructure configuration and launch apps immediately.
2. Core Docker Concepts
- Image: A template including executable files, libraries, settings, etc.
- Container: The running state of an image.
- Dockerfile: A specification for creating an image.
3. Dockerfile Example
# Set base image
FROM node:18
# Set working directory
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
# Copy source and execute
COPY . .
CMD ["npm", "start"]
4. Essential Commands
docker build -t my-app . # Build an image
docker run -p 8080:80 my-app # Run a container (port binding)
docker ps # Check running containers
docker stop [ID] # Stop a container
In the next section, we'll learn about CI/CD, which automatically detects code changes and deploys them.