Must-know Docker commands for power users.
Container Management
docker ps
# Lists running containersdocker ps -a
# Lists all containers (including stopped ones)docker run [image]
# Creates and runs a container based on an imagedocker start [id/name]
# Starts a stopped containerdocker stop [id/name]
# Stops a running containerdocker restart [id/name]
# Restarts a containerdocker rm [id/name]
# Removes a stopped containerdocker exec -it [id/name] /bin/sh
# Runs a command inside a containerdocker logs [id/name]
# Shows the logs of a containerdocker inspect [id/name]
# Shows details of a containerdocker port [id/name]
# Shows the ports assigned to a containerdocker stats [id/name]
# Displays resource usage statistics for containersdocker top [id/name]
# Shows running processes inside a container
Image Management
docker images
# Lists available images locallydocker pull [image]
# Downloads an image from a repositorydocker rmi [image]
# Removes a local imagedocker build -t [image-name] .
# Builds an image from a Dockerfiledocker tag [id/name] [new-name]
# Tags an imagedocker push [image-name]
# Pushes an image to a repository
Volume Management
docker volume create [volume-name]
# Creates a volumedocker volume ls
# Lists all volumesdocker volume inspect [volume-name]
# Shows details of a volumedocker volume rm [volume-name]
# Removes a volume
Docker Networks
docker network ls
# Lists all networksdocker network create [network-name]
# Creates a networkdocker network inspect [network-name]
# Shows details of a networkdocker network connect [network-name] [container-id/name]
# Connects a container to a networkdocker network disconnect [network-name] [container-id/name]
# Disconnects a container from a networkdocker network rm [network-name]
# Removes a network
Docker Compose Management
docker-compose up
# Starts the services defined in docker-compose.ymldocker-compose down
# Stops and removes the containers, networks, and volumes created by docker-composedocker-compose ps
# Lists containers managed by docker-composedocker-compose logs
# Shows logs of services managed by docker-composedocker-compose exec [service] [command]
# Runs a command inside a container managed by docker-composedocker-compose build
# Builds images defined in docker-compose.ymldocker-compose pull
# Pulls images defined in docker-compose.ymldocker-compose restart
# Restarts services managed by docker-compose
Inspection and Debugging
docker inspect [resource]
# Shows details of any resource (container, image, network, volume)docker events
# Shows real-time events from the Docker enginedocker info
# Displays detailed information about the Docker environmentdocker history [image]
# Shows the build history of an imagedocker diff [id/name]
# Shows filesystem changes in a containerdocker logs -f [id/name]
# Follows the logs of a container in real-time
Other Useful Commands
docker system df
# Shows disk usage by images, containers, and volumesdocker system prune
# Removes unused data (stopped containers, unreferenced images, etc.)docker attach [id/name]
# Attaches to the console of a running containerdocker cp [id/name]:[path-inside] [local-path]
# Copies files from a container to your local machinedocker save -o [file.tar] [image]
# Saves an image to a tar filedocker load -i [file.tar]
# Loads an image from a tar filedocker export [id/name] > [file.tar]
# Exports the filesystem of a containerdocker import [file.tar]
# Imports a tar file as a Docker image
Comments
Post a Comment