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.
dockerfmt is the tool that formats your Dockerfile (indentation, multi-line RUN commands, etc.).
-
Download the latest Linux x86_64 binary:
wget https://github.com/reteps/dockerfmt/releases/latest/download/dockerfmt-Linux-x86_64 -O dockerfmt
-
Make it executable and move to a system directory:
chmod +x dockerfmt sudo mv dockerfmt /usr/local/bin/
Verify installation:
dockerfmt --versionHadolint scans your Dockerfile for best practices (e.g., unpinned versions, unnecessary packages) and security risks.
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/hadolintVerify:
hadolint --versionInstall yamlint
Linux / Windows (Go required)
go install github.com/google/yamlfmt@latestMake sure it’s on your PATH:
yamlfmt --version
- Open VS Code → Extensions view (
Ctrl+Shift+X). - Install Run on Save by emeraldwalk (ID:
emeraldwalk.runonsave) – this enables auto-formatting on save. - (Optional) Install hadolint by exiasr (ID:
exiasr.hadolint) – provides real-time linting with underlines and a Problems panel.
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,
}- Add
--newlinefor a trailing newline:
"cmd": "dockerfmt --write --indent=4 --space-redirects --newline ${file}" - For 2-space indentation:
"cmd": "dockerfmt --write --indent=2 --space-redirects ${file}"
- Open or create a
Dockerfile. - Add some intentionally messy multi-line commands (e.g., unindented
RUNinstructions). - Press Ctrl+S – the file should auto-format perfectly!
- (If Hadolint is installed) Check the Problems panel for any linting warnings or suggestions.