Docker Cheatsheet
What is Docker?
Docker is an open-source platform that automates the deployment, scaling, and management of applications. It achieves this by packaging an application and its dependencies into an isolated, lightweight unit called a container.
Unlike traditional virtual machines that require a full operating system for each application, containers share the host machine's OS kernel. This makes them incredibly fast to start, highly resource-efficient, and easily portable across different environments.
Why Use Docker?
Consistency
It solves the classic "it works on my machine" problem. A container
behaves exactly the same way in development, testing, and
production.
Efficiency
Containers are lightweight. They boot up in seconds and consume a
fraction of the memory compared to traditional VMs.
Isolation
Each container operates independently. You can run different
applications with conflicting dependencies on the same host without
issues.
CI/CD Ready
Docker fits perfectly into continuous integration and delivery
pipelines, making automated testing and deployment much smoother.
Commands Reference
| Command | Description |
|---|---|
|
Check installed Docker version |
|
Display detailed system-wide Docker info |
| Command | Description |
|---|---|
|
Authenticate with Docker Hub (enter username & password) |
|
Tag local image for Docker Hub repository |
|
Push tagged image to Docker Hub |
|
Log out from Docker Hub |
| Command | Description |
|---|---|
|
Build Docker image from Dockerfile |
|
List all local Docker images |
|
Remove Docker image |
|
Download image from Docker Hub |
|
Upload image to Docker Hub |
| Command | Description |
|---|---|
|
Run a container from an image |
|
Run container in interactive mode (terminal access) |
|
Run container in detached (background) mode |
|
Bind container port to host port |
|
List running containers |
|
List all containers (including stopped ones) |
|
Stop a running container |
|
Start a stopped container |
|
Restart a container |
|
Remove a stopped container |
|
View logs from container |
|
Execute bash shell inside a running container |
|
Execute shell (sh) in a running container |
| Command | Description |
|---|---|
|
Create a named volume |
|
List all volumes |
|
View detailed info about a volume |
|
Remove a named volume |
| Command | Description |
|---|---|
|
List available Docker networks |
|
Create a custom network |
|
Inspect network configuration/details |
|
Remove a Docker network |
| Command | Description |
|---|---|
|
Start all services in docker-compose.yml |
|
Start services in detached (background) mode |
|
Build images and then start services (recommended after Dockerfile change) |
|
Stop and remove containers, networks, volumes created by Compose |
|
Only build images defined in docker-compose.yml |
|
List running containers from Compose |
|
View logs for all services in Compose |
|
View logs for a specific service |
|
Exec into a running service's shell |
| Command | Description |
|---|---|
|
Remove all unused containers, networks, images |
|
Remove all stopped containers |
|
Remove unused Docker images |
|
Remove unused volumes |
|
Stop all containers |
|
Remove all images |
|
Remove all containers |
When to Use What?
Use docker build when:
- You are working with a single service app.
-
You are not using
docker-compose. - You want to manually build a Docker image from a Dockerfile.
Example:
docker build -t my-app-image .
docker run -p 3000:3000 my-app-image
Use docker-compose up --build when:
- You are managing multiple services (e.g., Node.js, MongoDB, Redis, etc.).
-
You have a
docker-compose.ymlfile and aDockerfile. - You made changes to your Dockerfile or environment variables and need to rebuild.
Recommended Command:
docker-compose up --build
Use this to:
-
Run all services defined in
docker-compose.ymlin the background (detached mode). - You already have images built or don't need to rebuild.
Example:
docker-compose up -d
Use this when:
- You want to stop and clean up all services, networks, and volumes created by Docker Compose.
Example:
docker-compose down