
Useful Commands in Docker
Here’s a list of useful Docker commands that can help you work more efficiently with Docker containers, images, networks, and volumes.
Container Commands
List Running Containers
docker ps
Shows all currently running containers.
List All Containers (Including Stopped)
docker ps -a
Shows all containers, including stopped ones.
Run a New Container
docker run -it ubuntu
Runs a container interactively (
-it
allows you to interact with the container) with theubuntu
image.Start a Container
docker start <container_id>
Starts a stopped container (replace
<container_id>
with the actual container ID).Stop a Running Container
docker stop <container_id>
Stops a running container.
Restart a Container
docker restart <container_id>
Restarts a container.
Remove a Container
docker rm <container_id>
Removes a container (it must be stopped first).
Attach to a Running Container
docker attach <container_id>
Attach your terminal to a running container.
Execute a Command in a Running Container
docker exec -it <container_id> <command>
Runs a command inside a running container. Example:
docker exec -it <container_id> bash
View Container Logs
docker logs <container_id>
Shows the logs of a container (useful for debugging).
Image Commands
List Docker Images
docker images
Lists all Docker images available on your system.
Pull an Image from Docker Hub
docker pull <image_name>
Example:
docker pull ubuntu
Build an Image from a Dockerfile
docker build -t <image_name> .
Builds an image from the current directory (where the
Dockerfile
is located). Example:docker build -t my-ubuntu-image .
Remove an Image
docker rmi <image_id>
Removes an image from the system (useful for cleanup).
Tag an Image
docker tag <image_id> <new_image_name>
Tags an existing image with a new name. Example:
docker tag my-ubuntu-image myusername/my-ubuntu-image
Push an Image to Docker Hub
docker push <image_name>
Pushes an image to a Docker registry (like Docker Hub). Example:
docker push myusername/my-ubuntu-image
View Image Details
docker inspect <image_id>
Provides detailed information about an image or container.
Volume Commands
List Docker Volumes
docker volume ls
Lists all volumes available on your system.
Create a Docker Volume
docker volume create <volume_name>
Creates a new volume. Example:
docker volume create my-volume
Remove a Docker Volume
docker volume rm <volume_name>
Removes a Docker volume.
Inspect a Volume
docker volume inspect <volume_name>
Shows detailed information about a specific volume.
List Mounted Volumes in a Container
docker inspect <container_id> --format='{{json .Mounts}}'
Network Commands
List Docker Networks
docker network ls
Lists all networks created by Docker.
Create a Docker Network
docker network create <network_name>
Creates a new Docker network.
Connect a Container to a Network
docker network connect <network_name> <container_name>
Connects a running container to a network.
Disconnect a Container from a Network
docker network disconnect <network_name> <container_name>
Disconnects a container from a network.
Inspect a Docker Network
docker network inspect <network_name>
Shows detailed information about a network.
Docker Compose Commands
Start Docker Compose Services
docker-compose up
Starts all the services defined in a
docker-compose.yml
file.Start Docker Compose Services in Detached Mode
docker-compose up -d
Starts the services in detached mode (in the background).
Stop Docker Compose Services
docker-compose down
Stops and removes all containers, networks, and volumes defined in the
docker-compose.yml
file.Build Docker Compose Services
docker-compose build
Builds the images for the services defined in
docker-compose.yml
.View Docker Compose Logs
docker-compose logs
Displays the logs of the services started by Docker Compose.
Run a One-Off Command with Docker Compose
docker-compose run <service_name> <command>
Runs a one-off command for a service. For example:
docker-compose run web bash
System Commands
View Docker System Information
docker info
Displays detailed information about the Docker system, including the version, storage driver, number of containers, images, etc.
View Docker Version
docker --version
Displays the current version of Docker installed on your system.
Clean Up Docker System (Remove Unused Containers, Images, and Volumes)
docker system prune
Cleans up unused Docker objects, including containers, images, and volumes. You can add the
-a
flag to also remove unused images:docker system prune -a
View Disk Usage
docker system df
Displays the disk usage of Docker objects (images, containers, volumes).
Docker Logs and Monitoring
View Container Logs
docker logs <container_id>
Shows the logs of a specific container.
Tail the Logs in Real-Time
docker logs -f <container_id>
Follows and streams the logs of a running container.
View Resource Usage (CPU, Memory, etc.)
docker stats
Displays real-time resource usage statistics (CPU, memory, etc.) of running containers.
Helpful Shortcuts
Run a container in detached mode (background):
docker run -d <image_name>
Get the shell of a running container:
docker exec -it <container_id> /bin/bash
Show all networks:
docker network ls
Show all volumes:
docker volume ls
Conclusion
These are some of the most useful Docker commands that help you manage and interact with containers, images, networks, and volumes effectively. Whether you're starting containers, building images, or performing system cleanup, these commands are essential for daily Docker operations.