A Docker container cheat sheet provides a quick reference for common commands used to manage Docker containers.
Container Lifecycle Management:
docker run [image_name]: Creates and starts a new container from an image.
docker start [container_name/id]: Starts one or more stopped containers.docker stop [container_name/id]: Stops one or more running containers.docker restart [container_name/id]: Stops and then starts a container.docker pause [container_name/id]: Pauses all processes within a running container.docker unpause [container_name/id]: Unpauses a paused container.docker rm [container_name/id]: Removes one or more containers (use -f to force removal of running containers).
Container Information and Interaction:
docker ps: Lists all running containers.
docker ps -a: Lists all containers, including stopped ones.
docker logs [container_name/id]: Fetches and follows the logs of a container. Use -f to follow logs in real-time and --tail [number] to specify the number of lines from the end.docker exec -it [container_name/id] [command]: Executes a command inside a running container, often used to open an interactive shell (e.g., /bin/bash or sh).docker inspect [container_name/id]: Displays detailed information about a container.docker stats: Shows live resource usage statistics for running containers.docker cp [source_path] [destination_path]: Copies files/folders between a host and a container (e.g., docker cp ./local/file.txt container:/app/ or docker cp container:/app/file.txt ./).Container Configuration Flags (used with docker run):
-d: Runs the container in detached mode (in the background).
-p [host_port]:[container_port]: Publishes a container's port to the host.-v [host_path]:[container_path]: Mounts a host path as a volume inside the container.--name [custom_name]: Assigns a custom name to the container.--rm: Automatically removes the container when it exits.-it: Allocates a pseudo-TTY and keeps STDIN open, enabling interactive terminal use.-e [VAR_NAME]=[value]: Sets an environment variable inside the container.--network [network_name]: Connects the container to a specific Docker network.
A Docker images cheat sheet provides a quick reference for common commands used to manage Docker images.
Listing Images:
To list all local Docker images.
Шифра
docker images
To list images with their digests.
Шифра
docker images --digests
Шифра
docker images --filter "reference=ubuntu:latest"
Pulling and Pushing Images:
To download an image from a registry (e.g., Docker Hub):
Шифра
docker pull <image-name>:<tag>
Шифра
docker push <image-name>:<tag>
Building Images:
To build an image from a Dockerfile in the current directory:
Шифра
docker build -t <image-name>:<tag> .
Шифра
docker build -t <image-name>:<tag> <repository-url>
Removing Images:
To remove a specific image.
Шифра
docker rmi <image-id-or-name>
To remove all unused images.
Шифра
docker image prune
Шифра
docker image prune -a
Inspecting Images:
To display detailed information about an image:
Шифра
docker inspect <image-id-or-name>