https://github.com/tahmid-tanzim/docker-cheat-sheet
Docker and Kubernetes Cheat Sheet
https://github.com/tahmid-tanzim/docker-cheat-sheet
docker docker-compose kubernetes
Last synced: 3 months ago
JSON representation
Docker and Kubernetes Cheat Sheet
- Host: GitHub
- URL: https://github.com/tahmid-tanzim/docker-cheat-sheet
- Owner: tahmid-tanzim
- Created: 2016-04-22T04:40:44.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-09-26T18:00:13.000Z (almost 3 years ago)
- Last Synced: 2025-04-07T03:53:21.281Z (over 1 year ago)
- Topics: docker, docker-compose, kubernetes
- Language: JavaScript
- Homepage:
- Size: 388 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Docker Cheat Sheet
Common commands on Docker and Kubernetes.
## 1. Manipulating Containers with the Docker Client
### Container Lifecycle
`docker run` = `docker create` + `docker start`
### Creating and Running a Container from an Image
Command Format - `docker run `
```shell
$ docker pull busybox:stable
$ docker run busybox:stable echo Hello World, Tanzim!
$ docker run busybox:stable ping google.com
```
### List all running/stopped containers
```shell
$ docker ps
$ docker ps -a
```
### Restarting stopped container
```shell
$ docker start -a
```
### Remove all stopped containers
```shell
$ docker container prune
```
### Remove unused data
```shell
$ docker system prune
```
### Get logs from a container
```shell
$ docker logs
```
### Stopping running containers
```shell
$ docker stop
$ docker kill
```
### Executing commands in running containers
```shell
$ docker exec -it
# Executing commands one-time without interactive shell
$ docker exec
$ docker run -it
```
## 2. Building Custom Images Through Docker Server
### Building custom docker image from Dockerfile
```shell
$ docker build -t .
$ docker run
```
### Generate docker image manually
```shell
$ docker commit -c 'CMD["python", "manage.py", "runserver"]'
```
## 3. Making Real Projects with Docker
### Generate docker image manually
```shell
$ cd web-app
$ docker build -t oneleven/web-app:1.0.0 .
$ docker run --name node-app -p 3000:8080 -d oneleven/web-app:1.0.0
# Go to - http://localhost:3000
$ docker exec -it node-app sh
$ docker stop node-app
```
## 4. Docker Compose with Multiple Local Containers
### Generate docker image manually
```shell
$ cd count-visits
$ docker-compose build
$ docker-compose up -d
$ docker-compose down
```
### Container Restart Policy
| Command | Description |
|----------------|---------------------------------------------------------------------------------------------|
|`always` | Always attempt to restart. Recommended for web application. |
|`on-failure` | Only restart if the container stops with an error code. Recommended for worker application. |
|`unless-stopped`| Always restart unless anyone forcibly stop it. |
## 5. Kubernetes
### Layer of Abstraction
1. Deployment manages ReplicaSet
2. ReplicaSet manages Pod
3. Pod is an abstraction of Docker container
### Setup Minikube
https://minikube.sigs.k8s.io/docs/start/
#### Install
```shell
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64
sudo install minikube-darwin-amd64 /usr/local/bin/minikube
```
#### Start Cluster
```shell
minikube start --driver=docker
```
#### Basic kubectl commands
* Create Deployment: `kubectl create deployment [deployment name]`
* Edit Deployment: `kubectl edit deployment [deployment name]`
* Delete Deployment: `kubectl delete deployment [deployment name]`
* Status of K8s components: `kubectl get all / nodes / pod / services / replicaset / deployment`
* Status of details pod: `kubectl get pod -o wide`
* Service Details: `kubectl describe service [service name]`
* Log to console: `kubectl logs [pod name]`
* Get interactive terminal: `kubectl exec -it [pod name] -- bin/bash`
```shell
kubectl create deployment nginx-deployment --image=nginx:1.25.2-alpine
kubectl edit deployment nginx-deployment
kubectl delete deployment nginx-deployment
kubectl apply -f k8s/nginx-deployment.yaml
kubectl apply -f k8s/nginx-service.yaml
kubectl delete -f k8s/nginx-deployment.yaml
kubectl delete -f k8s/nginx-service.yaml
```