ports command validates that a container image only exposes authorized network ports. This helps enforce network security policies and prevent unintended service exposure.
Usage
Description
Theports command extracts the list of exposed ports from the image configuration (from the EXPOSE instruction in Dockerfile) and validates them against an allowed list. Ports can be provided as a comma-separated list or loaded from a file.
This check helps:
- Enforce network security policies
- Prevent unintended port exposure
- Identify unauthorized services
- Control network attack surface
- Validate Dockerfile EXPOSE instructions
Flags
Comma-separated list of allowed ports (e.g.,
80,443) or @<file> to load from JSON/YAML file. Use @- to read from stdin.Short form: -pOutput format:
text or jsonShort form: -oColor output mode:
auto, always, neverSet log level: trace, debug, info, warn, error, fatal, panic
Allowed Ports File Format
When using@<file>, the file must contain an allowed-ports array:
Examples
Inline port list
Load from JSON file
Load from YAML file
Ports from stdin
Check OCI archive
No allowed ports specified
JSON output
Output
Text Format
When validation passes:JSON Format
Exit Codes
| Code | Meaning | Example |
|---|---|---|
| 0 | All exposed ports are allowed or no ports exposed | Image exposes 80,443 and allowed list is 80,443,8080 |
| 1 | Unauthorized ports detected | Image exposes 3000 but allowed list is only 80,443 |
| 2 | Execution error | Invalid port format, file not found |
Configuration
When using theall command, configure ports validation in your config file:
Implementation Details
- Reads exposed ports from image config’s
ExposedPortsfield - Parses port format
<port>/<protocol>(e.g.,8080/tcp,53/udp) - Only validates the port number, protocol is ignored
- Port comparison is exact (no ranges or wildcards)
- Empty allowed list with exposed ports = validation failure
- No exposed ports = always passes (regardless of allowed list)
Common Issues
No allowed ports provided
--allowed-ports with at least one port.
Invalid port format
EXPOSE vs runtime ports
Theports command validates EXPOSE declarations in the Dockerfile, not runtime port mappings (-p in docker run):
Best Practices
Define allowed ports based on your application’s needs
Include common web ports (80, 443) for web applications
Be restrictive with database ports (3306, 5432, etc.)
Document why each port is allowed
Use separate policies for different application types
Port Protocols
The image config stores ports with protocols (e.g.,80/tcp, 53/udp), but the validation only considers the port number:
| Image EXPOSE | Extracted Port |
|---|---|
EXPOSE 80 | 80 |
EXPOSE 80/tcp | 80 |
EXPOSE 53/udp | 53 |
EXPOSE 8080/tcp | 8080 |