Docker Quick Reference
Contents
1. Docker concepts
Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. The use of Linux containers to deploy applications is called containerization.
2. Containers and virtual machines
A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.
3. Commands - Run and Debug
docker run -d -v redis-vol:/data redis:alpine -p
run containersdocker run -d -v redis-vol:/data redis
docker run --entrypoint /bin/bash -it nodeais:2.4.4
docker run -it -v /root:/root nodeais:2.4.3 bash
docker exec -i 92a1 curl 127.0.0.1:9200
debug go into shell without interrupt the main container, recommendationdocker exec -it 92a1 bash
docker attach 69d1
attach will stop main process and return control heredocker logs --tail=100 -f mongodb
to get debug logs
4. Commands - Image
docker pull library/elasticsearch:5.6
to pull esdocker pull java:openjdk-8-jdk
to pull javadocker images
list imagesdocker image ls
docker image ls mongo --format "{{.ID}}"
docker image rm *id*
docker build -t plugins-scripts:v23 .
build image
5. Commands - Container
docker container ls
docker container ls -a
to include stooped onesdocker container rm *id*
docker container prune
remove all stopped containersdocker ps
is a short cut fordocker container ls
6. Commands - Volumes
docker volume ls
docker volume create redis-vol
docker run -d --name devtest --mount source=myvol2,target=/app nginx:latest
docker run -d --name devtest -v myvol2:/app nginx:latest
docker volume inspect my-vol
docker volume rm my-vol
docker run -d --name=nginxtest --mount source=nginx-vol,destination=/usr/share/nginx/html,readonly nginx:latest
for read onlydocker run -d --name=nginxtest -v nginx-vol:/usr/share/nginx/html:ro nginx:latest
for read onlydocker volume prune
delete all unused
7. Commands - Network
docker network create my-net
bridgedocker network create --driver=bridge --subnet=172.28.0.0/16 --ip-range=172.28.5.0/24 --gateway=172.28.5.254 br0
docker network create -d macvlan --subnet=172.16.86.0/24 --gateway=172.16.86.1 -o parent=eth0 pub_net
macvlandocker network rm my-net
Remove networkdocker create --name my-nginx --network my-net --publish 8080:80 nginx:latest
Connect by startingdocker network connect my-net my-nginx
Connect while starteddocker network disconnect my-net my-nginx
Disconnectdocker create --name my-nginx --network host --publish 8080:80 nginx:latest
docker network prune
all unused networks are removed
8. Commands - Format
println
print each linedocker inspect --format='{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}' container
docker inspect -f '{{range .NetworkSettings.Networks}}{{println .IPAddress}}{{end}}'
get IPupper
:docker inspect --format "{{upper .Name}}" container
title
:docker inspect --format "{{title .Name}}" container
split
:docker inspect --format '{{split .Image ":"}}'
lower
:docker inspect --format "{{lower .Name}}" container
json
:docker inspect --format '{{json .Mounts}}' container
join
:docker inspect --format '{{join .Args " , "}}' container
Sample:
docker inspect 7cb9995533cb | grep "IPAddress" .Service.ID Service ID .Service.Name Service name .Service.Labels Service labels .Node.ID Node ID .Node.Hostname Node Hostname .Task.ID Task ID .Task.Name Task name .Task.Slot Task slot
9. Commands Tags, Save and Export
(Seems modern docker can remember the tag)
docker image save b5435fede523 -o ./plugins-scrcips.2.3.0.180428.tar
docker image load -i ./plugins-scrcips.2.3.0.180428.tar
docker image tag b5435fede523 plugins-scripts:2.3.0
docker image rmi plugins-scripts:2.3.0
10. Commands Others
docker system prune
remove all images, containers, and networks.docker run --rm -it --security-opt apparmor=docker-default hello-world
security policydocker run -v /root:/root -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -it node:6.14.0 /bin/bash
Display (For LibreOffice/Chrome/FF)LABEL "com.example.vendor"="ACME Incorporated"
to add labelsbin/registry garbage-collect [--dry-run] /path/to/config.yml
garbage collection
Comments !