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

# Docker Commands

### Installation

#### Docker (Debian-Based Systems)

* To install Docker on Debian-based systems (e.g., Ubuntu, Kali Linux):

```bash
sudo apt update && sudo apt install docker.io -y
```

#### Docker Compose

* To install Docker Compose:

```bash
sudo apt install docker-compose -y
```

### Docker Commands

| Command                                        | Description                                         |
| ---------------------------------------------- | --------------------------------------------------- |
| `docker --version`                             | Display the installed Docker version.               |
| `docker pull debian:11`                        | Pull the Debian 11 image from Docker Hub.           |
| `docker build -f Dockerfile`                   | Build a Docker image using a `Dockerfile`.          |
| `docker build -t <custom-name> .`              | Build an image and assign a custom name (tag).      |
| `docker run <image>`                           | Run a container from the specified image.           |
| `docker ps`                                    | List running containers.                            |
| `docker ps -a`                                 | List all containers (running and stopped).          |
| `docker images`                                | List all Docker images.                             |
| `docker stop <container-id/container-name>`    | Stop a running container.                           |
| `docker start <container-id/container-name>`   | Start a stopped container.                          |
| `docker restart <container-id/container-name>` | Restart a container.                                |
| `docker rm <container-id/dcontainer-name>`     | Remove a container.                                 |
| `docker rmi <image-id/image-name>`             | Remove an image.                                    |
| `docker system prune -a`                       | Remove unused images, stopped containers, and cache |

### Docker Compose Commands

| Command                  | Description                                         |
| ------------------------ | --------------------------------------------------- |
| `docker-compose start`   | Start services defined in the `docker-compose.yml`. |
| `docker-compose stop`    | Stop services.                                      |
| `docker-compose pause`   | Pause services.                                     |
| `docker-compose unpause` | Unpause services.                                   |
| `docker-compose ps`      | List services and their status.                     |
| `docker-compose up -d`   | Start services in detached mode.                    |
| `docker-compose down`    | S**top and completely remove** containers, networks |

### Docker Operations

| Command                                                       | Description                                                           |
| ------------------------------------------------------------- | --------------------------------------------------------------------- |
| `docker run -it <image> /bin/sh`                              | Run a container interactively with a shell session.                   |
| `docker run --hostname <hostname> -it centos:7 /bin/bash`     | Run a CentOS 7 container with a custom hostname and interactive bash. |
| `docker exec -it <container> <command>`                       | Execute a command inside a running container.                         |
| `docker stop $(docker ps -a -q)`                              | Stop all running containers.                                          |
| `docker rm $(docker ps -a -q)`                                | Remove all stopped containers.                                        |
| `docker rmi $(docker images -q)`                              | Remove all Docker images.                                             |
| `docker network rm $(docker network ls -q)`                   | Remove all Docker networks.                                           |
| `docker volume rm $(docker volume ls -q)`                     | Remove all Docker volumes.                                            |
| `docker commit <container_id_or_name> <new_image_name>:<tag>` | Save changes in a container as a new image.                           |
| `docker login -u <username> -p <password> docker.io`          | Log in to Docker Hub.                                                 |
| `docker tag <image>:latest <username>/<image>:<new-tag>`      | Tag a local image for Docker Hub.                                     |
| `docker push <username>/<image>:<tag>`                        | Push an image to Docker Hub.                                          |
| `docker network ls`                                           | List Docker networks.                                                 |
| `docker network rm <network_name_or_id>`                      | Remove a specific Docker network.                                     |

***

### Reference

* [Docker Official Documentation](https://docs.docker.com/)
* [Docker GitHub Releases](https://github.com/docker)
