📖 Docker CheatSheets In A4
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
samples
.gitignore
README.org
cheatsheet-docker-A4.pdf
container-install-devkit.sh
delete-all-containers.sh

README.org

1 Docker CheatSheet

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

See more CheatSheets from Denny: #denny-cheatsheets

1.1 Docker Compose

NameSummary
Change entrypoint to run nothingentrypoint: ["tail", "-f", "/dev/null"]
Change restart policyrestart: always, Link: Compose file version 3 reference
Mount file as volume$PWD/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf:ro samples/mount-file.yml
Start compose envdocker-compose up, docker-compose up -d
Stop compose envdocker-compose down, docker-compose down -v
Check logsdocker-compose logs

1.2 Container Runtime

NameSummary
dockerd
rkta pod-native container engine for Linux.
CRI-OOpen Container Initiative-based implementation of Kubernetes Container Runtime Interface

1.3 Check Status

NameSummary
Tail container logsdocker logs --tail 5 $container_name
Check container healthcheck statusdocker inspect --format '{{.State.Health}}' $container_name
List containersdocker ps
List all containersdocker ps -a
List containers by labels=docker ps –filter “label=org.label-schema.group”=
List all imagesdocker images -a

1.4 Container Basic

NameSummary
Start docker containerdocker run -p 4000:80 imgname
Start docker container in detached modedocker run -d -p 4000:80 imgname
Enter a running containerdocker exec -it [container-id] sh
Stop containerdocker stop <hash>
Remove containerdocker rm <hash>
Remove all containersdocker rm $(docker ps -a -q)
Force shutdown of one given containerdocker kill <hash>
Login to docker hubdocker login
Tag <image>docker tag <image> username/repo:tag
Docker push a tagged image to repodocker push username/repo:tag
Run image from a given tagdocker run username/repo:tag
Create docker imagedocker build -t denny/image:test .

1.5 Docker Socket file

NameSummary
LinksLink: Develop with Docker Engine SDKs and API
Run container mounting socket filedocker run -v /var/run/docker.sock:/var/run/docker.sock -ti alpine sh
A different docker socket fileexport DOCKER_HOST=unix:///my/docker.sock
List containerscurl -XGET --unix-socket /myrun/docker.sock http://localhost/containers/json
Stop containercurl -XPOST --unix-socket /var/run/docker.sock http://localhost/containers/<container_id>/stop
Start containercurl -XPOST --unix-socket /var/run/docker.sock http://localhost/containers/<container_id>/start
List eventscurl --unix-socket /var/run/docker.sock http://localhost/events
Create containercurl -XPOST --unix-socket /var/run/docker.sock -d '{"Image":"nginx:alpine"}' -H 'Content-Type: application/json' http://localhost/containers/create

1.6 Cleanup

NameSummary
Remove the specified imagedocker rmi <imagename>
Remove all docker imagesdocker rmi $(docker images -q)
Remove orphaned docker volumesdocker volume rm $(docker volume ls -qf dangling=true)
Remove dead containersdocker rm $(docker ps --filter status=dead -qa)

1.7 Resource Reference

NameSummary
Docker SDKhttps://docs.docker.com/develop/sdk/examples/
Docker REST APIhttps://docs.docker.com/engine/api/v1.27/#tag/Container

1.8 Scripts

  • Docker build
cd "$dir" && cat "$dockerfile" | docker build -t "$docker_image" -f - .
  • Use google docker registry
docker build -t gcr.io/<MY_REPO>/<MY_IMAGE>:<MY_TAG> .
gcloud docker -- push gcr.io/<MY_REPO>/<MY_IMAGE>:<MY_TAG>
  • Delete all containers

delete-all-containers.sh

curl -L https://raw.githubusercontent.com/dennyzhang/cheatsheet-docker-A4/master/delete-all-containers.sh | bash
  • Install test kit

container-install-devkit.sh

apt-get -y update
apt-get install -y curl netcat

curl -L https://raw.githubusercontent.com/dennyzhang/cheatsheet-docker-A4/master/container-install-devkit.sh | bash
  • Clean up Disk

Remove All Useless Resources.

docker ps --filter status=exited -aq | xargs -r docker rm -v

Remove unused docker images

docker rmi $(docker images | grep "<none>"  | awk -F' ' '{print $3}')

Remove intermediate containers generated during docker build

docker ps -a | grep "/bin/sh -c" |  awk -F' ' '{print $1}' | xargs docker rm

Remove Image with <none> string

echo "Remove docker images with <none> string"
if docker images | grep none | tee; then
   docker rmi $(docker images | grep "<none>"  | awk -F' ' '{print $3}') | tee
fi

1.9 More Resources

License: Code is licensed under MIT License.

linkedin github slack