Administration
Context
docker context lsShow created contexts (list of Docker sockets).
docker context use myRemoteContextSwitch to a Docker context.
docker context create myRemoteContext --docker "host=ssh://my.remote.docker.ip"Create a remote context (connect via ssh to execute Docker commands).
Monitoring
docker psShow running containers.
docker ps -aShow all containers.
docker statsStats about running containers.
docker volume lsShow volumes.
Images
Basis
docker imagesShow images locally available.
docker pull registryImagePull image from an image registry.
docker pull registryImage:imageTagPull specific tag of an image an image registry.
docker rmi myImageRemove the local image.
Layers
docker image history myImageShow image layers.
docker image history myImage --no-trunc --format json | tac | jq -r '.CreatedBy'Show image layers commands.
Create
docker build path/to/Dockerfile -t myUser/myImageName:myVersionCreate an image from a Dockerfile.
docker diff myContainerShow differences between an image and a container created from this image.
docker commit myContainer myNewImageSave the current state of a container into a new image.
docker save myImage > /path/myImage.tarExport an image (archive).
docker load -i /path/myImage.tarImport an image (form archive).
Containers
Create
docker create myImageCreate a new container.
docker run myImage myCommandRun a command in new container.
Manage
docker rename myContainer myContainerNewNameRename container.
docker start myContainerStart the container.
docker pause myContainerPause the container.
docker stop myContainerStop the container.
docker rm myContainerRemove the container.
Interact
docker exec myCommand myContainerRun the command in a running container.
docker exec -it /bin/bash myContainerRun Bash in interactive mode in a running container.
docker cp myFile.txt myContainer:/path/to/destinationCopy a file from host to the container.
docker cp myContainer:/path/to/source myFile.txtCopy a file form the container to the host.
Flags
--name myContainerGive a name to the container.
-dDetached Mode (run in background).
-itKeep stdin open (to use it like a shell).
--rmRemove the container when it is stopped.
-p 1212:1212Map ports (host:container).
-v /myHostFolder:/myContainerFolderMap a folder between the host and the container.
--read-onlyMakes the container's root file system read-only.
--security-opt=no-new-privilegesPrevents processes inside the container from gaining new privileges during execution (security measure against privilege escalation).
--cap-drop=ALLStart the container without capabilities.
--cap-add=SYS_NICECapability for scheduling (required for GPU scheduling?).
--cpus=2Set CPU limit.
--gpu allSet GPU limits (syntax allows for more detailed control than CPU).
--memory=4gSet RAM limit.
--memory-swap=4gSet Swap limit.
Tips
Access Container File System
CONTAINER_PID=$(docker inspect -f '{{.State.Pid}}' myContainer)Get container's PID.
nsenter --target $CONTAINER_PID --mount ls /Run ls in the container's file system.