# DOCKER-BP-013: Missing dnf clean all

> **Severity:** LOW | **CWE:** CWE-710

- **Language:** Docker
- **Category:** Best Practice
- **URL:** https://codepathfinder.dev/registry/docker/best-practice/DOCKER-BP-013
- **Detection:** `pathfinder scan --ruleset docker/DOCKER-BP-013 --project .`

## Description

Detects 'dnf install' commands without subsequent 'dnf clean all'.
Package manager caches unnecessarily increase image size by storing package metadata
and repository information that is not needed at runtime.

## Vulnerable Code

```python
FROM fedora:38

# Bad: Leaves dnf cache, increases image size
RUN dnf install -y nginx
```

## Secure Code

```python
FROM fedora:38

# Good: Cleans cache in same layer
RUN dnf install -y nginx && dnf clean all
```

## Detection Rule (Python SDK)

```python
from rules.container_decorators import dockerfile_rule
from rules.container_matchers import instruction
from rules.container_combinators import all_of


@dockerfile_rule(
    id="DOCKER-BP-013",
    name="Missing dnf clean all",
    severity="LOW",
    cwe="CWE-710",
    category="best-practice",
    tags="docker,dockerfile,dnf,package-manager,fedora,rhel,cache,cleanup,image-size,optimization,best-practice",
    message="RUN uses 'dnf install' without 'dnf clean all'. This increases image size."
)
def missing_dnf_clean_all():
    return all_of(
        instruction(type="RUN", contains="dnf install"),
        instruction(type="RUN", not_contains="dnf clean all")
    )
```

## How to Fix

- Review your Dockerfile to address the missing dnf clean all issue
- Follow Docker official best practices for image building
- Use docker build --check to validate Dockerfile syntax and best practices

## FAQ

**Q: Why does this rule flag missing dnf clean all?**

RUN uses 'dnf install' without 'dnf clean all'. This increases image size.

**Q: How do I fix this?**

Review the secure code example in the playground above and apply the recommended pattern to your Dockerfile or docker-compose.yml.

## References

- [Docker Best Practices](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/)
- [Dockerfile Best Practice: Clean package manager cache](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#apt-get)

---

Source: https://codepathfinder.dev/registry/docker/best-practice/DOCKER-BP-013
Code Pathfinder — Open source, type-aware SAST with cross-file dataflow analysis
