Skip to content

Instantly share code, notes, and snippets.

@esmaeelE
Last active December 28, 2025 09:14
Show Gist options
  • Select an option

  • Save esmaeelE/e96c63e23f18fa416d0b313eb8ca91ca to your computer and use it in GitHub Desktop.

Select an option

Save esmaeelE/e96c63e23f18fa416d0b313eb8ca91ca to your computer and use it in GitHub Desktop.
Configure vscode to autoformat file when press ctrl + s

How to Auto-Format Dockerfiles and docker-compose in VS Code

This guide helps you automatically format your Dockerfile every time you press Ctrl+S in Visual Studio Code – no manual steps required! Debian/Ubuntu Edition

It also includes optional setup for Hadolint (a popular Dockerfile linter) to highlight best practices, security issues, and inefficiencies in real time.

Step 1: Install dockerfmt (Formatter) on Debian/Ubuntu

dockerfmt is the tool that formats your Dockerfile (indentation, multi-line RUN commands, etc.).

Manual binary install dockerfmt

  1. Download the latest Linux x86_64 binary:

    wget https://github.com/reteps/dockerfmt/releases/latest/download/dockerfmt-Linux-x86_64 -O dockerfmt
  2. Make it executable and move to a system directory:

    chmod +x dockerfmt
    sudo mv dockerfmt /usr/local/bin/

Verify installation:

dockerfmt --version

Step 2: Install hadolint (Linter) on Debian/Ubuntu

Hadolint scans your Dockerfile for best practices (e.g., unpinned versions, unnecessary packages) and security risks.

Direct binary install (fast and reliable)

sudo wget -O /usr/local/bin/hadolint https://github.com/hadolint/hadolint/releases/download/v2.12.0/hadolint-Linux-x86_64
sudo chmod +x /usr/local/bin/hadolint

Verify:

hadolint --version

Install yamlint

Linux / Windows (Go required)

go install github.com/google/yamlfmt@latest

Make sure it’s on your PATH:

yamlfmt --version

Step 3: Install Required VS Code Extensions

  1. Open VS Code → Extensions view (Ctrl+Shift+X).
  2. Install Run on Save by emeraldwalk (ID: emeraldwalk.runonsave) – this enables auto-formatting on save.
  3. (Optional) Install hadolint by exiasr (ID: exiasr.hadolint) – provides real-time linting with underlines and a Problems panel.

Step 4: Configure VS Code Settings

Open your user settings.json:

  • Press Ctrl+Shift+P → type Preferences: Open User Settings (JSON)

Add or merge this configuration:

{

    "files.autoSave": "afterDelay",
    // add with run on save, after that if I press ctrl + s in dockerfile, run dockerfmt
    "emeraldwalk.runonsave": {
        "commands": [
            {
                "match": "Dockerfile*",
                // "cmd": "dockerfmt --write --indent=4 --space-redirects --newline ${file}"
                "cmd": "dockerfmt --write --indent=4 --space-redirects ${file}"
            },
            {
                "match": "(docker-compose\\.ya?ml|compose\\.ya?ml|.*\\.ya?ml)",
                "cmd": "yamlfmt ${file}"
            }
        ]
    },
    "makefile.configureOnOpen": true,
    "editor.minimap.enabled": false,
    "git.confirmSync": false,
    "editor.formatOnSaveMode": "modificationsIfAvailable",
    "editor.formatOnSave": true,
    "notebook.formatOnSave.enabled": true,
    "mbake.formatOnSave": true,
    "files.autoSaveWhenNoErrors": true,
    "files.autoSaveWorkspaceFilesOnly": true,
}

Customization options

  • Add --newline for a trailing newline:
    "cmd": "dockerfmt --write --indent=4 --space-redirects --newline ${file}"
  • For 2-space indentation:
    "cmd": "dockerfmt --write --indent=2 --space-redirects ${file}"

Step 5: Test It!

  1. Open or create a Dockerfile.
  2. Add some intentionally messy multi-line commands (e.g., unindented RUN instructions).
  3. Press Ctrl+S – the file should auto-format perfectly!
  4. (If Hadolint is installed) Check the Problems panel for any linting warnings or suggestions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment