Docker CheatSheet
CheatSheet

Install Docker on Debian
https://docs.docker.com/engine/install/
Install using the apt repository
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get updateAnd last step
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-pluginTo start the docker daemon:
docker -dTo build a docker image:
docker build -t <image-tag-name> <path-of-Dockerfile>To start a container with an interactive shell:
docker run -ti <image-name> /bin/bashTo run a docker container in the background:
docker run -d <image-name>To "shell" into a running container (docker-1.3+):
docker exec -ti <container-name> bashTo inspect a running container:
docker inspect <container-name> (or <container-id>)To get the process ID for a container:
docker inspect --format {{.State.Pid}} <container-name-or-id>To list (and pretty-print) the current mounted volumes for a container:
docker inspect --format='{{json .Volumes}}' <container-id> | python -mjson.toolTo copy files/folders between a container and your host:
docker cp foo.txt mycontainer:/foo.txtTo list currently running containers:
docker psTo list all containers:
docker ps -aTo remove all stopped containers:
docker container pruneTo remove all stopped containers:
docker rm $(docker ps -qa)To list all images:
docker imagesTo only see all images id:
docker image ls -qTo remove all untagged images:
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')To remove all volumes not used by at least one container:
docker volume pruneTo save image as tar archive:
docker save -o <archive-name>.tar <image-name>To restore image from a saved tar archive:
docker load -i <archive-name>.tarTo remove an image:
docker image rm <image-name-or-id>To tag an image:
docker image tag <image-name>:<tag-name> <image-name>:<new-tag-name>To login into hub.docker.com:
docker loginTo push a docker image into dockerhub repository:
docker push <image-name>:<image-tag-name>List all networks daemon knows about:
docker network lsCreate a specific network:
docker network create "<network_name>"Connect a specific container to a network:
docker network connect "<network_id|name>" "<container_id|name>"Disconnect a specific container from network:
docker network disconnect "<network_id|name>" "<container_id|name>"To see the logs of a background or stopped container:
docker logs <container-id>To publish a port of container on localhost:
docker run -p <localhost-port>:<container-port> <image-name>To create a docker volume:
docker volume create <volume-name>To see information of a docker volume:
docker volume inspect <volume-name>To use a volume in the container:
docker run -v <volume-name>:<folder-path-in-container> <image>To link current folder between host and container for development:
docker run <image-name> -v $(pwd):<folder-path-in-container> <image>To copy a file from the running container to host machine:
docker cp <container-id>:<path/to/file> <host/copy/path>To copy a file from host machine to the running container:
docker cp <host/copy/path> <container-id>:<path/to/file>Docker CLI Overview
Manage Docker containers and images efficiently using the following commands. For more detailed documentation on specific subcommands, such as docker run, refer to the official Docker CLI documentation.
List All Docker Containers
docker ps --allLists all Docker containers, both running and stopped.
Start a Container from an Image
docker run --name <container_name> <image>Starts a new container from the specified image with a custom name.
Start or Stop an Existing Container
docker start|stop <container_name>Starts or stops the specified container.
Pull an Image from a Docker Registry
docker pull <image>Downloads an image from a Docker registry to your local system.
Display the List of Downloaded Images
docker imagesShows all images available locally.
Open a Shell Inside a Running Container
docker exec -it <container_name> shAccesses an interactive shell in the specified running container.
Remove a Stopped Container
docker rm <container_name>Deletes the specified stopped container.
Fetch and Follow the Logs of a Container
docker logs -f <container_name>Displays the logs of the specified container and follows them in real-time.
Last updated on