### Install dockerfmt using go install Source: https://github.com/reteps/dockerfmt/blob/main/README.md Install the dockerfmt binary using the Go toolchain. Ensure Go is installed and configured on your system. ```bash go install github.com/reteps/dockerfmt@latest ``` -------------------------------- ### Example Dockerfile Before Formatting Source: https://github.com/reteps/dockerfmt/blob/main/README.md This is an example of a Dockerfile before being processed by dockerfmt, showcasing various formatting inconsistencies. ```dockerfile MAINTAINER me FROM node:lts-alpine as builder COPY . /app WORKDIR /app ENV MY_VAR my-value ENV a=1 \ b=2 \ c=3 RUN apt-get update && \ # install deps apt-get install -y --no-install-recommends \ vim curl git && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* \ && find /tmp -not -path /tmp -delete ``` -------------------------------- ### Example Dockerfile After Formatting Source: https://github.com/reteps/dockerfmt/blob/main/README.md This is the same Dockerfile after being processed by dockerfmt, demonstrating normalized whitespace, directive alignment, and formatted RUN commands. ```dockerfile LABEL org.opencontainers.image.authors="me" FROM node:lts-alpine as builder COPY . /app WORKDIR /app ENV MY_VAR=my-value ENV a=1 \ b=2 \ c=3 RUN apt-get update \ # install deps && apt-get install -y --no-install-recommends \ vim curl git \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && find /tmp -not -path /tmp -delete ``` -------------------------------- ### Install dockerfmt using Docker Source: https://github.com/reteps/dockerfmt/blob/main/README.md Run dockerfmt as a Docker container, mounting the current directory to format Dockerfiles within it. This is useful for CI environments. ```bash docker run --rm -v $(pwd):/pwd ghcr.io/reteps/dockerfmt:latest /pwd/Dockerfile ``` -------------------------------- ### EditorConfig for Dockerfile Formatting Source: https://github.com/reteps/dockerfmt/blob/main/README.md Example .editorconfig file to define project-level formatting defaults for Dockerfiles, such as indentation and final newline. ```ini [Dockerfile] indent_size = 2 insert_final_newline = true ``` -------------------------------- ### Ignore directive with shell grouping Source: https://github.com/reteps/dockerfmt/blob/main/README.md Example of using '# dockerfmt-ignore' to preserve specific shell command structures within a RUN directive, such as grouped commands. ```dockerfile # dockerfmt-ignore RUN { echo hello && echo world; } # dockerfmt-ignore RUN echo hello; echo world ``` -------------------------------- ### dockerfmt CLI Usage Help Source: https://github.com/reteps/dockerfmt/blob/main/README.md Displays the available commands and flags for the dockerfmt CLI, including options for checking, writing, and configuring formatting. ```bash Usage: dockerfmt [Dockerfile...] [flags] dockerfmt [command] Available Commands: completion Generate the autocompletion script for the specified shell help Help about any command version Print the version number of dockerfmt Flags: -c, --check Check if the file(s) are formatted -h, --help help for dockerfmt -i, --indent uint Number of spaces to use for indentation (default 4) -n, --newline End the file with a trailing newline -s, --space-redirects Redirect operators will be followed by a space -w, --write Write the formatted output back to the file(s) ``` -------------------------------- ### Configure via EditorConfig Source: https://context7.com/reteps/dockerfmt/llms.txt Define project-level formatting defaults using an .editorconfig file. ```ini # .editorconfig [Dockerfile] indent_size = 2 insert_final_newline = true space_redirects = true ``` -------------------------------- ### Format Dockerfiles In-Place Source: https://context7.com/reteps/dockerfmt/llms.txt Use the -w or --write flag to overwrite the original files with formatted content. ```bash # Format and save changes back to the file dockerfmt -w Dockerfile # Format multiple files in place dockerfmt -w Dockerfile Dockerfile.dev Dockerfile.prod ``` -------------------------------- ### Format Dockerfile and print to stdout Source: https://github.com/reteps/dockerfmt/blob/main/README.md Use dockerfmt to format a Dockerfile and display the output on standard output without modifying the original file. ```bash dockerfmt Dockerfile ``` -------------------------------- ### Format Dockerfiles via CLI Source: https://context7.com/reteps/dockerfmt/llms.txt Basic commands to format Dockerfiles and print to stdout or read from stdin. ```bash # Format a single Dockerfile and print to stdout dockerfmt Dockerfile # Format multiple files dockerfmt Dockerfile.dev Dockerfile.prod # Read from stdin cat Dockerfile | dockerfmt ``` -------------------------------- ### Format Dockerfile from Path Source: https://github.com/reteps/dockerfmt/blob/main/js/README.md Use `formatDockerfile` to format a Dockerfile located at a given path. Options for indentation and trailing newline are available. Requires an async context. ```javascript import { formatDockerfile } from '@reteps/dockerfmt' // Alternatively, you can use `formatDockerfileContents` to format a string instead of a file. const result = await formatDockerfile('../tests/comment.dockerfile', { indent: 4, trailingNewline: true }) console.log(result) ``` -------------------------------- ### Format Dockerfile in place Source: https://github.com/reteps/dockerfmt/blob/main/README.md Use the -w flag with dockerfmt to format a Dockerfile and overwrite the original file with the formatted content. ```bash dockerfmt -w Dockerfile ``` -------------------------------- ### Format Dockerfile lines in Go Source: https://context7.com/reteps/dockerfmt/llms.txt Uses the Go library to format Dockerfile lines based on a provided configuration. ```go package main import ( "fmt" "strings" "github.com/reteps/dockerfmt/lib" ) func main() { content := `FROM node:lts-alpine as builder COPY . /app WORKDIR /app RUN npm install && npm run build ` lines := strings.SplitAfter(content, "\n") config := &lib.Config{ IndentSize: 4, TrailingNewline: true, SpaceRedirects: false, } formatted := lib.FormatFileLines(lines, config) fmt.Println(formatted) } ``` -------------------------------- ### Run dockerfmt via Docker Source: https://context7.com/reteps/dockerfmt/llms.txt Execute dockerfmt using a containerized environment, mounting the current directory to access files. ```bash # Format a Dockerfile using Docker docker run --rm -v $(pwd):/pwd ghcr.io/reteps/dockerfmt:latest /pwd/Dockerfile # Format in place docker run --rm -v $(pwd):/pwd ghcr.io/reteps/dockerfmt:latest -w /pwd/Dockerfile # Check formatting in CI docker run --rm -v $(pwd):/pwd ghcr.io/reteps/dockerfmt:latest -c /pwd/Dockerfile ``` -------------------------------- ### Check Dockerfile Formatting Source: https://context7.com/reteps/dockerfmt/llms.txt Use the -c or --check flag to verify formatting without modification, returning a non-zero exit code if files are unformatted. ```bash # Check if Dockerfile is properly formatted (exits non-zero if not) dockerfmt -c Dockerfile # Check multiple files dockerfmt -c Dockerfile Dockerfile.dev # Example CI script if ! dockerfmt -c Dockerfile; then echo "Dockerfile is not formatted. Run: dockerfmt -w Dockerfile" exit 1 fi ``` -------------------------------- ### Configure dockerfmt in pre-commit Source: https://github.com/reteps/dockerfmt/blob/main/README.md Add this configuration to your .pre-commit-config.yaml file to enable dockerfmt. Use pre-commit autoupdate to pin the version. ```yaml repos: - repo: https://github.com/reteps/dockerfmt rev: main # run `pre-commit autoupdate` to pin a version hooks: - id: dockerfmt args: - --indent=4 - --newline - --write ``` -------------------------------- ### Go Library - FormatFileLines Source: https://context7.com/reteps/dockerfmt/llms.txt The core Go function for formatting Dockerfile content provided as lines of text. ```APIDOC ## Go Library - FormatFileLines The core formatting function in the Go library that takes file lines and a configuration, returning the formatted content. ```go package main import ( "fmt" "strings" "github.com/reteps/dockerfmt/lib" ) func main() { content := `FROM node:lts-alpine as builder COPY . /app WORKDIR /app RUN npm install && npm run build ` lines := strings.SplitAfter(content, "\n") config := &lib.Config{ IndentSize: 4, TrailingNewline: true, SpaceRedirects: false, } formatted := lib.FormatFileLines(lines, config) fmt.Println(formatted) } ``` ``` -------------------------------- ### Configure Pre-commit Hook Source: https://context7.com/reteps/dockerfmt/llms.txt Integrate dockerfmt into a pre-commit workflow using the .pre-commit-config.yaml file. ```yaml # .pre-commit-config.yaml repos: - repo: https://github.com/reteps/dockerfmt rev: main # run `pre-commit autoupdate` to pin a version hooks: - id: dockerfmt args: - --indent=4 - --newline - --write ``` -------------------------------- ### Node.js API - formatDockerfile Source: https://context7.com/reteps/dockerfmt/llms.txt Formats a Dockerfile from disk using the provided configuration options. ```APIDOC ## Node.js API - formatDockerfile The `formatDockerfile` function reads a file from disk and returns the formatted content. Available in the `@reteps/dockerfmt` npm package. ```javascript import { formatDockerfile } from '@reteps/dockerfmt' const result = await formatDockerfile('./Dockerfile', { indent: 4, trailingNewline: true, spaceRedirects: false }) console.log(result) ``` ``` -------------------------------- ### Format JSON-form directives Source: https://context7.com/reteps/dockerfmt/llms.txt Ensures consistent spacing within JSON-style array directives like CMD, RUN, and ENTRYPOINT. ```dockerfile # Input RUN ["ls" , "-la"] CMD [ "node", "server.js" ] ENTRYPOINT ["python" ,"-u", "app.py"] # Output RUN ["ls", "-la"] CMD ["node", "server.js"] ENTRYPOINT ["python", "-u", "app.py"] ``` -------------------------------- ### Format Dockerfile from file Source: https://context7.com/reteps/dockerfmt/llms.txt Reads a Dockerfile from disk and returns the formatted content using the Node.js API. ```javascript import { formatDockerfile } from '@reteps/dockerfmt' const result = await formatDockerfile('./Dockerfile', { indent: 4, trailingNewline: true, spaceRedirects: false }) console.log(result) ``` -------------------------------- ### Normalize ENV directive syntax Source: https://context7.com/reteps/dockerfmt/llms.txt Converts legacy ENV key-value pairs to the modern key=value format and aligns multiline declarations. ```dockerfile # Input ENV MY_VAR my-value ENV a=1 \ b=2 \ c=3 # Output ENV MY_VAR=my-value ENV a=1 \ b=2 \ c=3 ``` -------------------------------- ### Format Dockerfile from stdin Source: https://github.com/reteps/dockerfmt/blob/main/README.md Pipe the content of a Dockerfile to dockerfmt via standard input for formatting. This is useful for integrating with other tools or scripts. ```bash cat Dockerfile | dockerfmt ``` -------------------------------- ### Normalize whitespace and comments Source: https://context7.com/reteps/dockerfmt/llms.txt Cleans up excess blank lines and ensures proper alignment for comments and directives. ```dockerfile # Input FROM node:lts-alpine as builder COPY . /app WORKDIR /app # This copies in all the `package.json` files in `apps` and `packages`, which # Yarn needs to correctly install all dependencies in our workspaces. # Output FROM node:lts-alpine as builder COPY . /app WORKDIR /app # This copies in all the `package.json` files in `apps` and `packages`, which # Yarn needs to correctly install all dependencies in our workspaces. ``` -------------------------------- ### Ignore Formatting with Comments Source: https://context7.com/reteps/dockerfmt/llms.txt Use the # dockerfmt-ignore comment to skip formatting for the immediately following directive. ```dockerfile # Input Dockerfile FROM scratch # dockerfmt-ignore RUN echo "do not format this" && echo "keep as-is" RUN echo "this should be formatted" && echo "yes" # dockerfmt-ignore ENV FOO bar # Output (after formatting) FROM scratch # dockerfmt-ignore RUN echo "do not format this" && echo "keep as-is" RUN echo "this should be formatted" && echo "yes" # dockerfmt-ignore ENV FOO bar ``` -------------------------------- ### Define Dockerfmt Config Struct in Go Source: https://context7.com/reteps/dockerfmt/llms.txt Defines the Config struct for the dockerfmt Go library, specifying formatting options like indentation size, trailing newline, and space around redirects. Use this struct to customize formatting behavior when integrating dockerfmt into your Go projects. ```go package lib type Config struct { IndentSize uint // Number of spaces for indentation (default: 4) TrailingNewline bool // Add trailing newline at end of file SpaceRedirects bool // Add space after redirect operators (>, >>, <) } ``` -------------------------------- ### Convert MAINTAINER to LABEL Source: https://context7.com/reteps/dockerfmt/llms.txt Automatically upgrades deprecated MAINTAINER directives to the modern LABEL format. ```dockerfile # Input MAINTAINER me # Output LABEL org.opencontainers.image.authors="me" ``` -------------------------------- ### Format Dockerfile string Source: https://context7.com/reteps/dockerfmt/llms.txt Formats a Dockerfile string directly in memory using the Node.js API. ```javascript import { formatDockerfileContents } from '@reteps/dockerfmt' const dockerfile = ` FROM node:lts-alpine as builder COPY . /app WORKDIR /app RUN npm install && npm run build ` const formatted = await formatDockerfileContents(dockerfile, { indent: 4, trailingNewline: true, spaceRedirects: false }) console.log(formatted) // Output: // FROM node:lts-alpine as builder // COPY . /app // WORKDIR /app // RUN npm install && npm run build ``` -------------------------------- ### Normalize RUN directive syntax Source: https://context7.com/reteps/dockerfmt/llms.txt Normalizes multiline RUN commands by aligning line continuations and command chaining. ```dockerfile # Input RUN apt-get update && \ # install deps apt-get install -y --no-install-recommends \ vim curl git && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* \ && find /tmp -not -path /tmp -delete # Output RUN apt-get update \ # install deps && apt-get install -y --no-install-recommends \ vim curl git \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* \ && find /tmp -not -path /tmp -delete ``` -------------------------------- ### Configure Space Redirects Source: https://context7.com/reteps/dockerfmt/llms.txt Use the -s or --space-redirects flag to add a space after shell redirect operators. ```bash # Enable space after redirects: echo foo >> bar instead of echo foo >>bar dockerfmt -s Dockerfile # Full formatting with all options dockerfmt -w -n -i 4 -s Dockerfile ``` -------------------------------- ### Node.js API - formatDockerfileContents Source: https://context7.com/reteps/dockerfmt/llms.txt Formats a Dockerfile content string directly, suitable for in-memory operations. ```APIDOC ## Node.js API - formatDockerfileContents The `formatDockerfileContents` function formats a Dockerfile string directly without file I/O, useful for in-memory processing or browser environments. ```javascript import { formatDockerfileContents } from '@reteps/dockerfmt' const dockerfile = ` FROM node:lts-alpine as builder COPY . /app WORKDIR /app RUN npm install && npm run build ` const formatted = await formatDockerfileContents(dockerfile, { indent: 4, trailingNewline: true, spaceRedirects: false }) console.log(formatted) // Output: // FROM node:lts-alpine as builder // COPY . /app // WORKDIR /app // RUN npm install && npm run build ``` ``` -------------------------------- ### Format heredoc blocks Source: https://context7.com/reteps/dockerfmt/llms.txt Formats shell commands contained within heredoc blocks for RUN and COPY directives. ```dockerfile # Input RUN <> /hello echo "World!">>/hello EOF COPY <<-EOF /x x EOF # Output RUN <>/hello echo "World!" >>/hello EOF COPY <<-EOF /x x EOF ``` -------------------------------- ### Check if Dockerfile is formatted Source: https://github.com/reteps/dockerfmt/blob/main/README.md Use the -c flag to check if a Dockerfile is already formatted according to dockerfmt's rules. The command exits with a non-zero status if the file needs formatting. ```bash dockerfmt -c Dockerfile ``` -------------------------------- ### Configure Indentation Source: https://context7.com/reteps/dockerfmt/llms.txt Control the number of spaces used for indentation in multiline directives using the -i or --indent flag. ```bash # Use 2 spaces for indentation (default is 4) dockerfmt -i 2 Dockerfile # Use 8 spaces for indentation dockerfmt --indent 8 Dockerfile # Combine with write flag dockerfmt -w -i 2 Dockerfile ``` -------------------------------- ### Ignore specific Dockerfile directive Source: https://github.com/reteps/dockerfmt/blob/main/README.md Use a '# dockerfmt-ignore' comment on the line preceding a directive to prevent dockerfmt from formatting that specific instruction. ```dockerfile # dockerfmt-ignore RUN echo "this stays exactly as-is" RUN echo "this gets formatted normally" ``` -------------------------------- ### Ensure Trailing Newline Source: https://context7.com/reteps/dockerfmt/llms.txt Use the -n or --newline flag to ensure the formatted file ends with a newline character. ```bash # Format with trailing newline dockerfmt -n Dockerfile # Combine flags for complete formatting dockerfmt -w -n -i 4 Dockerfile ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.