🔧 Common Docker Commands
- Start Docker:
systemctl start docker # Linux
open -a Docker # macOS
- Check Docker Version:
docker --version
📦 Working with Containers
- List Running Containers:
docker ps
- List All Containers (Running + Stopped):
docker ps -a
- Run a Container (starts and attaches):
docker run <image_name>
- Run in Detached Mode:
docker run -d <image_name>
- Run with Port Mapping:
docker run -p <host_port>:<container_port> <image_name>
- Stop a Running Container:
docker stop <container_id>
- Start a Stopped Container:
docker start <container_id>
- Remove a Stopped Container:
docker rm <container_id>
📜 Images
- List Docker Images:
docker images
- Pull an Image from Docker Hub:
docker pull <image_name>
- Build an Image from Dockerfile:
docker build -t <image_name> .
- Tag an Image:
docker tag <image_id> <new_image_name>:<tag>
- Remove an Image:
docker rmi <image_id>
🔄 Container Management
- View Logs of a Container:
docker logs <container_id>
- Access a Running Container (Interactive Shell):
docker exec -it <container_id> /bin/bash
- Copy Files from Container to Host:
docker cp <container_id>:<path_inside_container> <host_path>
🏗 Docker Networks
- List Networks:
docker network ls
- Create a Network:
docker network create <network_name>
- Connect a Running Container to a Network:
docker network connect <network_name> <container_id>
🐳 Docker Compose
- Start Services in Detached Mode:
docker-compose up -d
- Stop Services:
docker-compose down
- Build and Start Containers:
docker-compose up --build
📊 Inspecting and Monitoring
- Inspect Container Details:
docker inspect <container_id>
- Display Resource Usage (CPU, Memory):
docker stats
🛠 Volumes
- List Volumes:
docker volume ls
- Create a Volume:
docker volume create <volume_name>
-
Mount a Volume (during
docker run
):
docker run -v <volume_name>:<path_inside_container> <image_name>
💡 Pro Tip: Use docker system prune
to remove unused containers, networks, and images.
Feel free to save or bookmark this cheat sheet for quick reference!
Top comments (20)
You can also make a helper script:
New file at the root folder called
dc
without extension.Content example. Modify for your needs.
Give execute permission with
chmod +x ./dc
And now you can run:
./dc
to show all containers with status./dc up
to start in detached mode./dc install
to run npm install in the node container as user node./dc npm install package-name-here
to run any npm command inside node container. Works with./dc npm run start
too./dc nr
interactive exec inside node container./dc nr node index.js
run any command inside node container./dc recreate
applies any modifications to docker-compose.yml./dc recreate node
applies modifications to compose, only for node container./dc build
if you have a custom dockerfile, does run dc up with a fresh build../dc logs -n 10 -f node
- any other docker-compose command works as expected.Adding one more
docker system prune
This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- unused build cache
Hi keshav Sandhu,
Top, very nice and helpful !
Thanks for sharing.
Thanks man
This is incredible.
The most used commands in docker.
Thank for Writing.
Thank you @keshav___dev
Great read. I would just suggest you to use Compose v2, which is without a hyphen:
docker compose up
.Recover shrinking disk space on every build :
dev.to/keshav___dev/prisma-recap-e...
Prisma cheat sheet
Some comments may only be visible to logged-in visitors. Sign in to view all comments. Some comments have been hidden by the post's author - find out more