Dockerfile Parse
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
- breaking Python 2 support was completely dropped in version 2.0.0. Applications running on Python 2.x will fail to import or run `dockerfile-parse` version 2.0.0 or newer.
- breaking Support for Python 2.6 was dropped in version 0.0.14.
- gotcha Older versions of `dockerfile-parse` (prior to 1.1.0) do not properly support the `escape` directive in Dockerfiles.
- gotcha Handling of multi-line instructions, specifically with the `add_lines_at()` method, had issues in versions prior to 0.0.14.
- gotcha Error reporting for incorrect LABEL syntax was improved in version 0.0.15. Older versions might provide less descriptive errors or unexpected behavior for malformed LABEL instructions.
Install
-
pip install dockerfile-parse
Imports
- DockerfileParser
from dockerfile_parse import DockerfileParser
Quickstart
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)