Using CheatSheets To Apply Best Practices

Docker CheatSheet

Docker CheatSheet

1.1 Docker start service

Name Summary
Start a ubuntu test env docker run ubuntu:16.04 /bin/echo hello world
Start a centos test env docker run centos:centos6 /bin/echo hello world
Start nginx docker run -t -d -p 8080:80 --name nginx-test nginx
Start a sshd server docker run -t -d --privileged -p 5022:22 denny/sshd:latest /usr/sbin/sshd -D
Start a jenkins server docker run -p 8080:8080 denny/jenkins:1.0
Start a ftp server docker run -t -d -p 21:21 -p 20:20 -e USERNAME=${username} -e PASSWORD=${password} denny/proftproftpd:v1

1.2 Docker Check Status

Name Summary
Tail container logs docker logs --tail 5 $container_name
Check container healthcheck status docker inspect --format '{{.State.Health}}' $container_name
List all containers docker ps, docker ps -a
List containers by labels docker ps --filter ""
List all images docker images, docker images -a

1.3 Docker Cleanup

Name Summary
Remove the specified image docker rmi <imagename>
Remove all docker images docker rmi $(docker images -q)
Remove orphaned docker volumes docker volume rm $(docker volume ls -qf dangling=true)
Remove dead containers docker rm $(docker ps --filter status=dead -qa)

1.4 Container Basic

Name Summary
Start docker container docker run -p 4000:80 imgname
Start docker container in detached mode docker run -d -p 4000:80 imgname
Enter a running container docker exec -it [container-id] sh
Stop container docker stop <hash>
Remove container docker rm <hash>
Remove all containers docker rm $(docker ps -a -q)
Force shutdown of one given container docker kill <hash>
Login to docker hub docker login
Tag <image> docker tag <image> username/repo:tag
Docker push a tagged image to repo docker push username/repo:tag
Run image from a given tag docker run username/repo:tag
Create docker image docker build -t denny/image:test .
Docker files /var/lib/docker, /var/lib/docker/devicemapper/mnt

1.5 Docker Compose

Name Summary
Change entrypoint to run nothing entrypoint: ["tail", "-f", "/dev/null"]
Change restart policy restart: always, Link: Compose file version 3 reference
Mount file as volume $PWD/httpd/httpd.conf:/usr/local/apache2/conf/httpd.conf:ro sample-mount-file.yml
Start compose env docker-compose up, docker-compose up -d
Stop compose env docker-compose down, docker-compose down -v
Check logs docker-compose logs

1.6 Ubuntu docker: Install missing packages

Name Summary
Pull ubuntu docker image docker pull ubuntu
man: command not found apt-get update, apt-get install man
ping: command not found apt-get update, apt-get install iputils-ping
dig: command not found apt-get install dnsutils

1.7 Docker Socket file

Name Summary
Links Link: Develop with Docker Engine SDKs and API
Run container mounting socket file docker run -v /var/run/docker.sock:/var/run/docker.sock -ti alpine sh
A different docker socket file export DOCKER_HOST=unix:///my/docker.sock
List containers curl -XGET --unix-socket /var/run/docker.sock http://localhost/containers/json
Stop container curl -XPOST --unix-socket /var/run/docker.sock http://localhost/containers/<container_id>/stop
Start container curl -XPOST --unix-socket /var/run/docker.sock http://localhost/containers/<container_id>/start
List events curl --unix-socket /var/run/docker.sock http://localhost/events
Create container curl -XPOST --unix-socket /var/run/docker.sock -d '{"Image":"nginx:alpine"}' -H 'Content-Type: application/json' http://localhost/containers/create

1.8 Container Runtime

Name Summary
rkt a pod-native container engine for Linux.
CRI-O Open Container Initiative-based implementation of Kubernetes Container Runtime Interface

1.10 More Resources

License: Code is licensed under MIT License.


Leave a Reply

Your email address will not be published. Required fields are marked *