blog icon indicating copy to clipboard operation
blog copied to clipboard

Commonly Used Docker Commands

Open qingquan-li opened this issue 1 year ago • 0 comments

Use docker help and docker-compose help for a complete list and description of commands and options.

1. Docker General Commands

  • docker --version: Display the version of Docker installed.
  • docker info: Display system-wide information about Docker and its components.

2. Docker Image Commands

  • docker images: List all local images.
  • docker rmi <image_id>: Remove a Docker image.
  • docker pull <image_name>: Pull an image from Docker Hub or another registry.
  • docker push <image_name>: Push an image to a Docker registry.
  • docker build -t <image_name>:<tag> .: Build an image from a Dockerfile in the current directory.

3. Docker Container Commands

  • docker ps: List running containers.
  • docker ps -a: List all containers (both running and stopped).
  • docker run <options> <image_name>: Create and start a container from an image.
  • docker start <container_id>: Start an existing container.
  • docker stop <container_id>: Stop a running container.
  • docker restart <container_id>: Restart a running or stopped container.
  • docker rm <container_id>: Remove a stopped container.
  • docker logs <container_id>: Fetch the logs of a container.
  • docker exec -it <container_id> <command>: Execute a command in a running container. Commonly used to open a shell with sh or bash.

4. Docker Network Commands

  • docker network ls: List all Docker networks.
  • docker network create <network_name>: Create a new network.
  • docker network connect <network_name> <container_id>: Connect a container to a network.
  • docker network disconnect <network_name> <container_id>: Disconnect a container from a network.
  • docker network rm <network_name>: Remove a Docker network.

5. Docker Volume Commands

  • docker volume ls: List all volumes.
  • docker volume create <volume_name>: Create a new volume.
  • docker volume rm <volume_name>: Remove a volume.

6. Docker Compose Commands

  • docker-compose up: Start services defined in a docker-compose.yml file.
  • docker-compose down: Stop and remove all services defined in the docker-compose.yml file.
  • docker-compose logs: View the logs for services.
  • docker-compose ps: List the services and their status.
  • docker-compose build: Build or rebuild services.

7. Docker on Linux Server

  • sudo systemctl status docker: Check the status of the Docker service.
  • sudo journalctl -u docker.service: Check the Docker logs.
  • sudo docker container prune: Remove all stopped containers.
  • sudo docker image prune: Remove all dangling images.
  • sudo docker image prune -a: To remove unused images (including the dangling images).

A snapshot of the current resource usage (such as CPU, memory, and network I/O) for all running Docker containers:

$ sudo docker stats --no-stream
CONTAINER ID   NAME                    CPU %     MEM USAGE / LIMIT     MEM %     NET I/O          BLOCK I/O         PIDS
f7cd215dccc5   us-data-demo-frontend   0.00%     3.594MiB / 1.925GiB   0.18%     2.51kB / 1.7kB   4.11MB / 8.19kB   2
73932f5e7302   us-data-demo-backend    0.01%     66.33MiB / 1.925GiB   3.36%     4.73kB / 151kB   8.13MB / 0B       2

System-wide Cleanup:

Remove Stopped containers, All unused (with -a) and dangling images, Unused networks, All build cache:

$ sudo docker system prune -a
sudo docker system prune -a
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache

Are you sure you want to continue? [y/N]

qingquan-li avatar Aug 30 '23 15:08 qingquan-li