irongit

Good to know things for docker

Update docker.README.md

huncholanehuncholaneauthored committed by GitHub
parent 3a36901commit a7c87960c369240a66c6e0f8028da18517395572Browse files

1 file changed, +240 -30

+240-30docker.README.md
@@ -1,30 +1,240 @@
1-# Docker Cheatsheet
2-## Pro Tips
3-- Copy .gitignore into .dockerignore
4-
5-## Mantainance
6-### Remove
7-- Remove all dangling images
8- - `docker rmi $(docker images --filter "dangling=true" -q)`
9-
10-## Single image
11-1. Build
12-2. Run
13-3. Remove
14-4. 1
15-### [Build](https://docs.docker.com/get-started/02_our_app/)
16-```docker build -t getting-started .```
17-- `-t` name
18-### [Run](https://docs.docker.com/engine/reference/run/)
19-```docker run [OPTIONS] IMAGE[:TAG|@DIGEST] [COMMAND] [ARG...]```
20-- How to do volumes
21- - `$(pwd)/volume:/volume` for in same folder
22-- Options
23- - `-v`: volume
24- - `-p`: publish but really port `80:80` the left is on the outside and the right is on the inside
25- - `-i`: allows STDIN
26- - `-t`: get a terminal
27- - `--name` yep you have to type out name
28-
29-## Favorite Images
30-- [Node](https://hub.docker.com/_/node) `node:20-alpine3.17`
1+Sure! Here's a Docker cheat sheet in the form of a README with some common complex commands and concepts:
2+
3+```markdown
4+# Docker Cheat Sheet
5+
6+## Table of Contents
7+- [Basic Commands](#basic-commands)
8+- [Images](#images)
9+- [Containers](#containers)
10+- [Volumes](#volumes)
11+- [Networking](#networking)
12+- [Docker Compose](#docker-compose)
13+- [Dockerfile](#dockerfile)
14+- [Miscellaneous](#miscellaneous)
15+
16+## Basic Commands
17+
18+### Build an image
19+```bash
20+docker build -t <image_name> .
21+```
22+
23+### Run a container
24+```bash
25+docker run <image_name>
26+```
27+
28+### List running containers
29+```bash
30+docker ps
31+```
32+
33+### Stop a running container
34+```bash
35+docker stop <container_id>
36+```
37+
38+### Remove a container
39+```bash
40+docker rm <container_id>
41+```
42+
43+### List Docker images
44+```bash
45+docker images
46+```
47+
48+### Remove an image
49+```bash
50+docker rmi <image_id>
51+```
52+
53+## Images
54+
55+### Pull an image from Docker Hub
56+```bash
57+docker pull <image_name>
58+```
59+
60+### Push an image to Docker Hub
61+```bash
62+docker push <image_name>
63+```
64+
65+### Tag an image
66+```bash
67+docker tag <source_image> <target_image>
68+```
69+
70+## Containers
71+
72+### Start a stopped container
73+```bash
74+docker start <container_id>
75+```
76+
77+### Attach to a running container
78+```bash
79+docker attach <container_id>
80+```
81+
82+### Execute a command in a running container
83+```bash
84+docker exec <container_id> <command>
85+```
86+
87+### Copy files between host and container
88+```bash
89+docker cp <source_path> <container_id>:<target_path>
90+docker cp <container_id>:<source_path> <target_path>
91+```
92+
93+### Inspect container details
94+```bash
95+docker inspect <container_id>
96+```
97+
98+## Volumes
99+
100+### Create a named volume
101+```bash
102+docker volume create <volume_name>
103+```
104+
105+### Mount a volume in a container
106+```bash
107+docker run -v <volume_name>:<container_path> <image_name>
108+```
109+
110+### Mount a host directory in a container
111+```bash
112+docker run -v <host_path>:<container_path> <image_name>
113+```
114+
115+### List Docker volumes
116+```bash
117+docker volume ls
118+```
119+
120+### Remove a volume
121+```bash
122+docker volume rm <volume_name>
123+```
124+
125+## Networking
126+
127+### Create a user-defined network
128+```bash
129+docker network create <network_name>
130+```
131+
132+### Run a container on a specific network
133+```bash
134+docker run --network <network_name> <image_name>
135+```
136+
137+### Connect a container to a network
138+```bash
139+docker network connect <network_name> <container_name>
140+```
141+
142+### Disconnect a container from a network
143+```bash
144+docker network disconnect <network_name> <container_name>
145+```
146+
147+### List Docker networks
148+```bash
149+docker network ls
150+```
151+
152+## Docker Compose
153+
154+### Start services defined in a Compose file
155+```bash
156+docker-compose up
157+```
158+
159+### Start services in the background
160+```bash
161+docker-compose up -d
162+```
163+
164+### Stop services defined in a Compose file
165+```bash
166+docker-compose down
167+```
168+
169+### Scale services
170+```bash
171+docker-compose up --scale <service_name>=<count>
172+```
173+
174+## Dockerfile
175+
176+### Specify a base image
177+```Dockerfile
178+FROM <base_image>
179+```
180+
181+### Set the working directory
182+```Dockerfile
183+WORKDIR /app
184+```
185+
186+### Copy files to the image
187+```Dockerfile
188+COPY <source_path> <target_path>
189+```
190+
191+
192+
193+### Run commands in the image
194+```Dockerfile
195+RUN <command>
196+```
197+
198+### Expose a port
199+```Dockerfile
200+EXPOSE <port>
201+```
202+
203+## Miscellaneous
204+
205+### Monitor resource usage of containers
206+```bash
207+docker stats
208+```
209+
210+### Remove all stopped containers
211+```bash
212+docker container prune
213+```
214+
215+### Remove all unused images
216+```bash
217+docker image prune
218+```
219+
220+### Remove all unused volumes
221+```bash
222+docker volume prune
223+```
224+
225+### Remove all unused networks
226+```bash
227+docker network prune
228+```
229+
230+### View logs of a container
231+```bash
232+docker logs <container_id>
233+```
234+
235+### Clean up Docker resources
236+```bash
237+docker system prune
238+```
239+
240+```