dockerfile-json

command module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dockerfile-json

Prints Dockerfiles as JSON to stdout, optionally evaluates build args. Uses the official Dockerfile parser from buildkit. Plays well with jq.

Contents

Get it

Using go get:

go get -u github.com/konflux-ci/dockerfile-json

Or download the binary for your platform from the releases page.

Build tags

The dfrunsecurity build tag is optional but recommended when building from source.

With the build tag:

go build -tags dfrunsecurity
  • Supports parsing files with RUN --security=insecure and RUN --security=sandbox flags
  • The Security field in the JSON output will contain the parsed value (defaults to sandbox)

Without the build tag:

go build
  • Cannot parse files containing RUN --security=* flags (will return an error)
  • The Security field in JSON output will always be an empty string
  • Sufficient for use with e.g. Podman or Buildah, which do not support the --security flag

Usage

CLI
dockerfile-json [PATHS...]

Usage of dockerfile-json:
  -build-arg value
    	a key/value pair KEY[=VALUE]
  -build-arg-file string
    	path to a file with containing build-args in the form argument=value (blank lines and # comments are ignored)
  -env value
    	a key/value pair KEY[=VALUE]
  -expand-build-args
    	DEPRECATED: use --expand-vars (expands ARG and ENV variables) (default true)
  -expand-vars
    	expand build ARGs and ENV variables (default true)
  -jsonpath string
    	select parts of the output using JSONPath (https://goessner.net/articles/JsonPath)
  -jsonpath-raw
    	when using JSONPath, output raw strings, not JSON values
  -quiet
    	suppress log output (stderr)

Examples

JSON output

Dockerfile

ARG ALPINE_TAG=3.10

FROM alpine:${ALPINE_TAG} AS build
RUN --network=host echo "Hello world" > abc

FROM build AS test
RUN --security=insecure echo "foo" > bar

FROM scratch
COPY --from=build --chown=nobody:nobody abc .
RUN --mount=type=bind,source=./abc,target=/def
CMD ["echo"]
$ dockerfile-json Dockerfile | jq .
{
  "MetaArgs": [
    {
      "Key": "ALPINE_TAG",
      "DefaultValue": "3.10",
      "ProvidedValue": null,
      "Value": "3.10"
    }
  ],
  "Stages": [
    {
      "Name": "build",
      "OrigCmd": "FROM",
      "BaseName": "alpine:3.10",
      "Platform": "",
      "Comment": "",
      "SourceCode": "FROM alpine:${ALPINE_TAG} AS build",
      "Location": [
        {
          "Start": {
            "Line": 3,
            "Character": 0
          },
          "End": {
            "Line": 3,
            "Character": 0
          }
        }
      ],
      "As": "build",
      "From": {
        "Image": "alpine:3.10"
      },
      "Commands": [
        {
          "CmdLine": [
            "echo \"Hello world\" > abc"
          ],
          "Files": null,
          "FlagsUsed": [
            "network"
          ],
          "Mounts": [],
          "Name": "RUN",
          "NetworkMode": "host",
          "PrependShell": true,
          "Security": "sandbox"
        }
      ]
    },
    {
      "Name": "test",
      "OrigCmd": "FROM",
      "BaseName": "build",
      "Platform": "",
      "Comment": "",
      "SourceCode": "FROM build AS test",
      "Location": [
        {
          "Start": {
            "Line": 6,
            "Character": 0
          },
          "End": {
            "Line": 6,
            "Character": 0
          }
        }
      ],
      "As": "test",
      "From": {
        "Stage": {
          "Named": "build",
          "Index": 0
        }
      },
      "Commands": [
        {
          "CmdLine": [
            "echo \"foo\" > bar"
          ],
          "Files": null,
          "FlagsUsed": [
            "security"
          ],
          "Mounts": [],
          "Name": "RUN",
          "NetworkMode": "default",
          "PrependShell": true,
          "Security": "insecure"
        }
      ]
    },
    {
      "Name": "",
      "OrigCmd": "FROM",
      "BaseName": "scratch",
      "Platform": "",
      "Comment": "",
      "SourceCode": "FROM scratch",
      "Location": [
        {
          "Start": {
            "Line": 9,
            "Character": 0
          },
          "End": {
            "Line": 9,
            "Character": 0
          }
        }
      ],
      "From": {
        "Scratch": true
      },
      "Commands": [
        {
          "Chmod": "",
          "Chown": "nobody:nobody",
          "DestPath": ".",
          "ExcludePatterns": null,
          "From": "build",
          "Link": false,
          "Mounts": null,
          "Name": "COPY",
          "NetworkMode": "",
          "Parents": false,
          "Security": "",
          "SourceContents": null,
          "SourcePaths": [
            "abc"
          ]
        },
        {
          "CmdLine": [],
          "Files": null,
          "FlagsUsed": [
            "mount"
          ],
          "Mounts": [
            {
              "Type": "bind",
              "From": "",
              "Source": "",
              "Target": "",
              "ReadOnly": true,
              "SizeLimit": 0,
              "CacheID": "",
              "CacheSharing": "",
              "Required": false,
              "Env": null,
              "Mode": null,
              "UID": null,
              "GID": null
            }
          ],
          "Name": "RUN",
          "NetworkMode": "default",
          "PrependShell": true,
          "Security": "sandbox"
        },
        {
          "CmdLine": [
            "echo"
          ],
          "Files": null,
          "Mounts": null,
          "Name": "CMD",
          "NetworkMode": "",
          "PrependShell": false,
          "Security": ""
        }
      ]
    }
  ]
}
Extract build stage names

Dockerfile

FROM maven:alpine AS build
# ...

FROM build AS test
# ...

FROM openjdk:jre-alpine
# ...
$ dockerfile-json --jsonpath=..As Dockerfile
"build"
"test"
Extract base images

Dockerfile

ARG ALPINE_MINOR=10
ARG ALPINE_TAG=3.${ALPINE_MINOR}
ARG APP_BASE=scratch

FROM alpine:$ALPINE_TAG AS build
# ...

FROM build
# ...

FROM $APP_BASE
# ...
Expand build args, omit stage aliases and scratch

Using jq:

$ dockerfile-json Dockerfile |
    jq '.Stages[] | select(.From | .Stage or .Scratch | not) | .BaseName'
"alpine:3.10"

Using --jsonpath:


$ dockerfile-json --jsonpath=..Image Dockerfile
"alpine:3.10"

Using --jsonpath, --jsonpath-raw output:

$ dockerfile-json --jsonpath=..Image --jsonpath-raw Dockerfile
alpine:3.10
Set build args, omit stage aliases and scratch
$ dockerfile-json --build-arg ALPINE_TAG=hello-world --jsonpath=..Image Dockerfile
"alpine:hello-world"
Expand build args, include all base names
$  dockerfile-json --jsonpath=..BaseName Dockerfile
"alpine:3.10"
"build"
"scratch"
Ignore build args, include all base names
$ dockerfile-json --expand-build-args=false --jsonpath=..BaseName Dockerfile
"alpine:${ALPINE_TAG}"
"build"
"${APP_BASE}"

Testing

Running tests
go test -v ./...
Adding integration tests

Each directory in testdata/* is the main directory of a test scenario. Each scenario can have sub-scenarios (subdirectories), but they all share one Containerfile.

testdata/
  my-scenario/
    Containerfile          # required, shared by sub-scenarios
    expected.json          # required but can be auto-generated
    args.txt               # optional args for main scenario
    sub-scenario-1/
      expected.json
      args.txt             # args for sub-scenario
    sub-scenario-2/
      expected.json
      args.txt

Generate expected outputs:

# Generate for all test cases
UPDATE_TESTDATA=1 go test

# Or generate for specific test case
UPDATE_TESTDATA=1 go test -run=TestIntegration/my-scenario/variant-1

In this mode (UPDATE_TESTDATA=1), tests overwrite the expected.json files instead of comparing them with the actual output. To compare against the existing data, run the tests without the UPDATE_TESTDATA variable.

Updating test expectations

The update mode is also useful when making changes to existing behavior. Re-generate the output files and verify that your changes achieved what you intended:

UPDATE_TESTDATA=1 go test
git diff testdata/  # Review changes

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址