Here’s a Docker cheat sheet that covers the most commonly used Docker commands, organized by categories.
Docker Basics
docker --version: Show the Docker version installed on your system.docker info: Display system-wide information, including Docker version, number of containers, and images.docker help: Get help on Docker commands.
Docker Images
docker images: List all Docker images on your system.docker pull <image>: Download an image from a Docker registry (e.g., Docker Hub).docker build -t <image_name> .: Build an image from a Dockerfile in the current directory and tag it with a name.docker tag <image_id> <new_image_name>: Tag an image with a new name.docker rmi <image>: Remove one or more images.docker history <image>: Show the history of an image (layers).
Docker Containers
docker ps: List all running containers.docker ps -a: List all containers (running and stopped).docker run <image>: Run a container from an image.docker run -d <image>: Run a container in detached mode (in the background).docker run -it <image>: Run a container in interactive mode with a terminal.docker run -p <host_port>:<container_port> <image>: Map a port from the host to the container.docker stop <container>: Stop a running container.docker start <container>: Start a stopped container.docker restart <container>: Restart a running container.docker rm <container>: Remove a stopped container.docker logs <container>: View the logs of a container.docker exec -it <container> <command>: Execute a command inside a running container (e.g.,bashto open a shell).
Docker Networks
docker network ls: List all Docker networks.docker network create <network_name>: Create a new Docker network.docker network inspect <network_name>: View detailed information about a network.docker network connect <network_name> <container>: Connect a container to a network.docker network disconnect <network_name> <container>: Disconnect a container from a network.docker network rm <network_name>: Remove a Docker network.
Docker Volumes
docker volume ls: List all Docker volumes.docker volume create <volume_name>: Create a new Docker volume.docker volume inspect <volume_name>: View detailed information about a volume.docker volume rm <volume_name>: Remove a Docker volume.docker run -v <volume_name>:<container_path> <image>: Mount a volume inside a container.
Docker Compose
docker-compose up: Start the services defined in adocker-compose.ymlfile.docker-compose down: Stop and remove containers, networks, volumes, and images created bydocker-compose up.docker-compose build: Build or rebuild services defined in adocker-compose.ymlfile.docker-compose ps: List containers managed by Docker Compose.docker-compose logs: View logs for services managed by Docker Compose.docker-compose exec <service> <command>: Execute a command in a running service.
Dockerfile Directives
FROM: Specifies the base image.WORKDIR: Sets the working directory inside the container.COPY: Copies files from the host to the container.RUN: Executes a command in the container.CMD: Specifies the command to run when the container starts.EXPOSE: Specifies the port on which the container will listen.ENV: Sets environment variables.ENTRYPOINT: Configures the container to run as an executable.
Docker Cleanup Commands
docker system prune: Remove unused data (stopped containers, unused networks, dangling images, etc.).docker container prune: Remove all stopped containers.docker image prune: Remove unused images.docker volume prune: Remove all unused volumes.docker network prune: Remove all unused networks.
Miscellaneous
docker inspect <container_or_image>: Return low-level information on Docker objects (containers, images, volumes, etc.).docker stats: Display a live stream of container(s) resource usage statistics.docker top <container>: Display the running processes of a container.docker cp <container>:<path> <local_path>: Copy files from a container to the host or vice versa.