Docker Fundamentals
Master the basics of containerization. Before learning Kubernetes, you must understand how Docker builds, ships, and runs containers.
Containers vs Virtual Machines
A Virtual Machine (VM) includes a full operating system (Guest OS), which is resource-heavy. A Container is just an isolated process running on the host OS. Containers share the host OS kernel, making them lightweight, extremely fast to start, and highly portable.
Core Concepts
- Image: A read-only template with instructions for creating a Docker container. It contains the OS libraries, dependencies, and your application code.
- Container: A runnable instance of an image. You can create, start, stop, move, or delete a container.
- Registry: A storage and distribution system for named Docker images. (e.g., Docker Hub, AWS ECR).
- Dockerfile: A text document that contains all the commands a user could call on the command line to assemble an image.
The Dockerfile
A Dockerfile tells Docker how to build an image layer by layer.
# Use the official Node.js image as base FROM node:18-alpine # Set the working directory inside the container WORKDIR /app # Copy package.json and install dependencies COPY package*.json ./ RUN npm install # Copy the rest of the application code COPY . . # Expose the port the app runs on EXPOSE 3000 # Command to run the application CMD ["npm", "start"]
Volumes and Networking
Because containers are ephemeral (temporary), any data written inside a container is lost when it stops. Docker Volumes allow you to persist data outside the container lifecycle.
Docker Networks allow containers to communicate with each other securely, isolated from the host network.