6 ms·
Dockerfmt: A Dockerfile Formatter
- PantaloonFlames 1y agoSide question. Why would people continue to build new dockerfiles as opposed to using Podman ?
- DazWilkin 1y agoPodman uses Dockerfiles too. Dockerfiles is the language for specifying a set of instructions for building container images.
- halostatue 1y agoIt's not a very good one, but it is ubiquitous (it's also better if you remember to include the right syntax marker that enables heredocs): # syntax=docker/dockerfile:1 I suspect that the GP was really asking "why not use a different tool", like buildah <https://buildah.io https://buildah.io>, buildpacks <https://buildpacks.io https://buildpacks.io>, nix <https://nix.dev/tutorials/nixos/building-and-running-docker-images.html https://nix.dev/tutorials/nixos/building-and-running-docker-...>, kaniko <https://github.com/GoogleContainerTools/kaniko https://github.com/GoogleContainerTools/kaniko>, ko <https://github.com/ko-build/ko https://github.com/ko-build/ko>, bazel <https://github.com/bazel-contrib/rules_oci https://github.com/bazel-contrib/rules_oci>, apko <https://github.com/chainguard-dev/apko https://github.com/chainguard-dev/apko>, or other tools. Each of those has tradeoffs compared to Dockerfiles (I have no need for bazel, but if I did, then adding `rules_oci` might be a win-win, rather than using a Dockerfile). If I used Nix, then the Nix dockerTools would be a huge win (I don't use Nix). If I were shipping Go programs, `ko` would likely be a good baseline.
- colonial 1y agoBuildah is the only serious alternative in my opinion. You lose automatic layer caching, but in exchange you can use the same tools (RUN, ADD, etc) within a much more powerful shell environment. I wrote a Buildah wrapper that uses a shell script harness to polyfill the familiar Dockerfile syntax while adding several extra goodies - mainly the ability to bake runtime arguments (mounts, ports...) into the image. Very handy!
- mikepurvis 1y agoBuildah's ability to mount the container in an unshare environment is pretty magical for copying stuff in and out of it. That said, in the end I'd still rather build containers with something other than an imperative sequence of commands, so my heart is going to be forever with nix2container and bazel's rules_oci.
- yjftsjthsd-h 1y agoWhen I've used buildah and kaniko, I still handed them a Dockerfile.
- jahsome 1y agoWhy would people continue to use hackernews as opposed to reddit?
- bpiroman 1y agoI feel dumber after using reddit
- acidmath 1y ago[dead]
- imp0cat 1y agoThe same is usually said about watching Spongebob Squarepants, but YMMV. :)
- chuckadams 1y agoAt least with Spongebob you feel dumb and happy. Feels like the whole world is dumb and angry now.
- dharmab 1y agoDockerfile has been standardized as Containerfile: https://github.com/containers/common/blob/main/docs/Containerfile.5.md https://github.com/containers/common/blob/main/docs/Containe...
- caleblloyd 1y agoDockerfile seems very specific to Docker still, I don’t think they have to go through a committee to add anything to the spec https://docs.docker.com/build/buildkit/dockerfile-release-notes/ https://docs.docker.com/build/buildkit/dockerfile-release-no... If any organization adopted the spec I would hope they would at least make it adopt a standard file extension like .oci so it would at least be more easily recognizable by IDEs, I have never liked having to put the use case as the extension like Dockerfile.dev But I do like that docker and buildkit have been able to freely evolve the spec with things like advanced caching directives that work great in buildx
- righthand 1y agoI 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 1y agoBut a dockerfile is not yaml. do you mean for docker compose?
- righthand 1y agoYou’re right. I’m sure there is a containerfile lsp however. https://github.com/sublimelsp/LSP-dockerfile https://github.com/sublimelsp/LSP-dockerfile
- sandeepgogarla 1y ago[flagged]
- jensenbox 1y agoI 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.
- klysm 1y agoThe project should certainly also be formatting its own docker file via a docker invocation
- thedougd 1y agoAnd built in a multistage Dockerfile.
- spicypete 1y agoHi there — I’ll try to distribute a docker release of the binary tomorrow!
- IshKebab 1y agoI think they were asking for a sample Dockerfile in the repo to test it on.
- spicypete 1y agoThen they can use one of the 14 in the `tests` directory :) https://github.com/reteps/dockerfmt/tree/main/tests https://github.com/reteps/dockerfmt/tree/main/tests
- ithkuil 1y agoI suggest you build it with https://github.com/ko-build/ko https://github.com/ko-build/ko so you can still have no dockerfile and the irony is not ruined
- PhilippGille 1y ago> 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.
- brynary 1y agoIt'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/plugins/linters/dockerfmt/plugin.toml 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 https://github.com/qltysh/qlty). It is completely free and published under a Fair Source license.
- keybored 1y ago> 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.
- mdaniel 1y agowaaaat? https://github.com/reteps/dockerfmt#:~:text=The%20RUN%20parser%20currently%20doesn%27t%20support%20grouping%20or%20semicolons%20in%20commands 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 1y agoIs there any reason you prefer `set -e` over `&&`? I'm curious if this is a readability thing.
- figmert 1y agoI'm firmly in that camp but I also always add `set -eux`, which makes it so much better at debugging as that gives you individual commands it runs before the output of them.
- figmert 1y agoTo be clear, the difference is something along this line: $ bash -ec 'echo hello && ls -la /tmp/ | grep systemd && false && echo testing' hello drwx------. 3 root root 60 Mar 29 18:33 systemd-private-bluetooth.service-yuSMVM drwx------. 3 root root 60 Mar 29 18:33 systemd-private-upower.service-YhHHP2 versus $ bash -euxc 'echo hello; ls -la /tmp/ | grep systemd; false; echo testing' + echo hello hello + ls -la /tmp/ + grep systemd drwx------. 3 root root 60 Mar 29 18:33 systemd-private-bluetooth.service-yuSMVM drwx------. 3 root root 60 Mar 29 18:33 systemd-private-upower.service-YhHHP2 + false Docker also supports the `SHELL` syntax now, which is even better, because you can set it once at the top of the Dockerfile without having to do the whole `set -eux` on every line.
- mdaniel 1y agoReadability is putting it mildly; do you write your shell scripts using that && style? No? Why not, is it for readability? I also have a hard time reasoning about && with anything other than the most braindead sequence of commands because: $(thing && if other_thing; then inner_thing1 && thing2; fi && ohgawd) And I just realized while typing that out that if its parser doesn't support ; then I guess one needs to RUN if conditional_thing \ then good_luck && \ fi && \ echo "whew"
- dddw 1y agoThis wouldve come in hansy yesterday
- yjftsjthsd-h 1y ago> 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 ; \ :
- deleted 1y ago[deleted]
- mcstafford 1y agoI prefer heredoc[1] syntax. I find it more readable and portable. [1] https://www.docker.com/blog/introduction-to-heredocs-in-dockerfiles/ https://www.docker.com/blog/introduction-to-heredocs-in-dock...
- yjftsjthsd-h 1y agoMeta: In HN, prefix a line with 2 spaces to get code formatting, ex. # syntax=docker/dockerfile:1.3-labs FROM alpine RUN <<EOF echo "This is a heredoc example" echo "It allows multi-line commands" EOF Non-meta: Do you happen to know how portable that is across old docker, podman/buildah, kaniko, etc.? I'd like to adopt it but I don't want it to bite me when I'm not running a recent version of literal docker.
- vbezhenar 1y agoIt is new feature and not portable to old versions. But modern podman supports it. No idea about kaniko.
- spicypete 1y agoI 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 https://github.com/mvdan/sh
- QuarterDuplex 1y agoOK but is there any way to layer docker files? I'm not talking about compose, I mean like combining layers from different dockers in a nice way?
- QuarterDuplex 1y agoor more like combining steps without explicitly having to combine layers?
- brigandish 1y agoThat sounds like multi-stage builds https://docs.docker.com/build/building/multi-stage/ https://docs.docker.com/build/building/multi-stage/
- nikeee 1y agoHow 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.
- ltbarcly3 1y ago[flagged]
- spicypete 1y agoIn the spirit of constructive feedback, I filed an issue for you: https://github.com/reteps/dockerfmt/issues/18 https://github.com/reteps/dockerfmt/issues/18 I fixed this in the 0.2.8 release https://github.com/reteps/dockerfmt/releases/tag/0.2.8 https://github.com/reteps/dockerfmt/releases/tag/0.2.8. Relevant reading about the `&&` normalization. https://pkg.go.dev/encoding/json#Marshal https://pkg.go.dev/encoding/json#Marshal
- ltbarcly3 1y agoIt's still wrong, and I don't know how I could explain it more completely: -ENTRYPOINT service ssh restart && bash +ENTRYPOINT ["service", "ssh", "restart", "&&", "bash"] You should read and understand the documentation for the containerfile format before continuing. This isn't some quirk because i'm using &&, you are breaking almost every dockerfile that doesn't use the array syntax for ENTRYPOINT.
- urxvtcd 1y agoI'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 1y agoThis 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-indentation-good-practice https://stackoverflow.com/questions/272210/sql-statement-ind...
- xyst 1y agoIt’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"
- grandempire 1y agoYou 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 1y ago> Who formats the formatter configs? Other formatters of course since the configs are often yaml, toml, ini or json.
- travisgriggs 1y agoI 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.
- mdaniel 1y agoI don't want to vouch for the flagged and dead comment https://news.ycombinator.com/item?id=43630653 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