Skip to content

dennyzhang/cheatsheet-docker-A4

Repository files navigation

1 Docker CheatSheet

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

1.1 Docker Trouble Shooting

NameSummary
Docker push: manifest invalidRe-push a new version of the same docker tag may fail, due to permission
Docker pull: missing signature keyDocker push again to resolve the issue
Docker cp: Error response from daemon: not a directorycontainer folder is in a symbol link
Find process id by container namedocker top $container_id, or docker top $container_name
List resource usage by containersdocker stats
Get dockerd storage driverdocker info, then check Storage Driver
docker-containerd-shimThe Docker four components: Docker engine, containerd, containerd-shm and runC

1.2 Docker Basic

NameSummary
Install docker on Ubuntuapt-get install docker.io
Install docker on CentOSUse docker repo https://download.docker.com/linux/centos/docker-ce.repo
Install docker in Debian 10Link: How To Install and Use Docker on Debian 10
Install old docker versionGitHub: install-old-docker.md

1.3 Docker start service

NameSummary
Start a ubuntu test envdocker run ubuntu:16.04 /bin/echo hello world
Start a ubuntu 18.04 test envdocker run ubuntu:18.04 /bin/echo hello world
Start a container run and stopdocker run --rm ubuntu:18.04 /bin/echo hello world
Start a debian9 test envdocker run debian:9 /bin/echo hello world
Start a centos test envdocker run centos:centos6 /bin/echo hello world
Start a jenkins serverdocker run -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts
Start a nginx serverdocker run -t -d -p 8080:80 --name nginx-test nginx
Start a mysql serverdocker run -e MYSQL_ROOT_PASSWORD=password123 -e MYSQL_DATABASE=wordpress -d mysql:5.7
Start a nexus serverdocker run -d -p 8082:8081 --name nexus -v /data/nexus-data:/nexus-data sonatype/docker-nexus3
Start a sshd serverdocker run -t -d --privileged -p 5022:22 denny/sshd:latest /usr/sbin/sshd -D
Start a ftp serverdocker run -t -d -p 21:21 -p 20:20 -e USERNAME=${username} -e PASSWORD=${password} denny/proftproftpd:v1

1.4 Container Runtime

NameSummary
dockerd
containerd
cri-oFrom Redhat
rkta pod-native container engine for Linux from CoreOS. Stopped maintainance now
Amazon ACSsupports DC/OS, Swarm, Kubernetes
CoreOS Fleet
Cloud Foundry DiegoNot actively maintained any more
ReferenceCheatSheet: Docker, CheatSheet: CRI-O, CheatSheet: rkt, CheatSheet: containerd

1.5 Container Basic

NameSummary
Start docker containerdocker run -p 4000:80 imgname
Start docker container in detached modedocker run -d -p 4000:80 imgname
Start container with entrypoint changeddocker run -t -d --entrypoint=/bin/sh "$docker_image"
Enter a running containerdocker exec -it <container-id> sh
Upload local file to container filesystemdocker cp /tmp/foo.txt mycontainer:/foo.txt
Download container file local filesystemdocker cp mycontainer:/foo.txt /tmp/foo.txt
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.6 Docker Cleanup

NameSummary
Remove unused docker imagesdelete-unused-images.sh
Delete all containersdelete-all-containers.sh
Remove exited containersdocker rm $(docker ps --filter status=exited -qa)
Docker prune imagesdocker image prune -f
Docker prune volumesdocker volume prune -f
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 Dockerfile

NameSummary
Change entrypoint to run nothingentrypoint: ["tail", "-f", "/dev/null"]
Set timezone in DockerfileRUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
Define multiple line commandGitHub: Dockerfile-example-multiline

1.8 Docker Compose

NameSummary
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 GitHub: sample-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.9 Docker Containers

NameSummary
Start docker containerdocker run -p 4000:80 <imgname>
Start docker container in detached modedocker run -d -p 4000:80 imgname
Start docker container and remove when exitdocker run -rm -it <imgname> sh
Enter a running containerdocker exec -it [container-id] sh
Stop containerdocker stop <hash>
List all containersdocker ps, docker ps -a
Remove containerdocker rm <hash>, docker rm $(docker ps -a -q)
Force shutdown of one given containerdocker kill <hash>
Login to docker hubdocker login
Run image from a given tagdocker run username/repo:tag
Tail container logsdocker logs --tail 5 $container_name
Check container healthcheck statusdocker inspect --format '{{.State.Health}}' $container_name
List containers by labelsdocker ps --filter "label=org.label-schema.group"

1.10 Docker Images

NameSummary
List all imagesdocker images, docker images -a
Create docker imagedocker build -t denny/image:<tag> .
Docker push a tagged image to repodocker push denny/image:<tag>
Show the history of an imagedocker history <image_name>
Export image to filedocker save <image_name> > my_img.tar
Load image to local registrydocker load -i my_img.tar
Tag <image>docker tag <image> username/repo:tag

1.11 Docker Socket file

NameSummary
Run container mounting socket filedocker run -v /var/run/docker.sock:/var/run/docker.sock -it alpine sh
A different docker socket fileexport DOCKER_HOST=unix:///my/docker.sock
List containerscurl -XGET --unix-socket /var/run/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
LinksLink: Develop with Docker Engine SDKs and API

1.12 Docker Conf

NameSummary
Docker files/var/lib/docker, /var/lib/docker/devicemapper/mnt
Docker for Mac~/Library/Containers/com.docker.docker/Data/

1.13 Ubuntu docker: Install missing packages

NameSummary
Pull ubuntu docker imagedocker pull ubuntu
man: command not foundapt-get update, apt-get install man
ping: command not foundapt-get update, apt-get install iputils-ping
dig: command not foundapt-get install dnsutils

1.14 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 labelsdocker ps --filter "label=org.label-schema.group"
List all imagesdocker images -a

1.15 Resource Reference

NameSummary
Docker SDKhttps://docs.docker.com/develop/sdk/examples/
Docker REST APIhttps://docs.docker.com/engine/api/v1.27/#tag/Container
Docker Hub auto buildhttps://docs.docker.com/docker-hub/builds/#build-statuses-explained

1.16 More Resources

License: Code is licensed under MIT License.

linkedin github slack

2 TODO docker ARG vs ENV

https://github.com/JCMais/docker-linux-ci/blob/master/debian.dockerfile#L15-L16