# docker

## Install docker aws ec2

([Depending on the OS you would run different commands,](https://docs.docker.com/engine/install/) I will be using t3-mini ubuntu from aws)

#### Setup docker apt repository

```
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
```

#### Install latest package

```
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
```

#### Verify installation

```
sudo docker run hello-world
```

You should see some output like this:

<figure><img src="https://3504844606-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkjovAfZK07rWR0t6DR9s%2Fuploads%2FYN0okr9wuKbkQRfWAVAh%2Fimage.png?alt=media&#x26;token=7fc117eb-94a8-4dac-89e9-9d1c003577ba" alt=""><figcaption></figcaption></figure>

## Remove some service

To completely remove some service from Docker (containers, images, volumes, and networks), follow these steps: (replace service\_name with your service, for example kali-linux, appflowy, whatever)

***

### 1. Stop and remove containers related to "service\_name"

```bash
docker ps -a --filter "name=SERVICE_NAME" -q | xargs -r docker rm -f
```

### 2. Remove images related to SERVICE\_NAME

```bash
docker images | grep SERVICE_NAME | awk '{print $3}' | xargs -r docker rmi -f
```

### 3. Remove volumes related to SERVICE\_NAME

```bash
docker volume ls | grep SERVICE_NAME | awk '{print $2}' | xargs -r docker volume rm
```

### 4. Remove networks related to SERVICE\_NAME

```bash
docker network ls | grep SERVICE_NAME | awk '{print $1}' | xargs -r docker network rm
```

***

🔍 **Tip:** Before executing, you can check what will be removed by running the commands without the last part.\
Example:

```bash
docker images | grep SERVICE_NAME
```

This will show the images without deleting them.

## Favorite commands

### Disk space

This command shows the total disk space used by Docker on your **EC2** instance, with a breakdown of images, containers, volumes, layers, and any host bind-mounts.

```
ROOT_DIR="$(docker info -f '{{.DockerRootDir}}' 2>/dev/null || echo /var/lib/docker)"; echo "📦 Docker root: $ROOT_DIR"; echo "🔹 Total:"; sudo du -sh --total "$ROOT_DIR" 2>/dev/null | tail -n1; echo "🔹 Desglose:"; sudo du -sh "$ROOT_DIR/overlay2" "$ROOT_DIR/volumes" "$ROOT_DIR/containers" "$ROOT_DIR/image" 2>/dev/null; echo "🔹 Bind-mounts:"; MAPS=$(docker ps --format '{{.Mounts}}' | tr ',' '\n' | sed -n 's/.*:\(\/[^:]*\):.*/\1/p' | sort -u); [ -n "$MAPS" ] && sudo du -sh $MAPS || echo "No host bind-mounts detected"
```

#### Example Output

```
📦 Docker root: /var/lib/docker
🔹 Total:
4.0G    total
🔹 Desglose:
3.8G    /var/lib/docker/overlay2
113M    /var/lib/docker/volumes
4.4M    /var/lib/docker/containers
21M     /var/lib/docker/image
🔹 Bind-mounts:
No host bind-mounts detected
```
