{"id":7190,"library":"dockerfile","title":"Dockerfile Parser","description":"The 'dockerfile' Python library (version 3.4.0) provides a high-level representation parser for Dockerfiles, leveraging the official Go parser. It allows programmatic inspection and manipulation of Dockerfile instructions, making it useful for static analysis, linting, or generating Dockerfile content. The library is actively maintained with regular updates.","status":"active","version":"3.4.0","language":"en","source_language":"en","source_url":"https://github.com/asottile/dockerfile","tags":["docker","parsing","devops","containerization","build","go"],"install":[{"cmd":"pip install dockerfile","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"parse_string","correct":"import dockerfile\ncommands = dockerfile.parse_string(\"FROM alpine\\nRUN echo hello\")"},{"symbol":"parse_file","correct":"import dockerfile\ncommands = dockerfile.parse_file(\"path/to/Dockerfile\")"},{"symbol":"Command","correct":"from dockerfile import Command\n# Used within results of parse_string/parse_file"},{"symbol":"all_cmds","correct":"import dockerfile\nknown_cmds = dockerfile.all_cmds()"}],"quickstart":{"code":"import dockerfile\nimport os\n\n# Create a dummy Dockerfile for demonstration\nwith open(\"temp_Dockerfile\", \"w\") as f:\n    f.write(\"FROM python:3.9-slim\\n\")\n    f.write(\"WORKDIR /app\\n\")\n    f.write(\"COPY requirements.txt .\\n\")\n    f.write(\"RUN pip install --no-cache-dir -r requirements.txt\\n\")\n    f.write(\"COPY . .\\n\")\n    f.write(\"CMD [\\\"python\\\", \\\"app.py\\\"]\\n\")\n\ndockerfile_path = \"temp_Dockerfile\"\n\n# Parse a Dockerfile from a file\ntry:\n    commands = dockerfile.parse_file(dockerfile_path)\n\n    print(f\"Parsed {len(commands)} commands from {dockerfile_path}:\")\n    for cmd in commands:\n        print(f\"  Command: {cmd.cmd:<10} Value: {cmd.value}, Original: '{cmd.original}'\")\n\n    # Example: Find all RUN commands\n    run_commands = [c.value for c in commands if c.cmd == 'run']\n    print(f\"\\nRUN commands found: {run_commands}\")\n\nfinally:\n    # Clean up the dummy Dockerfile\n    if os.path.exists(dockerfile_path):\n        os.remove(dockerfile_path)\n","lang":"python","description":"This quickstart demonstrates how to parse a Dockerfile from a file path using `dockerfile.parse_file()`. It then iterates through the parsed commands, printing their type, value, and original line, and shows how to filter for specific command types (e.g., 'RUN')."},"warnings":[{"fix":"Ensure you are installing and importing the correct library for your task: `dockerfile` for parsing Dockerfiles, `docker` for interacting with the Docker daemon.","message":"The `dockerfile` library is a parser for Dockerfiles and is distinct from the `docker` (or `docker-py`) SDK, which is used to interact with the Docker Engine API. Do not confuse the two, as they serve different purposes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Install a Go compiler (e.g., `sudo apt-get install golang` on Debian-based systems) or ensure `pip` is able to download a compatible prebuilt wheel.","message":"Building the 'dockerfile' library from source (e.g., if prebuilt wheels are not available for your specific platform/Python version) requires a Go compiler to be installed and accessible in your system's PATH.","severity":"breaking","affected_versions":"All versions, when building from source"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"pip install dockerfile","cause":"The 'dockerfile' package has not been installed in your Python environment.","error":"ModuleNotFoundError: No module named 'dockerfile'"},{"fix":"Verify that the file path is correct and the Dockerfile exists at that location. Check file permissions to ensure the Python script can read it.","cause":"The Dockerfile path provided to `dockerfile.parse_file()` does not exist or the Python process lacks read permissions for the file.","error":"dockerfile.GoIOError: The file could not be opened: 'path/to/non_existent_Dockerfile'"},{"fix":"Review the Dockerfile content for compliance with Dockerfile syntax rules. Use a Dockerfile linter (e.g., Hadolint) to identify specific syntax issues and best practice violations.","cause":"The Dockerfile content provided to `parse_string()` or read by `parse_file()` contains syntax errors that prevent the underlying Go parser from successfully interpreting it.","error":"dockerfile.GoParseError: Error parsing dockerfile: unexpected token at line X, column Y: ..."}]}