1+ # --------------------------------------------------------------------------------
2+ # Docker Reference and Guide
3+ #
4+ # ReferenceCollection.com
5+ # Licensed under CC BY-SA
6+ # --------------------------------------------------------------------------------
7+
8+ # TABLE OF CONTENTS
9+ # -----------------
10+ # 1. Introduction to Docker
11+ # 2. Core Concepts and Architecture
12+ # 3. Installation and Setup
13+ # 4. Docker Images and Containers
14+ # 5. Docker Networking
15+ # 6. Docker Volumes
16+ # 7. Docker Compose
17+ # 8. Docker Swarm
18+ # 9. Docker Security
19+ # 10. Container Registry
20+ # 11. Monitoring and Logging
21+ # 12. Troubleshooting and Maintenance
22+
23+ # --------------------------------------------------------------------------------
24+ # 1. Introduction to Docker
25+ # --------------------------------------------------------------------------------
26+
27+ # Docker is an open-source platform that automates the deployment, scaling, and
28+ # management of applications using containerization. It allows developers to package
29+ # applications and their dependencies into a standardized unit called a container.
30+
31+ # Key Features:
32+ # - Lightweight: Containers share the host OS kernel, making them more efficient than VMs
33+ # - Portable: Containers can run on any system that supports Docker
34+ # - Scalable: Easily scale applications across multiple environments
35+ # - Secure: Isolates applications from each other and the host system
36+ # - Version Control: Track container image versions
37+ # - DevOps Integration: Supports CI/CD pipelines
38+
39+ # Use Cases:
40+ # - Microservices architecture
41+ # - Development and testing environments
42+ # - Continuous Integration/Deployment (CI/CD)
43+ # - Application isolation and security
44+
45+ # --------------------------------------------------------------------------------
46+ # 2. Core Concepts and Architecture
47+ # --------------------------------------------------------------------------------
48+
49+ # Architecture Components:
50+ # - Docker Engine: Core container runtime
51+ # - Docker Daemon: Background service managing containers
52+ # - Docker Client: CLI tool for Docker interaction
53+ # - Docker Registry: Storage for Docker images
54+ # - Docker Objects: Images, containers, networks, volumes
55+
56+ # Key Concepts:
57+ # - Images: Read-only templates for creating containers
58+ # - Containers: Runnable instances of images
59+ # - Dockerfile: Instructions for building images
60+ # - Registry: Repository for storing and sharing images
61+ # - Volume: Persistent data storage
62+ # - Network: Communication between containers
63+
64+ # --------------------------------------------------------------------------------
65+ # 3. Installation and Setup
66+ # --------------------------------------------------------------------------------
67+
68+ # Linux (Ubuntu/Debian):
69+ curl -fsSL https://get.docker.com -o get-docker.sh
70+ sudo sh get-docker.sh
71+
72+ # MacOS Or Windows:
73+ # Download and install Docker Desktop from https://www.docker.com/products/docker-desktop
74+
75+ # Post-installation steps:
76+ sudo usermod -aG docker $USER # Add user to docker group
77+ newgrp docker # Apply group changes
78+
79+ # Verify installation:
80+ docker --version
81+ docker info
82+
83+ # Configure Docker daemon:
84+ cat > /etc/docker/daemon.json << EOF
85+ {
86+ "log-driver": "json-file",
87+ "log-opts": {
88+ "max-size": "10m",
89+ "max-file": "3"
90+ }
91+ }
92+ EOF
93+
94+ # Start and enable Docker service:
95+ sudo systemctl start docker
96+ sudo systemctl enable docker
97+
98+ # --------------------------------------------------------------------------------
99+ # 4. Docker Images and Containers
100+ # --------------------------------------------------------------------------------
101+ # Docker Images:
102+ # - A Docker image is a lightweight, standalone, and executable software package
103+ # that includes everything needed to run a piece of software.
104+
105+ # Image Commands:
106+ docker pull < image> # Download an image from Docker Hub
107+ docker build -t < name> . # Build an image from a Dockerfile
108+ docker images # List all images
109+ docker tag < image> < new> # Tag an image
110+ docker push < image> # Push image to registry
111+ docker rmi < image> # Remove an image
112+
113+ # Docker Containers:
114+ # - A container is a runtime instance of a Docker image, isolated from other containers
115+ # and the host system.
116+
117+ # Container Commands:
118+ docker run < image> # Run a container from an image
119+ docker ps # List running containers
120+ docker ps -a # List all containers
121+ docker stop < container> # Stop a running container
122+ docker start < container> # Start a stopped container
123+ docker restart < container> # Restart a container
124+ docker rm < container> # Remove a container
125+ docker exec -it < container> < command> # Execute command in container
126+
127+ # Resource Constraints:
128+ docker run --memory=512m < image> # Memory limit
129+ docker run --cpus=2 < image> # CPU limit
130+ docker run --pids-limit=100 < image> # Process limit
131+
132+ # --------------------------------------------------------------------------------
133+ # 5. Docker Networking
134+ # --------------------------------------------------------------------------------
135+ # Docker provides several networking options to connect containers to each other
136+ # and to the outside world.
137+
138+ # Network Types:
139+ # - Bridge: Default network type, allows containers to communicate on the same host
140+ # - Host: Shares the host's networking namespace
141+ # - None: Disables networking for a container
142+ # - Overlay: Enables communication between containers on different Docker hosts
143+ # - Macvlan: Assign MAC address to container
144+
145+ # Network Commands:
146+ docker network ls # List all networks
147+ docker network create < name> # Create a new network
148+ docker network connect < net> < container> # Connect container to network
149+ docker network disconnect < net> < container> # Disconnect container from network
150+ docker network inspect < name> # View network details
151+ docker network rm < name> # Remove a network
152+
153+ # Network Configuration:
154+ docker run --network=host < image> # Use host network
155+ docker run --dns=8.8.8.8 < image> # Custom DNS
156+ docker run --add-host=host:ip < image> # Add hosts entry
157+
158+ # --------------------------------------------------------------------------------
159+ # 6. Docker Volumes
160+ # --------------------------------------------------------------------------------
161+ # Docker volumes are used to persist data generated by and used by Docker containers.
162+
163+ # Volume Types:
164+ # - Named Volumes: Managed by Docker
165+ # - Bind Mounts: Mount host directory
166+ # - tmpfs: Temporary filesystem in memory
167+
168+ # Volume Commands:
169+ docker volume create < name> # Create a new volume
170+ docker volume ls # List all volumes
171+ docker volume inspect < name> # Display volume information
172+ docker volume rm < name> # Remove a volume
173+ docker volume prune # Remove unused volumes
174+
175+ # Volume Usage:
176+ docker run -v < vol> :/path < image> # Use named volume
177+ docker run -v /host:/container < image> # Bind mount
178+ docker run --tmpfs /path < image> # Use tmpfs mount
179+
180+ # --------------------------------------------------------------------------------
181+ # 7. Docker Compose
182+ # --------------------------------------------------------------------------------
183+ # Docker Compose is a tool for defining and running multi-container applications.
184+
185+ # Example docker-compose.yml:
186+ version: ' 3.8'
187+ services:
188+ web:
189+ image: nginx
190+ ports:
191+ - " 80:80"
192+ volumes:
193+ - ./html:/usr/share/nginx/html
194+ db:
195+ image: postgres
196+ environment:
197+ POSTGRES_PASSWORD: secret
198+
199+ # Common Commands:
200+ docker-compose up -d # Start services in detached mode
201+ docker-compose down # Stop and remove services
202+ docker-compose ps # List services
203+ docker-compose logs # View service logs
204+ docker-compose exec web bash # Execute command in service
205+
206+ # --------------------------------------------------------------------------------
207+ # 8. Docker Swarm
208+ # --------------------------------------------------------------------------------
209+ # Docker Swarm is a native clustering and orchestration tool for Docker containers.
210+
211+ # Key Concepts:
212+ # - Node: An instance of Docker Engine in the swarm
213+ # - Service: A task that can be scaled across nodes
214+ # - Stack: Collection of services that make up an application
215+
216+ # Swarm Commands:
217+ docker swarm init # Initialize a swarm
218+ docker swarm join # Join a swarm
219+ docker node ls # List nodes
220+ docker service create # Create a service
221+ docker service ls # List services
222+ docker service scale web=3 # Scale service
223+ docker stack deploy -c docker-compose.yml < stack> # Deploy stack
224+
225+ # --------------------------------------------------------------------------------
226+ # 10. Docker Security
227+ # --------------------------------------------------------------------------------
228+
229+ # Best Practices:
230+ # - Use official images from Docker Hub
231+ # - Keep Docker and images up to date
232+ # - Scan images for vulnerabilities
233+ # - Run containers as non-root users
234+ # - Use read-only filesystems when possible
235+ # - Implement resource limits
236+ # - Enable content trust
237+
238+ # Security Commands:
239+ docker scan < image> # Scan image for vulnerabilities
240+ docker trust sign < image> # Sign image
241+ docker run --read-only < image> # Read-only filesystem
242+ docker run --security-opt=no-new-privileges < image> # Restrict privileges
243+
244+ # --------------------------------------------------------------------------------
245+ # 9. Container Registry
246+ # --------------------------------------------------------------------------------
247+
248+ # Registry Operations:
249+ docker login # Log in to a registry
250+ docker logout # Log out from a registry
251+ docker pull < image> # Pull image from registry
252+ docker push < image> # Push image to registry
253+
254+ # Private Registry:
255+ docker run -d \
256+ -p 5000:5000 \
257+ --restart=always \
258+ --name registry \
259+ registry:2
260+
261+ # --------------------------------------------------------------------------------
262+ # 11. Monitoring and Logging
263+ # --------------------------------------------------------------------------------
264+
265+ # Monitoring Commands:
266+ docker stats # View container resource usage
267+ docker events # View real-time events
268+ docker top < container> # View container processes
269+
270+ # Logging:
271+ docker logs < container> # View container logs
272+ docker logs -f < container> # Follow log output
273+ docker logs --tail 100 < container> # View last 100 lines
274+
275+ # --------------------------------------------------------------------------------
276+ # 12. Troubleshooting and Maintenance
277+ # --------------------------------------------------------------------------------
278+
279+ # Debugging Commands:
280+ docker logs < container> # View container logs
281+ docker exec -it < container> /bin/sh # Access container shell
282+ docker inspect < container> # View detailed information
283+ docker events # Monitor Docker events
284+
285+ # Cleanup Commands:
286+ docker system prune # Remove unused data
287+ docker container prune # Remove stopped containers
288+ docker image prune # Remove unused images
289+ docker volume prune # Remove unused volumes
290+ docker network prune # Remove unused networks
291+
292+ # System Information:
293+ docker info # View system-wide information
294+ docker version # View Docker version
295+ docker system df # View Docker disk usage
296+
297+ # Happy Containerizing!
0 commit comments