Dockerfile Parse

2.0.1 · active · verified Thu Apr 09

Dockerfile Parse is a Python library designed for programmatic manipulation and parsing of Dockerfile files. It provides structured access to Dockerfile instructions, making it suitable for tasks like static analysis, linting, and automated tooling around Docker container builds. The library is actively maintained, with version 2.0.1 being the current release, and it frequently receives updates for compatibility with newer Python versions.

Warnings

Install

Imports

Quickstart

Initialize a `DockerfileParser` instance, load Dockerfile content as a string, and then access or modify its parsed elements such as the structure, labels, or the base image. The `content` attribute can be used to read or write the full Dockerfile string.

from pprint import pprint
from dockerfile_parse import DockerfileParser

dfp = DockerfileParser()
dfp.content = """
FROM base
LABEL foo="bar baz"
USER me
"""

# Print the parsed structure:
print("--- Structure ---")
pprint(dfp.structure)

# Print labels:
print("\n--- Labels ---")
pprint(dfp.labels)

# Set a new base image:
dfp.baseimage = 'centos:7'

# Print the new Dockerfile content with an updated FROM line:
print("\n--- Updated Dockerfile Content ---")
print(dfp.content)

view raw JSON →