Crate dockerfile_parser

Source
Expand description

§Rust parser for Dockerfile syntax

A pure Rust library for parsing and inspecting Dockerfiles, useful for performing static analysis, writing linters, and creating automated tooling around Dockerfiles. It can provide useful syntax errors in addition to a full syntax tree.

§Quick start

use dockerfile_parser::Dockerfile;

let dockerfile = Dockerfile::parse(r#"
  FROM alpine:3.11 as builder
  RUN echo "hello world" > /hello-world

  FROM scratch
  COPY --from=builder /hello-world /hello-world
"#).unwrap();

for stage in dockerfile.iter_stages() {
  println!("stage #{}", stage.index);
  for ins in stage.instructions {
    println!("  {:?}", ins);
  }
}

Structs§

ArgInstruction
A Dockerfile ARG instruction.
BreakableString
A Docker string that may be broken across several lines, separated by line continuations (\\\n), and possibly intermixed with comments.
CmdInstruction
A Dockerfile CMD instruction.
CopyFlag
A key/value pair passed to a COPY instruction as a flag.
CopyInstruction
A Dockerfile COPY instruction.
Dockerfile
A parsed Dockerfile.
EntrypointInstruction
A Dockerfile ENTRYPOINT instruction.
EnvInstruction
A Dockerfile ENV instruction.
EnvVar
An environment variable key/value pair
FromFlag
A key/value pair passed to a FROM instruction as a flag.
FromInstruction
A Dockerfile FROM instruction.
ImageRef
A parsed docker image reference
Label
A single label key/value pair.
LabelInstruction
A Dockerfile LABEL instruction.
MiscInstruction
A miscellaneous (unsupported) Dockerfile instruction.
RunInstruction
A Dockerfile RUN instruction.
Span
A byte-index tuple representing a span of characters in a string
SpannedComment
A comment with a character span.
SpannedString
A string with a character span.
Splicer
A utility to repeatedly replace spans of text within a larger document.
Stage
A single stage in a multi-stage build.
Stages
A collection of stages in a [multi-stage build].
StringArray
A string array (ex. [“executable”, “param1”, “param2”])

Enums§

BreakableStringComponent
A component of a breakable string.
Error
A Dockerfile parsing error.
Instruction
A single Dockerfile instruction.
Rule
ShellOrExecExpr
A string that may be broken across many lines or an array of strings.
StageParent
The parent image of a Docker build stage

Functions§

substitute
Given a map of key/value pairs, perform variable substitution on a given input string. max_recursion_depth controls the maximum allowed recursion depth if variables refer to other strings themselves containing variable references. A small number but reasonable is recommended by default, e.g. 16. If None is returned, substitution was impossible, either because a referenced variable did not exist, or recursion depth was exceeded.

Type Aliases§

Result
A Dockerfile parsing Result.