# DOCKER-BP-006: Avoid apt-get upgrade

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

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

## Description

Detects use of `apt-get upgrade` or `apt-get dist-upgrade` in Dockerfiles.
Running system upgrades in Docker builds creates unpredictable, non-reproducible images
and can introduce breaking changes or security vulnerabilities.

## Vulnerable Code

```python
FROM ubuntu:latest
RUN apt-get update && apt-get upgrade -y  # ❌ Bad practice
```

## Secure Code

```python
FROM ubuntu:22.04  # Specific version
RUN apt-get update && apt-get install -y nginx=1.18.0-0ubuntu1
```

## Detection Rule (Python SDK)

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


@dockerfile_rule(
    id="DOCKER-BP-006",
    name="Avoid apt-get upgrade",
    severity="MEDIUM",
    cwe="CWE-710",
    category="best-practice",
    tags="docker,dockerfile,apt-get,upgrade,package-manager,ubuntu,debian,reproducibility,best-practice,anti-pattern,build",
    message="Avoid apt-get upgrade in Dockerfiles. Use specific base image versions instead."
)
def avoid_apt_get_upgrade():
    return any_of(
        instruction(type="RUN", contains="apt-get upgrade"),
        instruction(type="RUN", contains="apt-get dist-upgrade")
    )
```

## How to Fix

- Use apt-get instead of apt in Dockerfiles for stable CLI behavior
- Always run apt-get update && apt-get install in the same RUN instruction
- Add --no-install-recommends to minimize installed packages
- Clean up with rm -rf /var/lib/apt/lists/* in the same layer

## FAQ

**Q: Why use apt-get instead of apt?**

apt is designed for interactive use and its output format may change between versions. apt-get provides a stable CLI interface suitable for scripting and Dockerfiles.

**Q: Why combine update and install in one RUN?**

Docker caches layers. If apt-get update is in a separate RUN, the package index cache may be stale when install runs, causing package-not-found errors.

## References

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

---

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