slimtoolkit/slim · error

invalid port range in EXPOSE: %s / error: %s

Error message

invalid port range in EXPOSE: %s / error: %s

What it means

ParseDockerExposeOpt returns 'invalid port range in EXPOSE: %s / error: %s' when nat.ParsePortRange cannot parse the port portion of an EXPOSE value. The port must be a number or a numeric range like '1000-2000'. Non-numeric or out-of-range ports produce this wrapped error including the underlying cause.

Source

Thrown at pkg/app/master/command/clifvparser.go:67

		return ""
	default:
		return flag //should validate if it can be a Docker volume name
	}
}

// based on expose opt parsing in Docker
func ParseDockerExposeOpt(values []string) (map[docker.Port]struct{}, error) {
	exposedPorts := map[docker.Port]struct{}{}

	for _, raw := range values {
		if strings.Contains(raw, ":") {
			return nil, fmt.Errorf("invalid EXPOSE format: %s", raw)
		}

		proto, ports := nat.SplitProtoPort(raw)
		startPort, endPort, err := nat.ParsePortRange(ports)
		if err != nil {
			return nil, fmt.Errorf("invalid port range in EXPOSE: %s / error: %s", raw, err)
		}

		for i := startPort; i <= endPort; i++ {
			portInfo, err := nat.NewPort(proto, strconv.FormatUint(i, 10))
			if err != nil {
				return nil, err
			}

			exposedPorts[docker.Port(portInfo)] = struct{}{}
		}
	}
	return exposedPorts, nil
}

func ParsePortBindings(values []string) (map[docker.Port][]docker.PortBinding, error) {
	portBindings := map[docker.Port][]docker.PortBinding{}

	for _, raw := range values {

View on GitHub (pinned to 81940d17fa)

Solutions

  1. Replace service names with numeric ports, e.g. 'http' -> '80'.
  2. Ensure ranges are numeric with start <= end and within 0-65535, e.g. '1000-2000'.
  3. Check the wrapped underlying error in the message for the exact parse failure.
  4. Normalize inputs (trim whitespace, remove stray characters) before parsing.

Example fix

// before
expose := []string{"http"}
// after
expose := []string{"80"}
Defensive patterns

Strategy: validation

Validate before calling

func validPortOrRange(s string) bool {
    if strings.Contains(s, "-") {
        parts := strings.SplitN(s, "-", 2)
        a, e1 := strconv.Atoi(parts[0]); b, e2 := strconv.Atoi(parts[1])
        return e1 == nil && e2 == nil && a <= b && a > 0 && b <= 65535
    }
    p, err := strconv.Atoi(s)
    return err == nil && p > 0 && p <= 65535
}

Try / catch

ports, err := command.ParseDockerExposeOpt(values)
if err != nil {
    if strings.Contains(err.Error(), "invalid port range") {
        return fmt.Errorf("fix numeric ports/ranges: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling ParseDockerExposeOpt with values like 'http', '80-80000', '80-', or other port strings that fail Docker's port-range parsing.

Common situations: Using service names instead of numbers (EXPOSE in Dockerfiles accepts names via build, but this parser does not); typos in port ranges; reversed or overflowing range bounds.

Related errors


AI-assisted analysis of slimtoolkit/slim@81940d17fa (2026-08-31). Data as JSON: /api/errors/f010089bd3601705. Report an issue: GitHub.