News for hackers. Stuff that matters.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by paulfurtado on Thursday April 22, 2021 @02:55AM (#26899607)
FWIW, if you wanted to get the static from-scratch go image a lot smaller, try running the binary through UPX. It typically improves compiled go binary sizes by absurd amounts: https://upx.github.io/

Also, https://busybox.net is a 1.0MB static binary that provides all of the standard posix-compliant tools (sh, grep, tail, etc) and actually comes with an httpd implementation. So if you wanted a more useful container at about 1MB, you could use it.

Edit: here's a previous thread on UPX that has 80 comments talking about the tradeoffs it makes. You should definitely consider these if you intend to use UPX: https://news.ycombinator.com/item?id=15456980

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by TonyTrapp on Thursday April 22, 2021 @04:10AM (#26900135)
Keep in mind that UPX needs to decompress the whole executable to memory and thus cannot make use of OS-level memory sharing. Normally, when you load the same executable twice, its base image will only consume memory once. But it with UPX, you cannot make use of that. So in the end, it may use more resources than you thought!
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by dividuum on Thursday April 22, 2021 @06:09AM (#26900925)
I was initially a bit skeptical if the page cache would work with all layering docker does. But OverlayFS, perhaps unsurprisingly, does indeed support that: [1] "OverlayFS supports page cache sharing. Multiple containers accessing the same file share a single page cache entry for that file. This makes the overlay and overlay2 drivers efficient with memory and a good option for high-density use cases such as PaaS." TIL

[1] https://docs.docker.com/storage/storagedriver/overlayfs-driv...

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by Hackbraten on Thursday April 22, 2021 @11:28AM (#26903843)
UPX creates anonymous pages that contain the decompressed payload, right? Those aren’t file-backed pages so how would the filesystem even know those pages exist?
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by dividuum on Thursday April 22, 2021 @01:19PM (#26905275)
That’s correct and I guess only page deduplication might help in that case. What I meant in my post was just to state that docker can use the page cache despite the massive indirection caused by overlays. I was wondering if that was even possible and it indeed is.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by amscanne on Thursday April 22, 2021 @11:25AM (#26903803)
The base would consume “up to once”. You will only load pages touched, and they are all reclaimable under memory pressure. Memory used by UPX for decompression is guaranteed to be used, and is not reclaimable!

So in fact, it’s not might use more resources, it’s guaranteed to use more resources.

The only benefit of UPX is saving disk space, which is unlikely to be super useful to most people these days.

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by paulfurtado on Thursday April 22, 2021 @12:34PM (#26904725)
The other use case is to distribute images more quickly over the network and improve image pull times, at the expense of slightly more memory use.

Similar to how saving disk is less useful to most people these days, RAM on servers in big docker/kubernetes clusters is also likely not much of a concern for binaries that are well under 100MB if you're not running a ton of copies on each server. In particular, it seems like this tradeoff is most useful for static go binaries, which don't benefit from shared system libraries and aren't likely to have many copies per server, but of course that definitely depends on the use case and I definitely do not recommend blindly using UPX without considering the tradeoffs.

It would be an amazing integration though if it were possible to integrate UPX with docker's layer compression such that the binary is extracted to the FS before running. But given how it all works, that's pretty challenging to implement and the network is only getting faster...

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by shawnz on Thursday April 22, 2021 @02:08PM (#26905914)
Are you sure UPX actually beats the standard layer compression which is already provided?

After applying UPX, the result should be nearly uncompressable, so if UPX performs worse than layer compression then it might be better for network usage to not use it at all.

In this scenario where you are unpacking it in advance, that eliminates the main benefit of UPX which is that it is self-extracting.

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by paulfurtado on Thursday April 22, 2021 @04:47AM (#26900374)
Definitely important to note, I wonder if upx could solve this via execing an memfd_create in some mode. Also wish upx came with a trivial extraction tool so you could use it for distribution but not runtime.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by mikepurvis on Thursday April 22, 2021 @09:03AM (#26902263)
Aren’t your container layers compressed in transit anyway?
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by paulfurtado on Thursday April 22, 2021 @10:41AM (#26903295)
UPX does significantly more compression on binaries than gzip can on the layers. It's not just simple compression, it makes a lot of modifications to the binary to make it more compressible.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by mikepurvis on Thursday April 22, 2021 @08:58PM (#26909923)
Perhaps this is what you were asking for in the GP, but it would be interesting then if UPX could apply those transformations without actually doing the final compression step itself. Perhaps the win isn't as great without the ability to smuggle specific hints to the compressor, but it would be interesting to see.

On the other hand, if it could be demonstrated that modest increases to binary size correlated to significantly improved compressability, then maybe that's something that should just be considered by the compilers themselves.

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by synergy20 on Thursday April 22, 2021 @10:41AM (#26903285)
that's true, plus on low-end devices upx will take forever to decompress and is essentially unusable.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by paulfurtado on Thursday April 22, 2021 @12:36PM (#26904748)
Is UPX actually that much slower than extracting a gzipped docker layer? I didn't realize UPX decompression was much slower than other compression algorithms
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by codetrotter on Thursday April 22, 2021 @03:25AM (#26899798)
Beware that at least historically some antivirus software would wrongly classify any UPX-packed binary as malware. Don’t know if this is still the case but something to keep in mind.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by account42 on Thursday April 22, 2021 @08:46AM (#26902108)
"some antivirus software would wrongly classify X as malware" is true for any X (except malware, then would not be wrong)
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by okamiueru on Thursday April 22, 2021 @04:57AM (#26900440)
I did exactly this a few years back.

It added 18.5 kB on top of busybox, so, not as good as the 6kB ASM server code. With busybox included, the whole image is 1.18 MiB, or 751 kB compressed.

If anyone cares to take a look: https://hub.docker.com/r/swalog/tiny-hello/

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by spalas on Thursday April 22, 2021 @10:34AM (#26903211)
Hi, author of the article here!

I figured there were a bunch of ways to shrink that one, and I had a C based image that was in between my GO and ASM images, but decided to cut them for the sake of brevity!

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by dnautics on Thursday April 22, 2021 @04:27PM (#26907750)
stealing vexu's routez: https://github.com/Vexu/routez

gives you a multiprocess http server in 612K uncompressed if you compile -Drelease-small

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by dolmen on Thursday April 22, 2021 @08:47AM (#26902119)
About busybox: if you want a small container, you don't need the posix tools at all, but just your binary. No shell.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by daniellockard on Thursday April 22, 2021 @03:05AM (#26899681)
Be sure to check out the switches, --ultra-brute is what I usually use.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by endgame on Thursday April 22, 2021 @06:24AM (#26901065)
If you only want the sh, you can get it even smaller. This doesn't UPX the busybox bin, though:

$ ls -shL `nix-build -E '(import <nixpkgs> {}).pkgsCross.musl64.busybox.override { enableStatic = true; enableMinimal = true; }'`/bin/sh

128K /nix/store/qkq9cj81mcvnfr5y04jmf6nqvnrfg2z1-busybox-1.31.1-x86_64-unknown-linux-musl/bin/sh

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by fouc on Thursday April 22, 2021 @03:49AM (#26899979)
1.0MB is quite large compared to 6.34kB :)
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by paulfurtado on Thursday April 22, 2021 @04:43AM (#26900356)
It definitely is, although a cool thing about busybox is that you can rebuild it with any applets disabled. I'm not sure just how small it can get though.

But yeah, the tradeoff of that 1MB is that you get a very functional shell environment vs just an http server. It's impressive just how small ut is, given everything that it is capable of

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by shakna on Thursday April 22, 2021 @05:29AM (#26900633)
> It definitely is, although a cool thing about busybox is that you can rebuild it with any applets disabled. I'm not sure just how small it can get though.

They do distribute statically linked individual binaries [0]. For just HTTPD and nothing else is about 85kb. But it is a lot more than just a static file server - it comes with CGI support.

[0] https://www.busybox.net/downloads/binaries/1.30.0-i686/

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by js8 on Thursday April 22, 2021 @12:42PM (#26904824)
> But yeah, the tradeoff of that 1MB is that you get a very functional shell environment vs just an http server. It's impressive just how small ut is, given everything that it is capable of

Maybe.. I suspect a well-designed Forth image could pack similar capability in less than 100kB. So it might seem low, but not a miracle.

Re:Tiny Container Challenge: Building a 6kB Containerized HT... by whizzter on Thursday April 22, 2021 @04:37AM (#26900311)
Certainly, I think this is more about getting over the developer pain threshold. In terms of computer speed vs human perception I don't see either one mattering too much (compared to a 900meg base for node-debian on Docker, although I guess there is some incremental stuff for that?).
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by davidjfelix on Thursday April 22, 2021 @08:27AM (#26901938)
If you're going for size, why not just sidecar all of the "standard posix-compliant tools" but ones that you would want normally. For example, bash instead of ash/dash. You don't need those tools all the time right?
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by dolmen on Thursday April 22, 2021 @08:48AM (#26902129)
Well, you don't even need a shell.
Re:Tiny Container Challenge: Building a 6kB Containerized HT... by davidjfelix on Thursday April 22, 2021 @08:55AM (#26902186)
I mostly meant to sidecar the shell and treat it as a debugging utility. While I get the point of ash being smaller as a point for busybox, if size stops being a concern because the shell is in an inactive-sidecar, then you can use a shell that has less surprising edge cases for users, like bash.