> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/docker/docker-commands.md).

# Docker Commands

### 🚀 Docker Commands Cheat Sheet

### 1️⃣ Docker Version & Info

```sh
docker --version          # Show Docker version
docker info               # Display system-wide Docker information
```

### 2️⃣ Working with Docker Images

```sh
docker images             # List all downloaded images
docker image -ls          # List all downloaded images
docker pull <image>       # Download an image from Docker Hub
docker build -t <name> .  # Build an image from a Dockerfile (in the current directory)
docker rmi <image_id>     # Remove an image
docker tag <image> <repo>:<tag>  # Tag an image for pushing to a repository
docker push <repo>:<tag>  # Push an image to Docker Hub
```

### 3️⃣ Managing Docker Containers

```sh
docker ps                 # List running containers
docker ps -a              # List all containers (including stopped ones)
docker run <image>        # Run a container from an image
docker run -d <image>     # Run a container in detached (background) mode
docker run --name <name> <image>  # Run container with a custom name
docker start <container>  # Start a stopped container
docker stop <container>   # Stop a running container
docker restart <container> # Restart a container
docker rm <container>     # Remove a container
docker logs <container>   # Show logs of a running container
docker inspect <container> # Get detailed information about a container
docker exec -it <container> bash  # Access container shell (if bash is available)
docker attach <container> # Attach terminal to a running container
```

### 4️⃣ Exposing Ports & Volumes

```sh
docker run -p 8080:80 <image>    # Map container port 80 to host 8080
docker run -v /host/path:/container/path <image>  # Mount a volume
docker volume ls                 # List all volumes
docker volume create <name>       # Create a named volume
docker volume rm <name>           # Remove a volume
```

### 5️⃣ Docker Networking

```sh
docker network ls                 # List networks
docker network create <name>       # Create a custom network
docker network connect <network> <container>  # Connect a container to a network
docker network inspect <network>   # Show network details
docker network rm <name>           # Remove a network
```

### 6️⃣ Docker Compose (Multi-Container Applications)

```sh
docker-compose up                 # Start all services defined in docker-compose.yml
docker-compose up -d              # Start services in detached mode (background)
docker-compose down               # Stop and remove all services
docker-compose ps                 # List running services
docker-compose logs                # View logs of all services
```

### 7️⃣ Managing Docker System & Cleanup

```sh
docker system prune -a             # Remove all unused containers, networks, images, and caches
docker container prune             # Remove all stopped containers
docker image prune -a              # Remove all unused images
docker volume prune                # Remove all unused volumes
```

### 8️⃣ Saving & Loading Docker Images

```sh
docker save -o <file.tar> <image>  # Save an image as a .tar file
docker load -i <file.tar>          # Load an image from a .tar file
docker export -o <file.tar> <container>  # Export a container as a .tar file
docker import <file.tar>           # Import a saved container as an image
```

### 9️⃣ Docker Container Resource Limits

```sh
docker run --memory=512m <image>   # Limit container memory to 512MB
docker run --cpus=1.5 <image>      # Limit container to 1.5 CPU cores
```

### 🔟 Debugging & Monitoring Containers

```sh
docker stats                        # Show live resource usage of containers
docker top <container>               # Show running processes inside a container
docker diff <container>               # Show file changes inside a container
docker events                         # Stream real-time Docker events
```

**This Markdown file provides \*\*all key Docker commands\*\* categorized for easy reference. 🚀 Let me know if you need further details!**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/docker/docker-commands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
