🐳 Docker Components Explained
🛠️ Dockerfile
- A text file that contains a set of instructions to build a Docker image
- Specifies the base image, environment variables, commands to run, and files to copy
- Each instruction creates a new layer in the Docker image
- Example instructions: FROM, RUN, COPY, ENV, EXPOSE, CMD, etc.
- Helps in automation and consistency across different environments
🖼️ Docker Image
- A lightweight, standalone, and executable package that includes:
- Code
- Runtime
- Libraries
- Environment variables
- System tools
- Read-only (immutable) and layered, built from the Dockerfile
- Can be inherited from a base image (like python:3.9 or ubuntu:20.04)
- Stored in a registry, such as:
- Docker Hub (public)
- AWS ECR (Elastic Container Registry)
- Azure Container Registry (ACR)
🐳 Docker Container
- A running instance of a Docker image
- Containers are isolated environments that run applications
- Multiple containers can be created from the same image
- Containers share the same kernel as the host machine but have their own file system, network, and processes
- By default, a container only lives until the process inside is running (e.g., a Node.js app or Python script)
🔥 Container Lifecycle
docker run → Creates and starts a container
docker stop → Stops a running container
docker restart → Restarts a container
docker rm → Removes a container
docker logs → View container logs
📊 Docker Workflow
+---------------------------------------------------+
| Dockerfile |
|---------------------------------------------------|
| - Base Image: Node:16-alpine |
| - ENV variables (NODE_ENV, PORT, DB_HOST) |
| - Install dependencies |
| - Copy application code |
| - Expose port 3000 |
| - CMD to run the app |
+---------------------------------------------------+
↓
↓ Docker Build Command
↓ `docker build -t myapp .`
+---------------------------------------------------+
| Docker Image |
|---------------------------------------------------|
| - Contains code, runtime, libraries, env vars |---> | Push to Docker Hub | ECR |
| - Layered structure |
| - Immutable and reusable |
| - Stored in Docker Hub or private registry |
+---------------------------------------------------+
↓
↓ Docker Run Command
↓ `docker run -d -p 3000:3000 myapp`
+---------------------------------------------------+
| Docker Container |
|---------------------------------------------------|
| - Running instance of the image |
| - Isolated environment |
| - Own file system, network, and processes |
| - Runs app on port 3000 |
| - Dies when process stops |
+---------------------------------------------------+