🐳 The Ultimate Docker Command Cheat Sheet for Developers

Table of contents

📌 Introduction
Docker has revolutionized how we build, ship, and run applications — making development faster, more consistent, and scalable. But with so many commands to remember, it’s easy to get lost.
This cheat sheet brings together the most essential Docker commands in one place, so you can work faster, avoid context switching, and focus on building great software.
Whether you're just starting or need a quick refresher — this guide has you covered. 🐳⚙️
🧱 Docker Installation & Setup
Windows: To install Docker in your windows machine you just need to download the Docker Desktop from the official website and follow the installation instructions.
You can download it from Docker Desktop for Windows.
for more information on how to install Docker in Windows, you can refer to the official documentation.Mac: To install Docker in your mac machine you just need to download the Docker Desktop from the official website and follow the installation instructions.
You can download it from Docker Desktop for Mac.
for more information on how to install Docker in Mac, you can refer to the official documentation.Linux: To install Docker in your Linux machine you just need to follow the instructions for your specific distribution. You can find the installation instructions for various Linux distributions in the official documentation.
To check if Docker is installed correctly, you can run the following command in your terminal:
docker --version
Now that you have Docker installed, let's dive into the commands that will help you manage your containers, images, and networks effectively.
📦 Working with Docker Images
List Images: View all Docker images on your system.
docker images
Pull Image: Download an image from Docker Hub.
docker pull <image_name>:<tag> # Example: docker pull nginx:latest
Here,
<image_name>
is the name of the image you want to pull, and<tag>
is the specific version or tag of the image. If no tag is specified, Docker defaults tolatest
.
- Build Image: Create a Docker image from a Dockerfile.
Here,docker build -t <image_name>:<tag> <path_to_dockerfile> # Example: docker build -t myapp:latest .
<image_name>
is the name you want to give to your image,<tag>
is the version tag (Optional), and<path_to_dockerfile>
is the directory containing your Dockerfile (often.
for the current directory),-t
is used to tag the image with a name and version. If you don't specify a tag, Docker will uselatest
by default and if you don't specify a path, Docker will look for a Dockerfile in the current directory.
- Build Image build without cache:
This command builds the image without using any cached layers, ensuring a fresh build.docker build --no-cache -t <image_name>:<tag> <path_to_dockerfile> # Example: docker build --no-cache -t myapp:latest .
Remove Image: Delete a Docker image.
docker rmi <image_name or image_id>
Here,
<image_name>
is the name of the image you want to remove, or you can use<image_id>
which is the unique identifier for the image. If the image is being used by any container, you will need to stop and remove those containers first.Tag Image: Add a tag to an existing image.
docker tag <existing_image_name>:<existing_tag> <new_image_name>:<new_tag> # Example: docker tag myapp:latest myapp:v1.0
This command creates a new tag for an existing image, allowing you to version your images easily.
🧰 Working with Containers
List Running Containers: View all currently running containers.
docker ps
List All Containers: View all containers, including stopped ones.
docker ps -a
Run Container: Start a new container from an image.
docker run -d --name <container_name> <image_name>:<tag> # Example: docker run -d --name myapp_container myapp:latest
Here,
-d
runs the container in detached mode (in the background),--name
assigns a name to the container,<image_name>
is the name of the image you want to run, and<tag>
is the specific version or tag of the image. If no tag is specified, Docker defaults tolatest
. If you want to run the container in the foreground, you can omit the-d
flag.Note: If the image is not present locally, Docker will automatically pull it from Docker Hub and then run the container.
Run Container with Port Mapping: Map a container port to a host port.
docker run -d -p <host_port>:<container_port> --name <container_name> <image_name>:<tag> # Example: docker run -d -p 8080:80 --name myapp_container myapp:latest
Here,
-p
maps the host port to the container port, allowing you to access the application running inside the container from your host machine.<host_port>
is the port on your host machine, and<container_port>
is the port inside the container where the application is running.Run Container with Volume Mounting: Mount a host directory as a volume in the container.
docker run -d -v <host_directory>:<container_directory> --name <container_name> <image_name>:<tag> # Example: docker run -d -v /path/to/host/dir:/path/in/container --name myapp_container myapp:latest
Here,
-v
mounts a directory from your host machine into the container, allowing you to persist data or share files between the host and the container.<host_directory>
is the path on your host machine, and<container_directory>
is the path inside the container where you want to mount the directory.
Set environment variables in a container: Pass environment variables to the container at runtime.
docker run -d -e <ENV_VAR_NAME>=<value> --name <container_name> <image_name>:<tag> # Example: docker run -d -e MY_ENV_VAR=my_value --name myapp_container myapp:latest
Here,
-e
sets an environment variable inside the container, which can be used by the application running in the container.<ENV_VAR_NAME>
is the name of the environment variable, and<value>
is its value.
- Interactive shell access: Start a container with an interactive shell.
Here,docker run -it --name <container_name> <image_name>:<tag> /bin/bash # Example: docker run -it --name myapp_container myapp:latest
-it
allows you to interact with the container's shell, and/bin/bash
specifies the command to run inside the container (in this case, starting a Bash shell). This is useful for debugging or running commands directly inside the container.
- Start Container: Start a stopped container.
Here,docker start <container_name or container_id> # Example: docker start myapp_container
<container_name>
is the name of the container you want to start, or you can use<container_id>
which is the unique identifier for the container. If the container is already running, this command will have no effect.
- Stop Container: Stop a running container.
Here,docker stop <container_name or container_id> # Example: docker stop myapp_container
<container_name>
is the name of the container you want to stop, or you can use<container_id>
which is the unique identifier for the container. This command sends a SIGTERM signal to the container, allowing it to gracefully shut down. If the container does not stop within a certain timeout period (default is 10 seconds), Docker will forcefully kill it.
Restart Container: Restart a running or stopped container.
docker restart <container_name or container_id> # Example: docker restart myapp_container
Here,
<container_name>
is the name of the container you want to restart, or you can use<container_id>
which is the unique identifier for the container. This command stops the container if it is running and then starts it again. It is useful for applying changes or recovering from issues without having to remove and recreate the container.Remove Container: Delete a stopped container.
docker rm <container_name or container_id> # Example: docker rm myapp_container
Here,
<container_name>
is the name of the container you want to remove, or you can use<container_id>
which is the unique identifier for the container. This command removes the container from your system. If the container is running, you will need to stop it first usingdocker stop <container_name or container_id>
. If you want to remove a running container, you can use the-f
flag to forcefully remove it:docker rm -f <container_name or container_id> # Example: docker rm -f myapp_container
🪛Troubleshooting Containers
View Container Logs: Check the logs of a running or stopped container.
docker logs <container_name or container_id> # Example: docker logs myapp_container
Here,
<container_name>
is the name of the container whose logs you want to view, or you can use<container_id>
which is the unique identifier for the container. This command displays the standard output and error logs from the container, which can help you diagnose issues or understand what the application is doing.View Real-time Logs: Stream logs from a running container in real-time.
docker logs -f <container_name or container_id> # Example: docker logs -f myapp_container
Here,
-f
stands for "follow," allowing you to see new log entries as they are generated. This is useful for monitoring the container's activity in real-time.Execute Command in Running Container: Run a command inside a running container.
docker exec -it <container_name or container_id> <command> # Example: docker exec -it myapp_container /bin/bash
Here,
-it
allows you to interact with the container's shell, and<command>
is the command you want to run inside the container. This is useful for debugging or performing administrative tasks directly within the container.Inspect Container: View detailed information about a container.
docker inspect <container_name or container_id> # Example: docker inspect myapp_container
This command provides detailed information about the container, including its configuration, state, network settings, and more. The output is in JSON format, which can be useful for debugging or understanding how the container is set up. You can also use the
--format
option to filter the output and display specific information. For example, to get the container's IP address:docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container_name or container_id> # Example: docker inspect --format '{{ .NetworkSettings.IPAddress }}' myapp_container
Check Container Resource Usage: View resource usage statistics for a container.
docker stats <container_name or container_id> # Example: docker stats myapp_container
This command displays real-time statistics about the container's CPU, memory, network I/O, and disk I/O usage. It helps you monitor the performance of your container and identify any resource bottlenecks.
- Check Container Health Status: View the health status of a container.
This command checks the health status of a container, which is useful if you have defined a health check in your Dockerfile or when running the container. The health status can bedocker inspect --format '{{ .State.Health.Status }}' <container_name or container_id> # Example: docker inspect --format '{{ .State.Health.Status }}' myapp_container
healthy
,unhealthy
, orstarting
, indicating whether the container is functioning correctly or experiencing issues.
🌐 Working with Docker Networks
- List Networks: View all Docker networks on your system.
docker network ls
- Create Network: Create a new Docker network.
This command creates a new network that containers can use to communicate with each other. You can specify additional options such as the network driver (e.g.,docker network create <network_name> # Example: docker network create my_network
bridge
,overlay
,null
etc.) using the--driver
flag:docker network create --driver bridge my_network # Example: docker network create --driver bridge my_network
- Connect Container to Network: Attach a container to a specific network.
This command connects a running container to a specified network, allowing it to communicate with other containers on that network. If the container is not running, you can use thedocker network connect <network_name> <container_name or container_id> # Example: docker network connect my_network myapp_container
--alias
option to assign an alias for the container on the network:
This allows you to refer to the container by the alias within the network, making it easier to manage and communicate with multiple containers.docker network connect --alias my_alias my_network myapp_container # Example: docker network connect --alias my_alias my_network myapp_container
Disconnect Container from Network: Remove a container from a network.
docker network disconnect <network_name> <container_name or container_id> # Example: docker network disconnect my_network myapp_container
This command disconnects a running container from a specified network, preventing it from communicating with other containers on that network. If the container is not running, you can still disconnect it using the same command.
Inspect Network: View detailed information about a network.
docker network inspect <network_name> # Example: docker network inspect my_network
This command provides detailed information about the specified network, including its configuration, connected containers, and IP address ranges. The output is in JSON format, which can be useful for debugging or understanding how the network is set up. You can also use the
--format
option to filter the output and display specific information. For example, to get the list of connected containers:docker network inspect --format '{{ range .Containers }}{{ .Name }} {{ end }}' <network_name> # Example: docker network inspect --format '{{ range .Containers }}{{ .Name }} {{ end }}' my_network
Remove Network: Delete a Docker network.
docker network rm <network_name> # Example: docker network rm my_network
This command removes a specified network from your system. You can only remove networks that are not currently in use by any containers. If you try to remove a network that is still connected to one or more containers, Docker will return an error. To forcefully remove a network and disconnect all connected containers, you can use the
-f
flag:docker network rm -f <network_name> # Example: docker network rm -f my_network
- List Containers in a Network: View all containers connected to a specific network.
This command lists all containers that are currently connected to the specified network. The output will show the names of the containers, making it easy to see which containers are part of the network. If you want to see more details about each container, you can modify thedocker network inspect <network_name> --format '{{ range .Containers }}{{ .Name }} {{ end }}' # Example: docker network inspect my_network --format '{{ range .Containers }}{{ .Name }} {{ end }}'
--format
option to include additional information, such as the container ID or IP address:docker network inspect <network_name> --format '{{ range .Containers }}{{ .Name }} ({{ .ID }}) - {{ .IPv4Address }} {{ end }}' # Example: docker network inspect my_network --format '{{ range .Containers }}{{ .Name }} ({{ .ID }}) - {{ .IPv4Address }} {{ end }}'
- Remove all unused networks: Clean up networks that are not in use by any containers.
This command removes all networks that are not currently in use by any containers. It helps keep your Docker environment clean and free of unused resources. Docker will prompt you for confirmation before proceeding with the removal. If you want to skip the confirmation prompt, you can use thedocker network prune
-f
flag:
This will forcefully remove all unused networks without asking for confirmation.docker network prune -f
- List Containers in a Network: View all containers connected to a specific network.
💻Docker HUB
Login to Docker Hub: Authenticate with your Docker Hub account.
docker login
This command prompts you for your Docker Hub username and password, allowing you to push and pull images from your Docker Hub repository. If you have two-factor authentication enabled, you will need to use a personal access token instead of your password.
Push an Image to Docker Hub: Upload a local image to your Docker Hub repository.
docker push <your_dockerhub_username>/<image_name>:<tag>
This command pushes a local image to your Docker Hub repository. Make sure to replace
<your_dockerhub_username>
,<image_name>
, and<tag>
with your actual Docker Hub username, the name of the image you want to push, and the tag you want to assign to the image. If the image is not tagged, you can tag it using thedocker tag
command before pushing.Pull an Image from Docker Hub: Download an image from Docker Hub repository.
docker pull <image_name>:<tag>
This command pulls an image from Docker Hub to your local machine. If the image is not available locally, Docker will automatically download it from the specified repository. If no tag is specified, Docker defaults to
latest
. You can also pull images from specific repositories by specifying the repository name in the format<repository_name>/<image_name>:<tag>
.docker pull <repository_name>/<image_name>:<tag> # Example: docker pull myrepo/myapp:latest
Search for Images on Docker Hub: Find images available on Docker Hub.
docker search <search_term>
This command searches for images on Docker Hub that match the specified search term. It returns a list of images along with their names, descriptions, and star ratings. You can use this command to discover new images or find official images for popular software.
Logout from Docker Hub: Log out of your Docker Hub account.
docker logout
This command logs you out of your Docker Hub account, removing your authentication credentials from the local Docker client. You will need to log in again using
docker login
to push or pull images from your Docker Hub repository.
📂 Docker Volumes & Data Management
List Volumes: View all Docker volumes on your system.
docker volume ls
This command lists all Docker volumes on your system, showing their names and other details. Docker volumes are used to persist data generated by and used by Docker containers. By listing the volumes, you can see which ones are available for use or need to be cleaned up.
Create Named Volume: Create a new Docker volume with a specific name.
docker volume create <volume_name> # Example: docker volume create my_volume
This command creates a new Docker volume with the specified name. Named volumes are useful for persisting data across container restarts and for sharing data between multiple containers.
Remove Volume: Delete a Docker volume.
docker volume rm <volume_name> # Example: docker volume rm my_volume
This command removes a specified Docker volume from your system. If the volume is currently in use by any containers, you will need to stop and remove those containers first. If you want to forcefully remove a volume that is in use, you can use the
-f
flag:docker volume rm -f <volume_name> # Example: docker volume rm -f my_volume
Mount Named volume with running container: Attach a named volume to a running container.
docker run -d -v <volume_name>:<container_directory> --name <container_name> <image_name>:<tag> # Example: docker run -d -v my_volume:/data --name myapp_container myapp:latest
This command mounts a named volume to a specific directory inside the container, allowing the container to read and write data to that volume.
<volume_name>
is the name of the volume you want to mount, and<container_directory>
is the path inside the container where you want to mount the volume.- Mount Named volume with running container using
--mount
:
This command achieves the same result as the previous one but uses thedocker run -d --mount type=volume,source=<volume_name>,target=<container_directory> --name <container_name> <image_name>:<tag> # Example: docker run -d --mount type=volume,source=my_volume,target=/data --name myapp_container myapp:latest
--mount
flag, which provides a more flexible and explicit way to specify volume mounts. Thetype=volume
indicates that you are mounting a Docker volume,source=<volume_name>
specifies the name of the volume, andtarget=<container_directory>
is the path inside the container where the volume will be mounted. This method is often preferred for its clarity and flexibility, especially when dealing with multiple mounts or complex configurations.
- Mount Named volume with running container using
Mount Anonymous volume with running container: Attach an anonymous volume to a running container.
docker run -v <container_directory> --name <container_name> <image_name>:<tag> # Example: docker run -d -v /data --name myapp_container myapp:latest
This command creates an anonymous volume that is automatically managed by Docker and mounts it to the specified directory inside the container. The volume will be removed when the container is deleted, making it suitable for temporary data storage.
To create a Bind Mount: Attach a host directory to a container.
docker run -d -v <host_directory>:<container_directory> --name <container_name> <image_name>:<tag> # Example: docker run -d -v /path/to/host/dir:/data --name myapp_container myapp:latest
This command mounts a directory from your host machine into the container, allowing the container to read and write data to that directory.
<host_directory>
is the path on your host machine, and<container_directory>
is the path inside the container where you want to mount the directory. This is useful for sharing files between the host and the container or for persisting data that should remain available even after the container is removed.To create a Bind Mount using
--mount
:docker run -d --mount type=bind,source=<host_directory>,target=<container_directory> --name <container_name> <image_name>:<tag> # Example: docker run -d --mount type=bind,source=/path/to/host/dir,target=/data --name myapp_container myapp:latest
This command achieves the same result as the previous one but uses the
--mount
flag, which provides a more flexible and explicit way to specify bind mounts. Thetype=bind
indicates that you are mounting a directory from the host,source=<host_directory>
specifies the path on the host machine, andtarget=<container_directory>
is the path inside the container where the directory will be mounted. This method is often preferred for its clarity and flexibility, especially when dealing with multiple mounts or complex configurations.
Inspect Volume: View detailed information about a Docker volume.
docker volume inspect <volume_name> # Example: docker volume inspect my_volume
This command provides detailed information about the specified volume, including its name, driver, mount point, and any labels associated with it. The output is in JSON format, which can be useful for debugging or understanding how the volume is set up. You can also use the
--format
option to filter the output and display specific information. For example, to get the mount point of the volume:docker volume inspect --format '{{ .Mountpoint }}' <volume_name> # Example: docker volume inspect --format '{{ .Mountpoint }}' my_volume
Remove all unused volumes: Clean up volumes that are not in use by any containers.
docker volume prune
This command removes all volumes that are not currently in use by any containers. It helps keep your Docker environment clean and free of unused resources. Docker will prompt you for confirmation before proceeding with the removal. If you want to skip the confirmation prompt, you can use the
-f
flag.
🧩 Docker Compose
To manage multi-container applications, Docker Compose allows you to define and run multi-container Docker applications using a simple YAML file.
- Create a
docker-compose.yml
file: Define your services, networks, and volumes in a YAML file.
This example defines two services:version: '3.8' services: web: image: nginx:latest ports: - "8080:80" volumes: - ./html:/usr/share/nginx/html db: image: mysql:latest environment: MYSQL_ROOT_PASSWORD: example volumes: - db_data:/var/lib/mysql volumes: db_data:
web
(using the Nginx image) anddb
(using the MySQL image). Theweb
service maps port 8080 on the host to port 80 in the container and mounts a local directory./html
to the Nginx HTML directory. Thedb
service sets an environment variable for the MySQL root password and mounts a named volumedb_data
to persist MySQL data.
- Start Services: Run the services defined in the
docker-compose.yml
file.
This command starts all the services defined in thedocker-compose up -d
docker-compose.yml
file in detached mode (-d
). If you want to run the services in the foreground, you can omit the-d
flag:docker-compose up
- Stop Services: Stop the running services defined in the
docker-compose.yml
file.
This command stops and removes all the containers defined in thedocker-compose down
docker-compose.yml
file, along with their networks and volumes. If you want to remove the volumes as well, you can use the-v
flag:docker-compose down -v
- Build services: Build the images for the services defined in the
docker-compose.yml
file.
This command builds the images for the services defined in thedocker-compose build
docker-compose.yml
file. If you have made changes to the Dockerfiles or the context, this command will rebuild the images accordingly.
- List running services: View the status of the services defined in the
docker-compose.yml
file.
This command lists all the services defined in thedocker-compose ps
docker-compose.yml
file along with their current status (running, exited, etc.). It provides a quick overview of the state of your multi-container application.
- Execute command in a container: Run a command inside a specific service container.
For example, to open a shell in thedocker-compose exec <service_name> <command>
web
service container:
This command allows you to run a command inside a specific service container defined in thedocker-compose exec web /bin/bash
docker-compose.yml
file. Theexec
command is useful for debugging or performing administrative tasks directly within the container. You can replace<service_name>
with the name of the service you want to access, and<command>
with the command you want to run inside that service's container. If you want to run an interactive shell, you can use-it
flags:docker-compose exec -it <service_name> /bin/bash
🗑️ Cleaning Up Docker Resources
Remove all unused volumes: Clean up volumes that are not in use by any containers.
docker volume prune
This command removes all volumes that are not currently in use by any containers. It helps free up disk space and keep your Docker environment tidy. Docker will prompt you for confirmation before proceeding with the removal. If you want to skip the confirmation prompt, you can use the
-f
flag:docker volume prune -f
This will forcefully remove all unused volumes without asking for confirmation.
Remove all unused images: Clean up images that are not tagged or used by any containers.
docker image prune
This command removes all images that are not tagged or used by any containers. It helps free up disk space and keep your Docker environment tidy. Docker will prompt you for confirmation before proceeding with the removal. If you want to skip the confirmation prompt, you can use the
-f
flag:
Remove all unused containers: Clean up stopped containers.
docker container prune
This command removes all stopped containers from your system. It helps free up resources and keep your Docker environment tidy. Docker will prompt you for confirmation before proceeding with the removal. If you want to skip the confirmation prompt, you can use the
-f
flag:
Conclusion
This cheat sheet provides a quick reference to the most commonly used Docker commands, helping you manage your Docker environment efficiently. Whether you're working with images, containers, networks, or volumes, these commands will help you streamline your workflow and focus on building great applications. Feel free to bookmark this page or print it out for easy access while working with Docker. Happy Dockering! 🐳🚀
📝 Additional Resources
💬 Have Questions or Suggestions?
Drop a comment below or connect with me on LinkedIn or GitHub. Let’s make apps faster together! 🚀
Subscribe to my newsletter
Read articles from KUNTAL MAITY directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

KUNTAL MAITY
KUNTAL MAITY
I’m a passionate Full-Stack Developer who loves building performance-driven web and mobile applications. I work primarily with JavaScript, React, Next.js, Node.js, and MongoDB, and I enjoy simplifying complex concepts into developer-friendly tutorials. On this blog, you'll find hands-on guides, real-world projects, and developer insights—all aimed at helping you level up your coding skills and build production-ready apps. Whether you're into backend performance, frontend polish, or full-stack architecture, there's something here for you! Let’s learn, build, and grow together. 💻✨