1.1 Images and Containers
Images: Read-only templates used to create containers. They are built from a Dockerfile.
Containers: Runnable instances of an image. They isolate the application from the host system.
1.2 Dockerfile Best Practices
Writing efficient Dockerfiles is crucial for security and performance.
- Use multi-stage builds to reduce image size.
- Minimize the number of layers by combining commands.
- Use specific tags (e.g.,
node:14-alpine) instead oflatest. - Run as a non-root user for security.
1.3 Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. You use a YAML file to configure your application's services.
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
1.4 Networking and Volumes
Networking: Allows containers to communicate with each other and the outside world.
Volumes: Persist data generated by and used by Docker containers.
🎯 Practical Exercise
Create a Dockerfile for a simple Python or Node.js app using a multi-stage build. Then, use docker-compose to spin it up along with a Redis database.