Docker Introduction
🚢 Introduction to Docker: A Comprehensive Guide¶
Understanding Docker 🧐¶
Docker
🐳 is a containerization 📦 platform that enables developers to package applications and their dependencies into self-sufficient units calledcontainers
.This approach ensures consistency 🔁 across various environments 🌍, mitigating the "works on my machine" 🖥️ issue.
Unlike
virtual machines
, Docker containers share the host operating system's kernel, making them more lightweight ⚡ and faster 🚀 to initialize.
Benefits of Using Docker 🎯¶
Portability: Applications run identically across different environments.
Isolation: Containers encapsulate dependencies, avoiding conflicts ⚔️ with the host system.
Scalability: Deploy multiple instances efficiently to accommodate workload fluctuations 📊.
Resource Efficiency:
Containers
utilize system resources more effectively thanvirtual machines
.Consistency: Maintains uniformity across development, testing, and production stages.
Rapid Deployment:
Containers
launch in seconds, streamlining development and deployment workflows.
Installing Docker ⚙️¶
Download and install Docker Desktop:
- Windows & Mac 🖥️
- Linux 🐧
Verify the installation
docker --version
Core Docker Concepts¶
Image
: A predefined blueprint for creating containers.Container
: A running instance of an image, executing isolated processes.Dockerfile
: A script defining instructions to build an image.Docker Hub
: Arepository
hosting prebuilt images.Volume
: A mechanism for persisting data beyond the container lifecycle.Networking
: Configurable networking options to facilitate inter-container communication.
Essential Docker Commands 🔑¶
Running a Container
- This command fetches the hello-world image and executes it within a
container
.
docker run hello-world
- This command fetches the hello-world image and executes it within a
Listing Containers
docker ps
- To view all containers, including stopped ones:
docker ps -a
Stopping and Removing Containers
docker stop <container_id> docker rm <container_id>
Pulling an Image
docker pull nginx
Running a Container in Detached Mode
- The Nginx 🌐 server is now accessible at http://localhost:8080.
docker run -d -p 8080:80 nginx
Removing an Image
docker rmi nginx
Building Custom Docker Images¶
Create a
Dockerfile
:# Use an official Python image as a base 🐍 FROM python:3.9-slim # Set the working directory 📂 WORKDIR /app # Copy local files into the container 📑 COPY . /app # Install dependencies 🏗️ RUN pip install --no-cache-dir -r requirements.txt # Define the default command 🎯 CMD ["python", "app.py"]
Build the image
docker build -t my-python-app .
Run the container:
docker run -d -p 5000:5000 my-python-app
Deploying a Flask Application with Docker¶
Create a
app.py
file:from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello, Docker! 👋🐳" if __name__ == "__main__": app.run(host='0.0.0.0', port=5000)
Define dependencies in
requirements.txt
Use the previously defined
Dockerfile
.Build and deploy the container:
docker build -t flask-app . docker run -d -p 5000:5000 flask-app
Advanced Docker Features¶
Docker Compose¶
Docker Compose
simplifies the management of multi-container applications using adocker-compose.yml
file 📑:- For applications requiring
multiple containers
(e.g., aweb app
and adatabase
), Docker Compose simplifies container management.
version: '3' services: web: image: flask-app ports: - "5000:5000" database: image: postgres environment: POSTGRES_USER: user POSTGRES_PASSWORD: password
- For applications requiring
Running the Multi-Container Application:
docker-compose up -d
Inspecting Containers and Logs¶
View container details:
docker inspect <container_id>
View real-time logs:
docker logs -f <container_id>
Execute commands inside a running container:
docker exec -it <container_id> bash
Persistent Storage Using Volumes¶
When a container stops or is removed, any data stored inside it is lost.
To avoid data loss and ensure persistence, Docker provides
volumes
, a mechanism to store data outside the container's writable layer.Volumes
are independent storage units that can be mounted to containers and persist beyond the container's lifecycle.
Create a named volume:
docker volume create my_volume
Run a container with the volume attached:
docker run -d -v my_volume:/data my-python-app
Inspect the volume details:
docker volume inspect my_volume
List all volumes:
docker volume ls
Remove a volume (only if it's not being used):
docker volume rm my_volume
🔹 Bind Mounts vs. Volumes
Volumes: Managed by Docker, stored in a specific directory controlled by Docker (
/var/lib/docker/volumes/
).Bind Mounts: Use existing directories on the host machine and provide a direct link to the container.
Which One to Use?
Volumes
are preferred for containerized applications because they offer better performance, security, and are less dependent on host-specific paths.
Networking in Docker¶
Docker
provides multiple networking options that allow containers to communicate with each other or external services.
The default networking modes include:
Bridge Network (Default)¶
Used for communication between multiple containers running on the same Docker host.
Example: Connecting a web server container to a database container.
Create a network:
docker network create my_bridge
Run containers in the same network:
docker run -d --network=my_bridge --name db postgres docker run -d --network=my_bridge --name web nginx
Host Network¶
Allows a container to share the host's networking stack.
Example:
docker run --network=host -d nginx
Use Case: Useful for applications requiring direct network access without NAT.
Overlay Network¶
Enables communication between containers running on different Docker hosts.
Used in Docker Swarm for managing multi-container deployments.