Skip to content

Latest commit

 

History

History
247 lines (187 loc) · 4.17 KB

Docker_Essential_Commands.md

File metadata and controls

247 lines (187 loc) · 4.17 KB

Essential Docker Commands Cheat Sheet

General Commands

  1. Check Docker Version:

    docker --version
  2. Show System-Wide Docker Information:

    docker info

Container Lifecycle

  1. List Running Containers:

    docker ps
  2. List All Containers (Running and Stopped):

    docker ps -a
  3. Restart a Stopped Container:

    docker start <container_id>
  4. Stop a Running Container:

    docker stop <container_id>
  5. Access a Running Container (Interactive Mode):

    docker exec -it <container_id> bash
  6. Remove a Specific Container:

    docker rm <container_id>

Advanced Commands

  1. Run a Container in Detached Mode:

    docker run -d <image_name>
  2. Remove All Stopped Containers, Unused Images, and Volumes:

    docker system prune -a --volumes
  3. Run a Container with Resource Limits:

    docker run --memory="512m" --cpus="1" <image_name>

Image Management

  1. List All Docker Images:

    docker images
  2. Pull a Specific Image from Docker Hub:

    docker pull <image_name>:<tag>

    Example:

    docker pull ubuntu:20.04
  3. Export an Image to a .tar File:

    docker save -o my_image.tar <image_name>:<tag>
  4. Load an Image from a .tar File:

    docker load -i my_image.tar
  5. Tag an Image:

    docker tag <image_id> <new_image_name>:<tag>
  6. Push an Image to a Docker Registry:

    docker push <image_name>:<tag>
  7. Remove a Specific Image:

    docker rmi <image_id>
  8. Remove All Unused Images:

    docker image prune -a

Saving, Importing, and Exporting

  1. Save a Docker Image to a .tar File:

    docker save -o my_image.tar <image_name>:<tag>
  2. Load a Docker Image from a .tar File:

    docker load -i my_image.tar
  3. Export a Container to a .tar File:

    docker export -o my_container.tar <container_id>
  4. Import a Container's Filesystem from a .tar File:

    cat my_container.tar | docker import - my_container

Volume Management

  1. List All Volumes:

    docker volume ls
  2. Create a Named Volume:

    docker volume create <volume_name>
  3. Inspect a Volume:

    docker volume inspect <volume_name>
  4. Remove a Volume:

    docker volume rm <volume_name>

Networking

  1. List Docker Networks:

    docker network ls
  2. Create a Docker Network:

    docker network create <network_name>
  3. Connect a Container to a Network:

    docker network connect <network_name> <container_id>
  4. Disconnect a Container from a Network:

    docker network disconnect <network_name> <container_id>

Debugging and Troubleshooting

  1. View Container Logs:

    docker logs <container_id>
  2. Inspect Container Details:

    docker inspect <container_id>
  3. Check Resource Usage of Containers:

    docker stats
  4. Check Disk Usage of Docker Resources:

    docker system df

Commands for Docker Compose

  1. Start Services Defined in docker-compose.yml:

    docker-compose up
  2. Stop Services:

    docker-compose down
  3. Rebuild and Start Services:

    docker-compose up --build
  4. Check Logs of Services:

    docker-compose logs

Key Cleanup Commands

  1. Stop All Running Containers:

    docker stop $(docker ps -q)
  2. Remove All Containers (Stopped or Running):

    docker rm $(docker ps -aq)
  3. Remove All Unused Images:

    docker image prune -a
  4. Remove All Docker Resources (DANGEROUS):

    docker system prune -a --volumes