Docker Commands Cheat Sheet for Developers
Docker has become one of the most important tools for modern software development and DevOps. It allows developers to package applications with all dependencies into lightweight containers, ensuring that applications run consistently across different environments.
Whether you are a backend developer, DevOps engineer, or full-stack developer, learning Docker commands can significantly improve your deployment workflow.
In this guide, we will cover the most commonly used Docker commands categorized into Images, Containers, Volumes, Networks, DockerHub, and Troubleshooting.
What is Docker?
Docker is an open-source platform that allows developers to build, ship, and run applications inside containers.
Containers are lightweight, portable environments that include everything needed to run an application:
- Application code
- Runtime
- System libraries
- Dependencies
This ensures your application works the same way on:
- Local machine
- Testing server
- Production server
- Cloud environments
Docker Image Commands
Docker images are the blueprints used to create containers. They contain the application and all required dependencies.
List all local images
docker images
Displays all Docker images stored on your local system.
Delete an image
docker rmi <image_name>
Removes a Docker image from the local system.
Example:
docker rmi nginx
Remove unused images
docker image prune
Deletes unused images that are not associated with any containers.
Build an image from a Dockerfile
docker build -t <image_name>:<version> .
Builds a Docker image using a Dockerfile.
Example:
docker build -t myapp:v1 .
Build an image without cache
docker build -t <image_name>:<version> . --no-cache
Forces Docker to rebuild all layers instead of using cached layers.
Docker Container Commands
Containers are running instances of Docker images.
List all containers (running + stopped)
docker ps -a
Displays all containers on the system.
List running containers
docker ps
Shows only containers that are currently running.
Run a new container
docker run <image_name>
If the image is not available locally, Docker will automatically download it from DockerHub.
Example:
docker run nginx
Run container in background (detached mode)
docker run -d <image_name>
The container runs in the background without blocking the terminal.
Run container with custom name
docker run --name <container_name> <image_name>
Example:
docker run --name my-nginx nginx
Port Binding
Port binding allows the container application to be accessed from the host machine.
docker run -p <host_port>:<container_port> <image_name>
Example:
docker run -p 8080:80 nginx
Now the application can be accessed at:
http://localhost:8080
Set environment variables
docker run -e <var_name>=<var_value> <image_name>
Example:
docker run -e MYSQL_ROOT_PASSWORD=123456 mysql
Start or stop a container
docker start <container_name>
docker stop <container_name>
Example:
docker stop my-nginx
Inspect container details
docker inspect <container_name>
Shows detailed configuration and metadata of a container.
Delete a container
docker rm <container_name>
Removes a stopped container from the system.
Docker Troubleshooting Commands
These commands help debug issues with running containers.
Fetch container logs
docker logs <container_name>
Shows logs generated by the container.
Access shell inside container
docker exec -it <container_name> /bin/bash
or
docker exec -it <container_name> sh
This opens an interactive terminal inside the container.
DockerHub Commands
DockerHub is a public repository for Docker images.
Pull an image from DockerHub
docker pull <image_name>
Example:
docker pull nginx
Push image to DockerHub
docker push <username>/<image_name>
Example:
docker push abdul/myapp
Login to DockerHub
docker login -u <username>
or
docker login
Logout:
docker logout
Search images on DockerHub
docker search <image_name>
Example:
docker search mysql
Docker Volume Commands
Volumes allow containers to store persistent data outside the container filesystem.
List all volumes
docker volume ls
Create a named volume
docker volume create <volume_name>
Example:
docker volume create mysql_data
Delete a volume
docker volume rm <volume_name>
Mount named volume
docker run --volume <volume_name>:<mount_path>
Example:
docker run --volume mysql_data:/var/lib/mysql mysql
Mount volume using --mount
docker run --mount type=volume,src=<volume_name>,dest=<mount_path>
Anonymous volume
docker run --volume <mount_path>
Docker automatically creates the volume.
Bind mount (host directory)
docker run --volume <host_path>:<container_path>
Example:
docker run --volume /home/project:/app node
Remove unused volumes
docker volume prune
Removes unused anonymous volumes.
Docker Network Commands
Docker networks allow containers to communicate with each other securely.
List networks
docker network ls
Create a network
docker network create <network_name>
Example:
docker network create app-network
Remove a network
docker network rm <network_name>
Remove unused networks
docker network prune
FAQ (30 FAQs)
1 What is Docker used for?
For building, deploying, and running applications inside containers.
2 What is containerization?
Running applications inside isolated containers.
3 Is Docker better than virtual machines?
Docker is lighter and faster.
4 Who created Docker?
Docker was created by Solomon Hykes.
5 Is Docker used in DevOps?
Yes, widely.
6 Can Docker run databases?
Yes.
7 Can Docker run Laravel?
Yes.
8 Can Docker run Node.js apps?
Yes.
9 Can Docker run Python apps?
Yes.
10 Is Docker secure?
Yes, if configured properly.
11 Does Docker work on Windows?
Yes.
12 Does Docker work on Mac?
Yes.
13 Is Docker difficult to learn?
No, basics are easy.
14 What is Dockerfile?
Instructions for building images.
15 What is docker-compose?
Tool to manage multi-container apps.
16 What is Docker image?
Template used to create containers.
17 What is Docker container?
Running instance of an image.
18 What is Docker registry?
Storage for images.
19 What is DockerHub?
Public Docker registry.
20 Is Docker used in production?
Yes.
21 What companies use Docker?
Netflix, Amazon, Spotify.
22 Can Docker run microservices?
Yes.
23 What is Kubernetes?
Container orchestration tool.
24 What is Docker Swarm?
Docker's native orchestration.
25 What is bind mount?
Host directory mounted in container.
26 What is Docker volume?
Persistent storage.
27 Can Docker improve deployment?
Yes.
28 Is Docker used in cloud?
Yes.
29 What is CI/CD with Docker?
Automated build and deployment.
30 Is Docker worth learning?
Yes, highly demanded skill.
Final Thoughts
Docker is an essential tool for modern application development, microservices architecture, and DevOps workflows.
By mastering these commands, developers can:
- Deploy applications faster
- Ensure consistent environments
- Simplify dependency management
- Improve scalability and portability
If you're a developer preparing for DevOps interviews or learning containerization, this Docker command cheat sheet will serve as a quick reference.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
