# DOCKER-BP-021: Missing -y flag for apt-get

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

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

## Description

apt-get install without -y flag. Add -y or --yes for non-interactive builds.

## Vulnerable Code

```python
FROM ubuntu:22.04

# Bad: Missing -y flag
# Build will hang waiting for user confirmation
RUN apt-get update
RUN apt-get install nginx curl

# This will fail in CI/CD pipelines
```

## Secure Code

```python
FROM ubuntu:22.04

# Good: Using -y flag for non-interactive installation
RUN apt-get update && \
    apt-get install -y nginx curl && \
    rm -rf /var/lib/apt/lists/*

# Alternative: Using --yes flag
RUN apt-get update && \
    apt-get install --yes \
        nginx \
        curl \
        ca-certificates && \
    rm -rf /var/lib/apt/lists/*
```

## 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-021",
    name="Missing -y flag for apt-get",
    severity="LOW",
    cwe="CWE-710",
    category="best-practice",
    tags="docker,dockerfile,apt-get,package-manager,automation,ci-cd,build,ubuntu,debian,best-practice,non-interactive",
    message="apt-get install without -y flag. Add -y or --yes for non-interactive builds."
)
def missing_apt_assume_yes():
    return all_of(
        instruction(type="RUN", contains="apt-get install"),
        instruction(type="RUN", not_contains="-y"),
        instruction(type="RUN", not_contains="--yes")
    )
```

## 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.

---

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