slimtoolkit/slim · error

invalid Dockerfile

Error message

invalid Dockerfile

What it means

ErrInvalidDockerfile is a sentinel error in the dockerfile parser package indicating the Dockerfile input could not be parsed as a valid Dockerfile. It is returned by FromFile when parsing a Dockerfile from disk fails because the file is empty, unreadable, or structurally invalid. The parser cannot extract any usable instruction stream from it.

Solutions

  1. Verify the file at the given path exists, is non-empty, and is actually a Dockerfile (starts with a valid instruction like FROM).
  2. Run 'docker build' or a linter on the same Dockerfile to confirm syntax validity independently.
  3. Check that your tooling passes the Dockerfile path, not the build-context directory, to FromFile.
  4. Regenerate or restore the Dockerfile from version control if it was truncated or corrupted.

Example fix

// before
ast, err := parser.FromFile(ctx, "./build")
// after
ast, err := parser.FromFile(ctx, "./Dockerfile")
Defensive patterns

Strategy: validation

Validate before calling

info, err := os.Stat(path)
if err != nil || info.IsDir() || info.Size() == 0 {
    return fmt.Errorf("dockerfile missing or empty: %s", path)
}
// optional: sniff first line for FROM/ARG/escape directive

Try / catch

if _, err := parser.FromFile(ctx, path); err != nil {
    if errors.Is(err, parser.ErrInvalidDockerfile) {
        // surface path + first bytes for diagnosis
    }
    return err
}

Prevention

When it happens

Trigger: Calling dockerfile/parser FromFile with a path that points to an empty file, a non-Dockerfile text file, or a file that fails AST-level parsing.

Common situations: Pointing build tooling at the wrong path (e.g. a directory or a .dockerignore), a Dockerfile deleted or truncated by CI, or a Dockerfile with unsupported syntax from a newer builder version.

Related errors


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

Appendix: source

Thrown at pkg/docker/dockerfile/parser/parser.go:17

// Package parser implements a Dockerfile parser
package parser

import (
	"errors"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"github.com/slimtoolkit/slim/pkg/docker/dockerfile/ast"
	"github.com/slimtoolkit/slim/pkg/docker/dockerfile/spec"
	"github.com/slimtoolkit/slim/pkg/docker/instruction"
)

var (
	ErrInvalidDockerfile = errors.New("invalid Dockerfile")
)

//TODO:
//* support incremental, partial and instruction level parsing
//* support parsing from reader and from string

func FromFile(fpath string) (*spec.Dockerfile, error) {
	fo, err := os.Open(fpath)
	if err != nil {
		return nil, err
	}

	defer fo.Close()

	astParsed, err := ast.Parse(fo)
	if err != nil {
		return nil, err
	}

View on GitHub (pinned to 81940d17fa)