Post

Docker Commands CMDsheet

Build Commands

docker buildBuilds an image from a Dockerfile in the current directory
docker build <remote-url>Builds an image from a remote GIT repository
docker build -t imagename/tagBuilds and tags an image for easier tracking
Builds an image via a Dockerfile that is passed through STDIN
docker build -t image:1.0 -<<EOFFROM busyboxRUN echo “hello world”EOF

Run Commands

docker run (options) image (command) (arg...)

--detach , -dRuns a container in the background and prints the container ID
--env , -eSets environment variables
--hostname , -hSets a hostname to a container
--label , -lCreates a meta data label for a container
--nameAssigns a name to a container
--networkConnects a container to a network
--rmRemoves container when it stops
--read-onlySets the container filesystem as read-only
--workdir , -wSets a working directory in a container

Copy Command

docker cp :/file/path/within/container /host/path/to/copy/files/toCopy a file from container to localhost
docker cp /host/path/to/copy/files/from :/file/path/within/containerCopy a file from localhost to container

Clean Up Commands

docker image pruneClears an unused image
docker image prune -aClears all images that are not being used by containers
docker system pruneRemoves all stopped containers, all networks not used by containers, all dangling images, and all build cache
docker image rm imageRemoves an image
docker rm containerRemoves a running container
docker swarm leaveLeaves a swarm
docker stack rm stacknameRemoves a swarm
docker volume rm $(docker volume ls -f dangling=true -q)Removes all dangling volumes
docker rm $(docker ps -a -q)Removes all stopped containers
docker kill $(docker ps -q)Stops all running containers

Container Interaction Commands

docker start containerStarts a new container
docker stop containerStops a container
docker pause containerPauses a container
docker unpause containerUnpauses a container
docker restart containerRestarts a container
docker wait containerBlocks a container
docker export containerExports container contents to a tar archive
docker attach containerAttaches to a running container
docker wait containerWaits until the container is terminated and shows the exit code
docker commit -m "commit message" -a "author" container username/image_name:tagSaves a running container as an image
docker logs -ft containerFollows container logs
docker exec -ti container script.shRuns a command in a container
docker commit container imageCreates a new image from a container
docker create imageCreates a new container from an image

Container Inspection Commands

docker -ps -aLists all containers
docker diff containerInspects changes to directories and files in the container filesystem
docker top containerShows all running processes in an existing container
docker inspect containerDisplays low-level information about a container
docker logs containerGathers the logs for a container
docker stats containerShows container resource usage statistics

Manage Images Commands

docker image lsLists images
docker image rm mysqlRemoves an image
docker tag image tagTags an image
docker history imageDisplays the image history
docker inspect imageDisplays low-level information about an image

Registry Commands

docker loginLogs in to a registry
docker logoutLogs out from a registry
docker pull mysqlPulls an image from a registry
docker push repo/rhel-httpd:latestPushes an image to a registry
docker search termSearches Docker Hub for images with the specified term

Service Commands

docker service lsLists all services running in a swarm
docker stack services stacknameLists all running services
docker service ps servicenameLists the tasks of a service
docker service update servicenameUpdates a service
docker service create imageCreates a new service
docker service scale servicename=10Scales one or more replicated services
docker service logs stackname servicenameLists all service logs

Network Commands

docker network create networknameCreates a new network
docker network rm networknameRemoves a specified network
docker network lsLists all networks
docker network connect networkname containerConnects a container to a network
docker network disconnect networkname containerDisconnects a container from a network
docker network inspect networknameDisplays detailed information about a network
This post is licensed under CC BY 4.0 by the author.