# Useful Docker commands

##### Stop all Docker containers

To stop all Docker containers from the command line, you can use the following command:

```bash
docker stop $(docker ps -q)
```

**Explanation:**

- <span style="color: rgb(230, 126, 35);">**docker ps -q**</span>: This command lists the container IDs of all running containers.
- **<span style="color: rgb(230, 126, 35);">docker stop</span>**: This stops the containers provided as arguments.

So, **<span style="color: rgb(230, 126, 35);">$(docker ps -q)</span>** will pass all running container IDs to docker stop, effectively stopping all of them.

##### Remove ALL Docker containers Networks

<span style="color: rgb(224, 62, 45);">**WARNING (USED / UNUSED Networks)**</span> This will remove all Networks from ALL container even the one you use.

```bash
docker network rm $(docker network ls -q)
```

**Explanation:**

- <span style="color: rgb(230, 126, 35);">**docker network ls -q**</span>: Lists all network IDs (including those that are in use).
- **<span style="color: rgb(230, 126, 35);">docker network rm</span>**: Removes networks by their IDs.

This will attempt to remove all networks, including the default ones if they are not in use by containers. *<span style="color: rgb(230, 126, 35);">However, if a network is in use by a container, Docker won't allow you to remove it</span>* without first disconnecting the containers.

If you really want to remove all networks including the ones in use, you'd have to stop and remove the containers using those networks first.

##### Remove all Docker volumes (including unused ones)

To remove all Docker volumes (including unused ones), you can use the following command:

```bash
docker volume prune -f -a
```

**Explanation:**

- <span style="color: rgb(230, 126, 35);">**docker volume prune**</span>: Removes all unused volumes (volumes that are not currently associated with any container).
- <span style="color: rgb(230, 126, 35);">**-f or --force**</span>: This flag forces the operation without asking for confirmation.
- **<span style="color: rgb(230, 126, 35);">-a or --all</span>**: Remove all unused volumes, not just anonymous ones

If you want to remove all volumes — even the ones in use by containers (which is dangerous and typically not recommended) — you'd need to remove the containers and then delete the volumes.   
**Here's how you can do it:**

**1. Stop and remove all containers:**

```bash
docker stop $(docker ps -q)
docker rm $(docker ps -aq)
```

**2. Remove all volumes:**

```bash
docker volume rm $(docker volume ls -q)
```

<span style="color: rgb(186, 55, 42);">**Caution:**</span>

- Removing volumes that are in use by containers can result in data loss, as volumes often contain persistent data for applications running in containers.
- Always double-check what volumes are being used and whether you need to keep any before removing them.

##### Remove all Docker images (including images that are not being used by any containers)

To remove all unused images (not in use by any container), including dangling images, you can run:

```bash
docker image prune -f -a
```

**Explanation:**

- <span style="color: rgb(230, 126, 35);">**docker image prune**</span>: This will remove all dangling images.
- <span style="color: rgb(230, 126, 35);">**-f or --force**</span>: This flag forces the operation without asking for confirmation.
- **<span style="color: rgb(230, 126, 35);">-a or --all</span>**: Remove all unused images, not just anonymous ones

To remove all Docker images (including images that are not being used by any containers), you can use the following command:

```bash
docker rmi $(docker images -q)
```

**Explanation:**

- <span style="color: rgb(230, 126, 35);">**docker images -q**</span>: Lists the IDs of all images on your system.
- <span style="color: rgb(230, 126, 35);">**docker rmi**</span>: Removes the images by their IDs.

**If you have containers using some of these images:**  
If there are containers still using these images, <span style="color: rgb(230, 126, 35);">*Docker will not let you remove them. To force removal*</span>, you'd need to stop and remove the containers first.   
**Here's how to do it:**

**1. Stop all running containers:**

```bash
docker stop $(docker ps -q)
```

**2. Remove all containers:**

```bash
docker rm $(docker ps -aq)
```

**3. Remove all images:**

```bash
docker rmi $(docker images -q)
```

<span style="color: rgb(186, 55, 42);">**Caution:**</span>

- Removing all images will mean you lose access to those images unless you pull them again, and you may need to rebuild any images you were working on.
- Double-check before running these commands if you're in a production environment, as this can disrupt your workflows!

##### Remove all stacks of containers in Docker

To remove all stacks of containers in Docker, you'll typically be dealing with Docker Swarm. A stack in Docker refers to a collection of services deployed in a Docker Swarm, often using docker stack deploy.

If you want to remove all stacks (and the services, networks, and containers associated with them), you can do so with the following command:

```bash
docker stack rm $(docker stack ls -q)
```

**Explanation:**

- <span style="color: rgb(230, 126, 35);">**docker stack ls -q**</span>: Lists the names of all deployed stacks.
- <span style="color: rgb(230, 126, 35);">**docker stack rm**</span>: Removes the stack by name, along with the services and containers related to it.

This will remove all stacks deployed in your Docker Swarm environment.

<span style="color: rgb(186, 55, 42);">**Important Notes:**</span>

- This command removes the entire stack — containers, networks, services, and volumes that are part of the stack will be removed.
- If you only want to remove specific stacks, replace $(docker stack ls -q) with the name of the stack you want to remove.

<span style="color: rgb(224, 62, 45);">**Ins0mniA**</span>