Working with docker

Useful Docker commands for managing containers, images, volumes, and system resources.

Debug docker container

List running container
docker ps

Enter the container using bash:
docker exec -it <container_name_or_id> /bin/bash

If bash is not available, try:
docker exec -it <container_name_or_id> /bin/sh

Show logs
docker logs <container_name_or_id>

To exit the container:
 exit

General Docker Information

  • docker version: Show Docker version information

  • docker info: Display system-wide Docker information

  • docker stats: Display a live stream of container(s) resource usage statistics

Container Management

  • docker ps: List running containers

  • docker ps -a: List all containers (running and stopped)

  • docker start <container>: Start one or more stopped containers

  • docker stop <container>: Stop one or more running containers

  • docker restart <container>: Restart one or more containers

  • docker rm <container>: Remove one or more containers

  • docker logs <container>: Fetch the logs of a container

  • docker exec -it <container> <command>: Run a command in a running container

Image Management

  • docker images: List images

  • docker pull <image>: Pull an image or a repository from a registry

  • docker push <image>: Push an image or a repository to a registry

  • docker rmi <image>: Remove one or more images

  • docker build -t <name:tag> .: Build an image from a Dockerfile

Volume Management

  • docker volume ls: List volumes

  • docker volume create <name>: Create a volume

  • docker volume rm <volume>: Remove one or more volumes

Network Management

  • docker network ls: List networks

  • docker network create <name>: Create a network

  • docker network rm <network>: Remove one or more networks

System Management

  • docker system df: Show Docker disk usage

  • docker system prune: Remove unused data

  • docker system prune -a: Remove all unused images not just dangling ones

  • docker system prune -a --volumes: Remove all unused images and volumes

Resource Cleanup

  • docker container prune: Remove all stopped containers

  • docker image prune: Remove unused images

  • docker volume prune: Remove all unused local volumes

  • docker network prune: Remove all unused networks

Docker Compose

  • docker-compose up: Create and start containers

  • docker-compose down: Stop and remove containers, networks, images, and volumes

  • docker-compose ps: List containers

  • docker-compose logs: View output from containers

Advanced Commands

  • docker inspect <container/image>: Return low-level information on Docker objects

  • docker events: Get real time events from the server

  • docker top <container>: Display the running processes of a container

Remember to use these commands responsibly, especially those that remove resources. Always ensure you're working in the correct environment before executing destructive commands.

Last updated