top | item 43628037

Dockerfmt: A Dockerfile Formatter

135 points| spicypete | 11 months ago |github.com | reply

62 comments

order
[+] jensenbox|11 months ago|reply
I had a chuckle when I looked at the source code and could not find a Dockerfile in there. I want to kick the tires on it and the easiest way would be to run it as a Docker container against an existing file and alas, I cannot.
[+] PhilippGille|11 months ago|reply
> the easiest way would be to run it as a Docker container

Regarding this part, you can always just run a base image and add the app yourself. I'm on mobile so can't test, but should be along these lines:

    docker run --rm --name dockerfmt \
    -v /path/to/Dockerfile:/tmp/Dockerfile \
    golang:1.24-alpine sh -c \
    "apk add git && go run github.com/reteps/dockerfmt@latest /tmp/Dockerfile"
> against an existing file

For this part yes, you'd still need one, but it can be any of your own.

[+] spicypete|11 months ago|reply
Hi there — I’ll try to distribute a docker release of the binary tomorrow!
[+] klysm|11 months ago|reply
The project should certainly also be formatting its own docker file via a docker invocation
[+] revskill|11 months ago|reply
The author doesn't know how to use AI.
[+] krick|11 months ago|reply
I usually share the sentiment, but, come on, it's packaged as a single binary file...
[+] mdaniel|11 months ago|reply
I don't want to vouch for the flagged and dead comment https://news.ycombinator.com/item?id=43630653 because I suspect it was killed for its tone but wow it is really illustrative of the QA that didn't go into this product
[+] spicypete|11 months ago|reply
What I can do for you is fix the bugs promptly! I resolved the bugs in the `0.2.7` release. I haven't graduated college yet and don't have a full time job, so my QA skills are still improving.
[+] grandempire|11 months ago|reply
You aren’t a real software engineer if your project doesn’t have 50 dot files in the root for your formatters, package mangers, linters, and ci.

Who formats the formatter configs?

[+] zufallsheld|11 months ago|reply
> Who formats the formatter configs?

Other formatters of course since the configs are often yaml, toml, ini or json.

[+] mdaniel|11 months ago|reply
waaaat? https://github.com/reteps/dockerfmt#:~:text=The%20RUN%20pars...

I am firmly in the camp of

  RUN set -e ;\
      export DEBIAN_FRONTEND=noninteractive ;\
      etc etc
so I guess this tool isn't for me
[+] spicypete|11 months ago|reply
Is there any reason you prefer `set -e` over `&&`? I'm curious if this is a readability thing.
[+] yjftsjthsd-h|11 months ago|reply
> The RUN parser currently doesn't support grouping or semicolons in commands

But then example show that it does support `&&`? Why the difference? I pretty much always write

  RUN foo && \
      bar && \
      :
but it seems syntactically identical to the also valid

  RUN set -e && \
      foo ; \
      bar ; \
      :
[+] solatic|11 months ago|reply
Or, you can write an actual shell script file (i.e. with a .sh extension) to be stored in your repository, ADD it in a throwaway context (i.e. multi-stage builds), then RUN --mount=type=bind to put it into a temporary directory in the build container so that you can execute it. This way, the script doesn't pollute the container, and you have proper separation of concerns, including the ability to use library functions, running shell linters directly, or using higher-level languages like Python if you really need it for some reason
[+] spicypete|11 months ago|reply
I use `mvdan/sh` [1] under the hood for processing the commands. So it will reformat

  if [ foo ] ; then
    bar
  fi
to

  if [ foo ]
  then
    bar
  fi
And also format your example to

  foo
  bar
In this type of situation, it becomes a little trickier to disambiguate when I need to add semicolons and a backslash, and when I need to add only backslashes. If you use `&&` -- you have disambiguated the two cases so I can format it.

[1] https://github.com/mvdan/sh

[+] urxvtcd|11 months ago|reply
I'd love to indent the body of each stage in multi-stage dockerfiles, like:

    FROM foo
        ...

    FROM bar
        ...
It's easy to see at glance what's going on.
[+] xyst|11 months ago|reply
This reminds me of SQL indentation “best practices” discussion [1]

I personally don’t find this particularly helpful but can see it helping some folks. You write enough dockerfiles, the formatting becomes irrelevant.

What pisses me off though is _inconsistency_. One code base uses "formatting practices 1b", then another code base uses "formatting practices 2x". Then the worst offender: a service owners that can’t make up their mind and each developer makes up their own "best practices".

[1] https://stackoverflow.com/questions/272210/sql-statement-ind...

[+] xyst|11 months ago|reply
It’s wild that this has to be a third party offering.

I wish projects would adopt/create their own formatters like go and rust.

The amount of time I have wasted discussing "best practices" with "senior" engineers is way too damn high.

In code bases such as go or rust, the discussion ends with the "the built in formatter and preferences is preferred"

[+] nikeee|11 months ago|reply
How does it handle multi-stage Dockerfiles? I always indent the steps following FROM to make the stages more obvious. I don't get why that isn't a norm because not doing it seems like not indenting function bodies in other languages.
[+] righthand|11 months ago|reply
I just use a yaml LSP which will probably try to lookup the schema.org Containerfile format for this. I first noticed this recently when working on a Github Actions yaml file. Pretty nifty.
[+] whalesalad|11 months ago|reply
But a dockerfile is not yaml. do you mean for docker compose?
[+] travisgriggs|11 months ago|reply
I hope there’s a config file for dockerfmt. Over time, it will get more and more options. It will approach Turing completeness.

Then we’ll need a formatter formatter.

Software is like onions said Shrek to Donkey.

[+] dddw|11 months ago|reply
This wouldve come in hansy yesterday
[+] PantaloonFlames|11 months ago|reply
Side question. Why would people continue to build new dockerfiles as opposed to using Podman ?
[+] DazWilkin|11 months ago|reply
Podman uses Dockerfiles too.

Dockerfiles is the language for specifying a set of instructions for building container images.

[+] jahsome|11 months ago|reply
Why would people continue to use hackernews as opposed to reddit?
[+] brynary|11 months ago|reply
It's great to see auto-formatting continuing to become universal across all languages. As LLMs write more code, full auto-formatting helps keep diffs clean.

For anyone looking to try dockerfmt, I just added a plugin to Qlty CLI, which is available in v0.508.0. The plugin took about ten minutes to add: https://github.com/qltysh/qlty/blob/main/qlty-plugins/plugin...

Full disclosure: I'm the founder of Qlty, which produces a universal code linter and formatter, Qlty CLI (https://github.com/qltysh/qlty). It is completely free and published under a Fair Source license.

[+] keybored|11 months ago|reply
> As LLMs write more code, full auto-formatting helps keep diffs clean.

Clean diffs matter irrespective of the author being a person or a program. But sure, I guess with the current hype a certain ratio of comments need to plug reminders that we are currently living in a code generation wasteland.