Skip to content

Commit 9c1253f

Browse files
committed
Docker Notes
1 parent a5f1abd commit 9c1253f

File tree

1 file changed

+227
-0
lines changed

1 file changed

+227
-0
lines changed

Dockers/README.md

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
# 🚀 Docker Quick Start Guide
2+
3+
A simplified reference for getting started with Docker: from installation, pulling images, running containers, and publishing to Docker Hub.
4+
5+
---
6+
7+
## 📥 Installation
8+
9+
Install Docker from the official site:
10+
👉 [https://docs.docker.com/get-docker/](https://docs.docker.com/get-docker/)
11+
12+
Verify installation:
13+
14+
```bash
15+
docker --version
16+
docker info
17+
```
18+
19+
---
20+
21+
## 📦 Pulling and Managing Docker Images
22+
23+
```bash
24+
docker pull <image_name> # Download image from Docker Hub
25+
docker images # List all downloaded images
26+
```
27+
28+
Example:
29+
30+
```bash
31+
docker pull hello-world
32+
```
33+
34+
---
35+
36+
## 🏃 Running Containers
37+
38+
```bash
39+
docker run <image_name> # Runs a new container
40+
docker run -it <image_name> # Runs interactively
41+
```
42+
43+
> Each run creates a new container with a random ID and name.
44+
45+
---
46+
47+
## 📋 Viewing Containers
48+
49+
```bash
50+
docker ps # Show running containers
51+
docker ps -a # Show all containers (running + stopped)
52+
```
53+
54+
---
55+
56+
## 🧱 Container Management
57+
58+
```bash
59+
docker start <container_id|name> # Start a container
60+
docker stop <container_id|name> # Stop a container
61+
docker rename old_name new_name # Rename a container
62+
docker rm <container_id|name> # Remove a stopped container
63+
docker container prune # Remove all stopped containers
64+
```
65+
66+
---
67+
68+
## 🔄 Running Commands in Containers
69+
70+
```bash
71+
docker exec -it <container_id|name> <command> # Run command inside container
72+
docker exec -it my_container node # Start Node.js shell
73+
docker exec -it my_container /bin/bash # Access Linux shell
74+
```
75+
76+
---
77+
78+
## 📁 Copying Files
79+
80+
### From Host to Container
81+
82+
```bash
83+
docker cp ./example.txt <container_name>:/example.txt
84+
```
85+
86+
### From Container to Host
87+
88+
```bash
89+
docker cp <container_name>:/example.txt ./copied.txt
90+
```
91+
92+
> Use `cat <filename>` inside the container to view file contents.
93+
94+
---
95+
96+
## 📄 Building Docker Images
97+
98+
### Dockerfile Example:
99+
100+
```Dockerfile
101+
FROM alpine:latest
102+
CMD ["echo", "Hello, Docker"]
103+
```
104+
105+
### Build Image
106+
107+
```bash
108+
docker build -t my_custom_image .
109+
```
110+
111+
---
112+
113+
## 🗃️ Docker Volumes (Data Persistence)
114+
115+
```bash
116+
docker volume create my_volume
117+
docker run -v my_volume:/data alpine
118+
docker volume ls
119+
```
120+
121+
> Volumes allow data to persist even after a container is deleted.
122+
123+
---
124+
125+
## 📦 Docker Compose (Multi-Container Apps)
126+
127+
Create a `docker-compose.yml` file:
128+
129+
```yaml
130+
version: '3'
131+
services:
132+
web:
133+
image: nginx
134+
ports:
135+
- "80:80"
136+
```
137+
138+
Start with:
139+
140+
```bash
141+
docker-compose up
142+
```
143+
144+
Stop with:
145+
146+
```bash
147+
docker-compose down
148+
```
149+
150+
---
151+
152+
## 🚫 Ignoring Files: `.dockerignore`
153+
154+
Create a `.dockerignore` file to exclude files/folders from the image:
155+
156+
```
157+
node_modules
158+
*.log
159+
Dockerfile
160+
```
161+
162+
---
163+
164+
## 📤 Docker Hub: Push Image
165+
166+
1. Login:
167+
168+
```bash
169+
docker login
170+
```
171+
172+
2. Tag the image:
173+
174+
```bash
175+
docker tag my_image <username>/<repository>:latest
176+
```
177+
178+
3. Push the image:
179+
180+
```bash
181+
docker push <username>/<repository>:latest
182+
```
183+
184+
4. Pull from anywhere:
185+
186+
```bash
187+
docker pull <username>/<repository>:latest
188+
```
189+
190+
---
191+
192+
## 🏢 Docker Registry
193+
194+
> A Docker registry is a system for storing and distributing Docker images.
195+
196+
- Docker Hub is the default public registry.
197+
- Run your own private registry:
198+
199+
```bash
200+
docker run -d -p 5000:5000 --name registry registry:2
201+
```
202+
203+
---
204+
205+
## 🛡️ Best Practices
206+
207+
- Use lightweight base images (like `alpine`)
208+
- Use multi-stage builds for production apps
209+
- Clean up unused packages in Dockerfile
210+
- Tag images clearly (e.g., `:v1`, `:latest`, `:prod`)
211+
- Use `.dockerignore` to reduce image size and build time
212+
213+
---
214+
215+
## ✅ Useful Summary Commands
216+
217+
| Action | Command |
218+
|------------------------|-------------------------------------|
219+
| List Images | `docker images` |
220+
| Run Container | `docker run <image>` |
221+
| List Containers | `docker ps -a` |
222+
| Build Image | `docker build -t name .` |
223+
| Exec into Container | `docker exec -it <id> bash` |
224+
| Copy Files | `docker cp` |
225+
| Push to Docker Hub | `docker push` |
226+
227+
---

0 commit comments

Comments
 (0)