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 psShows all currently running containers.
List All Containers (Including Stopped)
docker ps -aShows all containers, including stopped ones.
Run a New Container
docker run -it ubuntuRuns a container interactively (
-itallows you to interact with the container) with theubuntuimage.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> bashView Container Logs
docker logs <container_id>Shows the logs of a container (useful for debugging).
Image Commands
List Docker Images
docker imagesLists all Docker images available on your system.
Pull an Image from Docker Hub
docker pull <image_name>Example:
docker pull ubuntuBuild an Image from a Dockerfile
docker build -t <image_name> .Builds an image from the current directory (where the
Dockerfileis 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-imagePush 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-imageView Image Details
docker inspect <image_id>Provides detailed information about an image or container.
Volume Commands
List Docker Volumes
docker volume lsLists all volumes available on your system.
Create a Docker Volume
docker volume create <volume_name>Creates a new volume. Example:
docker volume create my-volumeRemove 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 lsLists 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 upStarts all the services defined in a
docker-compose.ymlfile.Start Docker Compose Services in Detached Mode
docker-compose up -dStarts the services in detached mode (in the background).
Stop Docker Compose Services
docker-compose downStops and removes all containers, networks, and volumes defined in the
docker-compose.ymlfile.Build Docker Compose Services
docker-compose buildBuilds the images for the services defined in
docker-compose.yml.View Docker Compose Logs
docker-compose logsDisplays 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 infoDisplays detailed information about the Docker system, including the version, storage driver, number of containers, images, etc.
View Docker Version
docker --versionDisplays the current version of Docker installed on your system.
Clean Up Docker System (Remove Unused Containers, Images, and Volumes)
docker system pruneCleans up unused Docker objects, including containers, images, and volumes. You can add the
-aflag to also remove unused images:docker system prune -aView Disk Usage
docker system dfDisplays 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 statsDisplays 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/bashShow all networks:
docker network lsShow 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.