Repository navigation
Expand file tree
/
Copy pathdocker.txt
More file actions
188 lines (131 loc) · 6.03 KB
/
Copy pathdocker.txt
File metadata and controls
188 lines (131 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
*Docker*
Basic Docker Commands
List all available images
*$ docker images*
Build an image
*$ docker build -t [image_name] .*
Build an image with a version tag
*$ docker build -t [image_name]:[version] .*
Tag an image for a registry
*$ docker tag [image_name]:[version] [registry/image_name:version]*
Push image to registry
*$ docker push [registry/image_name:version]*
Pull an image from registry
*$ docker pull [registry/image_name:version]*
Run an image
*$ docker run [image_name]*
Run an image with port mapping
*$ docker run --name [container_name] -p [host_port]:[container_port] [image_name]*
Run an image in background
*$ docker run -d --name [container_name] -p [host_port]:[container_port] [image_name]*
Run an image with environment variables
*$ docker run -e [KEY]=[VALUE] [image_name]*
Run an image and get a shell
*$ docker run --rm -it [image_name] /bin/bash*
List running containers
*$ docker ps*
List all containers
*$ docker ps -a*
Run a command within the container
*$ docker exec -it [container_name/container_id] /bin/bash*
Stop a container gracefully
*$ docker stop [container_name]*
Kill a container immediately
*$ docker kill [container_name]*
Start a stopped container
*$ docker start [container_name]*
Remove a container
*$ docker rm [container_name]*
Remove an image
*$ docker rmi [image_name]*
Show logs
*$ docker logs [container_name]*
Follow logs live
*$ docker logs -f [container_name]*
Inspect full container details
*$ docker inspect [container_name]*
*Docker Compose*
Most Important Docker Compose Commands
Start all services
*$ docker compose up*
Start all services and rebuild images
*$ docker compose up --build*
Start all services in background
*$ docker compose up -d*
Show running services
*$ docker compose ps*
Show logs
*$ docker compose logs*
Follow logs live
*$ docker compose logs -f*
Open a shell in a running service
*$ docker compose exec [service_name] sh*
Stop services
*$ docker compose stop*
Stop and remove the stack
*$ docker compose down*
Stop and remove the stack including volumes
*$ docker compose down -v*
Validate the compose file
*$ docker compose config*
*Docker Concepts*
**Image** Packaged filesystem and startup configuration used to create containers
**Container** Instance of an image with its own runtime state; can be running or stopped
**Volume** Persistent storage for container data
**Network** Way for containers to communicate
**Registry** Remote place to store and pull images
*Kubernetes*
Mental Model
**Namespace** Groups resources; not a security boundary by itself.
**Deployment** Keeps the desired number of Pods running and manages updates.
**Pod** Runs one or more containers sharing networking and optionally volumes.
**Service** Stable network endpoint; usually selects Pods by labels, not names.
**Management** Deployment -> ReplicaSet (maintains replica count) -> Pods -> containers.
**Traffic** Client -> Service -> ready Pod. The Deployment is not in the traffic path.
Key Fields and Gotchas
**---** separates resource definitions within a YAML file.
**replicas** sets the desired Pod count, not the number of Services.
A Service's **selector** matches Pod **labels** within the same namespace.
**port** is the Service port; **targetPort** is the destination port number or named container port.
**spec.template** is the Pod blueprint; changing its image or env triggers a rollout when applied.
**ClusterIP** exposes the Service inside the cluster. **containerPort** alone publishes nothing.
**imagePullPolicy: Never** requires the image to already exist on the node; no registry pull.
**Readiness** controls traffic eligibility; **liveness** restarts unhealthy containers after repeated failures.
**requests** guide scheduling; **limits** constrain usage (CPU throttling, possible memory OOM kill).
Delete a Deployment-managed Pod: its ReplicaSet creates a replacement.
**port-forward** connects to one backing Pod; it does not demonstrate Service load balancing.
Commands
Commands below use the current context. Check it before mutations; add **--context [context]** to target a specific cluster.
*$ kubectl config current-context*
Check cluster
*$ kubectl cluster-info*
Apply objects from a YAML manifest
*$ kubectl apply -f [manifest.yaml]*
Apply a Kustomize directory (requires a kustomization file)
*$ kubectl apply -k [k8s_folder]*
Check common workload resources (get all does not include every resource type)
*$ kubectl get all -n [namespace]*
Inspect Pod labels and Service selectors
*$ kubectl get pods --show-labels -n [namespace]*
*$ kubectl describe service [service-name] -n [namespace]*
Wait for rollout
*$ kubectl rollout status deployment/[app-name] -n [namespace]*
Expose locally
*$ kubectl port-forward svc/[service-name] [local_port]:[service_port] -n [namespace]*
View logs
*$ kubectl logs deployment/[app-name] -n [namespace]*
Debug shell
*$ kubectl exec -it deployment/[app-name] -n [namespace] -- sh*
Force a new rollout (does not rebuild the image)
*$ kubectl rollout restart deployment/[app-name] -n [namespace]*
Scale app
*$ kubectl scale deployment/[app-name] --replicas=[number] -n [namespace]*
Delete objects from a YAML manifest; deleting a declared Namespace also deletes everything inside it
*$ kubectl delete -f [manifest.yaml]*
Delete a Kustomize app
*$ kubectl delete -k [k8s_folder]*
References
Deployments: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
Services: https://kubernetes.io/docs/concepts/services-networking/service/
Probes: https://kubernetes.io/docs/concepts/workloads/pods/probes/
Resources: https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/