Complete Docker Command Cheatsheet
-
docker --version
- Example:
docker --version
- Description: Displays the installed Docker version.
-
docker info
- Example:
docker info
- Description: Provides detailed information about Docker's configuration and system details.
Image Management Commands
-
docker images
- Example:
docker images
- Description: Lists all Docker images stored on the local machine.
-
docker pull <image_name>
- Example:
docker pull nginx
- Description: Downloads a Docker image from Docker Hub or another registry.
-
docker build -t <image_name> .
- Example:
docker build -t my_image .
- Description: Builds an image from a Dockerfile in the current directory.
-
docker tag <image_id> <repository:tag>
- Example:
docker tag 123abc myrepo/myimage:latest
- Description: Tags an image for versioning or pushing to a registry.
-
docker rmi <image_id>
- Example:
docker rmi 123456abc
- Description: Removes a specified image by its ID.
Container Management Commands
-
docker ps
- Example:
docker ps
- Description: Lists all running containers.
-
docker ps -a
- Example:
docker ps -a
- Description: Lists all containers, including stopped ones.
-
docker run <image_name>
- Example:
docker run ubuntu
- Description: Runs a container from a specified image.
-
docker run -d <image_name>
- Example:
docker run -d nginx
- Description: Runs a container in detached mode (background).
-
docker run -it <image_name>
- Example:
docker run -it ubuntu
- Description: Starts a container in interactive mode, allowing the user to interact with the terminal directly. Useful for debugging or exploring an environment.
-
docker run --rm <image_name>
- Example:
docker run --rm ubuntu
- Description: Automatically removes the container once it stops, useful for temporary tasks to avoid leaving stopped containers.
-
docker run -P <image_name>
- Example:
docker run -P nginx
- Description: Automatically maps all exposed ports of the container to random host ports.
-
docker run -p <host_port>:<container_port> <image_name>
- Example:
docker run -p 8080:80 nginx
- Description: Maps a port from the host to a port in the container.
-
docker run --name <container_name> <image_name>
- Example:
docker run --name my_nginx nginx
- Description: Assigns a custom name to a container, making it easier to reference it in future commands.
-
docker run -e <ENV_VAR>=<value> <image_name>
- Example:
docker run -e MY_VAR=myvalue ubuntu
- Description: Sets environment variables for the container, which can be accessed by processes within the container.
-
docker run -v <host_path>:<container_path> <image_name>
- Example:
docker run -v /mydata:/data ubuntu
- Description: Mounts a host directory or file into the container. Useful for data persistence or sharing data between the host and container.
-
docker run --network <network_name> <image_name>
- Example:
docker run --network my_network ubuntu
- Description: Connects a container to a specified network, enabling better communication between services.
-
docker run -p <host_port>:<container_port> -e ENV_VAR=value --name container_name <image_name>
- Example:
docker run -p 5000:80 -e MY_VAR=value --name my_app app_image
- Description: Demonstrates combining options to expose ports, set environment variables, and name the container. Useful for detailed configuration of container startup.
-
docker run --cap-add <capability> --cap-drop <capability> <image_name>
- Example:
docker run --cap-add NET_ADMIN --cap-drop MKNOD ubuntu
- Description: Grants or restricts Linux capabilities within the container. For instance, `NET_ADMIN` gives network administration privileges to the container, while `MKNOD` is removed.
-
docker run --cpus <num_cpus> <image_name>
- Example:
docker run --cpus=2 nginx
- Description: Limits the container to use a specified number of CPU cores, controlling resource usage for more efficient system management.
-
docker stop <container_id>
- Example:
docker stop abc123
- Description: Stops a running container by its ID.
-
docker start <container_id>
- Example:
docker start abc123
- Description: Starts a stopped container.
-
docker restart <container_id>
- Example:
docker restart abc123
- Description: Restarts a running or stopped container.
-
docker rm <container_id>
- Example:
docker rm abc123
- Description: Removes a stopped container.
-
docker exec -it <container_id> /bin/bash
- Example:
docker exec -it abc123 /bin/bash
- Description: Opens a shell session inside a running container.
Docker Logs and Inspection Commands
-
docker logs <container_id>
- Example:
docker logs abc123
- Description: Retrieves the logs for a specific container.
-
docker inspect <container_id_or_image_name>
- Example:
docker inspect nginx
- Description: Provides detailed metadata about a container or image.
Network Management Commands
-
docker network ls
- Example:
docker network ls
- Description: Lists all Docker networks.
-
docker network create <network_name>
- Example:
docker network create my_network
- Description: Creates a custom network.
-
docker network connect <network_name> <container_name>
- Example:
docker network connect my_network service2
- Description: Connects an existing container to an additional network, which is useful for cross-network communication between services.
-
docker network disconnect <network_name> <container_name>
- Example:
docker network disconnect my_network service2
- Description: Disconnects a container from a specific network, isolating it from other containers on that network.
Docker Compose Commands
-
docker-compose up
- Example:
docker-compose up
- Description: Starts services defined in a
docker-compose.yml
file.
-
docker-compose down
- Example:
docker-compose down
- Description: Stops and removes containers, networks, and volumes created by
docker-compose up
.
-
docker-compose exec <service_name> <command>
- Example:
docker-compose exec service1 curl http://service2:port
- Description: Executes a command in a running service created with Docker Compose, useful for checking network connectivity between services.
Volume Creation and Management Commands
-
docker volume ls
- Example:
docker volume ls
- Description: Lists all Docker volumes on the system, showing their names and details.
-
docker volume create <volume_name>
- Example:
docker volume create my_volume
- Description: Creates a named Docker volume for persistent storage, which can be shared across multiple containers.
-
docker volume inspect <volume_name>
- Example:
docker volume inspect my_volume
- Description: Provides detailed information about a specific volume, including its mountpoint on the host system and metadata.
-
docker volume rm <volume_name>
- Example:
docker volume rm my_volume
- Description: Removes a specific Docker volume. This is irreversible and should be used with caution as it deletes all data stored in the volume.
-
docker volume prune
- Example:
docker volume prune
- Description: Removes all unused Docker volumes, freeing up storage space. This only affects volumes not used by any containers.
Using Volumes in Containers
-
docker run -v <volume_name>:<container_path> <image_name>
- Example:
docker run -v my_volume:/data nginx
- Description: Mounts a named volume to a specific path in the container. Data written to
/data
in the container is stored in my_volume
on the host.
-
docker run -v <host_path>:<container_path> <image_name>
- Example:
docker run -v /mydata:/data nginx
- Description: Binds a directory or file on the host to a path in the container, useful for sharing data between the host and container.
-
docker run --mount type=volume,source=<volume_name>,target=<container_path> <image_name>
- Example:
docker run --mount type=volume,source=my_volume,target=/app_data nginx
- Description: An alternative method to
-v
for mounting volumes with more explicit options. The `--mount` option provides a clear structure and is helpful in more complex configurations.
Persistent Data Management Commands
-
docker commit <container_id> <new_image_name>
- Example:
docker commit abc123 my_image:latest
- Description: Captures the current state of a container's filesystem and saves it as a new image, which includes all changes made since the container was started.
-
docker export -o <file_name.tar> <container_id>
- Example:
docker export -o my_container_data.tar abc123
- Description: Exports a container’s filesystem to a tar archive, which can then be saved, shared, or re-imported. Only captures data within the container at the time of export.
-
docker import <file_name.tar>
- Example:
docker import my_container_data.tar
- Description: Imports a tar archive as a new Docker image, creating a new container environment based on previously exported filesystem data.
Cleaning Up Docker Storage
-
docker system df
- Example:
docker system df
- Description: Displays Docker disk usage by images, containers, and volumes, helping identify which parts of Docker storage are consuming the most space.
-
docker system prune
- Example:
docker system prune
- Description: Removes all unused containers, networks, and images to free up storage space. By adding
--volumes
, it also removes all unused volumes.
-
docker system prune --all --volumes
- Example:
docker system prune --all --volumes
- Description: Performs a full cleanup of unused images, stopped containers, unused networks, and volumes, maximizing storage reclamation.
Advanced Commands
-
docker push <repository:tag>
- Example:
docker push myrepo/myimage:latest
- Description: Pushes a Docker image to a remote registry.
-
docker stats
- Example:
docker stats
- Description: Displays real-time statistics of running containers, including CPU, memory, and network usage.
-
docker save -o <filename.tar> <image_name>
- Example:
docker save -o my_image.tar my_image
- Description: Saves an image to a tar archive file.
-
docker load -i <filename.tar>
- Example:
docker load -i my_image.tar
- Description: Loads an image from a tar archive.
-
docker import <filename.tar>
- Example:
docker import my_container.tar
- Description: Imports a tar file as a Docker image.
-
docker checkpoint create <container_id> <checkpoint_name>
- Example:
docker checkpoint create abc123 checkpoint1
- Description: Creates a checkpoint for a container’s current state.