17 ms·
Bocker – Docker implemented in around 100 lines of Bash (2015)
- soygul 7y agoQuoting from the source code: [[ $# -gt 0 ]] && while [ "${1:0:2}" == '--' ]; do OPTION=${1:2}; [[ $OPTION =~ = ]] && declare "BOCKER_${OPTION/=*/}=${OPTION/*=/}" || declare "BOCKER_${OPTION}=x"; shift; done If the ambition is to write lines like this, you can make it into ~1 line of code.
- lifthrasiir 7y agoThat roughly corresponds to the following C-like pseudocode: for (i = 0; i < argc && (arg = argv[i]).startswith("--"); ++i) if (sep = arg.find("=")) opts[arg[..sep]] = arg[sep+1..]; else opts[arg] = "x"; It might be too much for one line, but it doesn't seem to be super complicated either. In fact that line is (as of now, commit 0006330) only the second longest in that file.
- deleted 7y ago[deleted]
- oblio 7y agoI'd definitely refactor your C-like pseudocode to multiple lines :-D
- lifthrasiir 7y agoOf course, in real projects I don't write such codes. But that's not because it is harder to read, but because it is harder to alter; say, if you have another condition for say `-v` vs. `--verbose` then you would have to reformat it to multiple lines. The meaning of the quoted line is not hard to figure out if you know a bit of bash, and that's all I would expect from a showcase.
- kubanczyk 7y agoOf course what you fight for is readability and only readability. Especially in large projects. There will be literally 10 man-hours spent on reading this for each 1 man-hour of reading-followed-by-altering-it.
- boobsbr 7y agoI think a perl expert can write it even shorter.
- bliss 7y agoI love perl because it's fast, but it definitely follows the WORN pattern. Write Once Read Never. No matter how many commnets I leave for future me, it's always a voyage of discovery reading my own code, forget reading anyone else's :)
- etripe 7y agoIf that's really what you think and you think it's down to the language, look up Modern::Perl (0) and, if you're object inclinded, Moo (1), Mouse (2) or Moose (3). Then there's always perlcritic (4), too. In my mind, it's down to it having been popular, attracting many amateurs. You'll find equally unmaintainable bash or PowerShell scripts and definitely as much garbage, WORN JavaScript code. Writing maintainable code requires both knowledge and effort. [0]: https://metacpan.org/pod/Modern::Perl https://metacpan.org/pod/Modern::Perl [1]: https://metacpan.org/pod/Moo https://metacpan.org/pod/Moo [2]: https://metacpan.org/pod/Mouse https://metacpan.org/pod/Mouse [3]: https://metacpan.org/pod/Moose https://metacpan.org/pod/Moose [4]: https://metacpan.org/pod/perlcritic https://metacpan.org/pod/perlcritic
- arkadiytehgraet 7y agoALL of the listed above libraries are complete garbage, impossible to use productively among several developers. I worked in a company that had huge Perl codebase, which made extensive use of the Moose library. After trying to make sense of it, I gave up and used plain Perl, writing it as unidiomatic and simple as possible, so that hundreds of other devs, also new to Perl, would be able to understand the code I wrote. This was the common sentiment - most of the people followed the same path. The library is just a nightmare - Perl is dynamically typed, there is NO adequate IDE support (compared to the one statically typed languages have), so good luck with working out how the library works underneath. And if I cannot understand that, how on Earth will I understand what even my code is doing? (Never mind the others') In my mind, the amateurs are those that created the libraries without any idea on how they are going to be abused, thinking everyone should use unreadable incomprehensible syntax coupled with unapproachable internals. I apologize for the rant, I had no idea this topic moved me so much.
- bawolff 7y agoI dont even read or write bash shell scripts regularly and i can understand what that line is doing fine. I would not be able to understand if the entire thing was one line, so i think there is a significant difference. Just because something doesn't follow general rules for readable code, doesn't mean it is actually unreadable.
- eru 7y agoI don't think it's that hard to read, but breaking over multiple lines probably wouldn't hurt.
- viraptor 7y agoDo you actually know what it's doing, or are you guessing and inferring from patterns? There's a difference when you actually need to read the source for details rather than a quick skim. I can definitely guess the pattern and I do write bash regularly, but I see at least 2 things I'd need to double-check the man page for the behaviour.
- bawolff 7y agoIf you are asking if i have memorized bash syntax fully and know that everything he did was valid, than the answer is no. However the intent of the code and what each component is supposed to do is clear, which is what i'm looking for when reading code. Heck, by this definition i'm not sure any code is really good enough. In my job i have to write php daily, i still need to regularly look at docs to figure out order of arguments for various library functions. I wouldn't be able to tell if explode($a, $b) in php has the right argument order at a glance without looking it up. But i understand the intent and generally assume the superficial aspects of the code are right unless that part appears not to work. And furthermore, adjusting the number of newlines isn't going to help with the question of if that piece of code is using bash syntax correctly.
- organsnyder 7y agoI don't see anyone claiming that the syntax is incorrect. But it definitely causes me to have to stop and do some mental processing (mainly thinking about order of operations) to make sense of it.
- Xophmeister 7y agoThis isn't really that horrible; just condensing the boilerplate that comes with command line argument parsing. Sure it's quick-and-dirty, but it gets it done and moves focus your elsewhere. Interestingly, I didn't know you could do this without an `eval`: declare something${something_else}=whatever
- markhenrry 7y agohttps://www.printerrepairnearme.com/troubleshooting/how-to-setup-canon-mg3222-wifi/ https://www.printerrepairnearme.com/troubleshooting/how-to-s...
- phinnaeus 7y agoI do really like the implementation of help text: function bocker_help() { #HELP Display this message:\nBOCKER help sed -n "s/^.*#HELP\\s//p;" < "$1" | sed "s/\\\\n/\n\t/g;s/$/\n/;s!BOCKER!${1/!/\\!}!g" }
- _def 7y agoIt'd be even better if the help texts would be readable in the source, but that would cost some lines for sure
- phinnaeus 7y agoDo you just mean without the escaped newline characters? Because the fact that they serve as function comments is exactly what I found to be novel about it.
- _def 7y agoI like the idea too but just the thought of needing to go through those text blocks to edit them makes me sad
- vfinn 7y agoWhat do you mean? They are in the source.
- t0mek 7y agoInitially I thought that bash supports reflection and is able to get the function contents, including comments. But this function scans its own script file ($1), looking for the comments starting with #HELP and formatting them. This way the usage info can live near the functions implementing sub-commands.
- andreineculau 7y agoYup. It is a nice trick that you might find more often in Makefile. I have a make target that does this consistently in all the makefiles I write: https://github.com/andreineculau/core.inc.mk/blob/master/target.help.inc.mk https://github.com/andreineculau/core.inc.mk/blob/master/tar... target1: ## Do this target2: target3: target2 ## Do that would be nicely formatted as the output of `make help` into Available targets: target1 Do this target2 Do that and if you want to see all targets along with their origin file, even those without help messages, type `make help-all` to render Available targets: target1 path/to/Makefile Do this target2 path/to/Makefile target3 path/to/Makefile Do that
- sixstringtheory 7y agoNo doubt this is cool and represents good work! Nice job! Can we really say it’s a reimplementation of Docker in 100 lines, though, when it requires other dependencies that probably total in the hundreds of thousands of lines? That’s still code that has to be audited by people so inclined. Not to mention the other setup specified in the readme and maybe having to build one of the dependencies from source. Usage doesn’t sound necessarily trivial. Makes me appreciate Docker that much more though. Getting up to speed using it at my job and it certainly abstracts away many things, simplifying our workflow and conceptual models.
- beagle3 7y agoDocker also depends on hundreds of thousands on lines as well. In fact, to run it on Windows, it requires both Windows and Linux as dependencies /s More seriously - it’s not a complete docket of course, but lines of code in a project are a liability, not an asset. If you can reduce your project size with reasonable dependencies, you should.
- heavenlyblue 7y agoThe fact that you’re using it at your job doesn’t make it a good product
- IMTDb 7y agoBut the fact that for they use case it "abstracts away many things, simplifying [their] workflow and conceptual models" Certainly makes the technology interesting to investigate if it could be a fit for you as well.
- heavenlyblue 7y agoWell, the fact is that those “abstracts away stuff” are actually a bunch of different products on the market and Docker is probably the worst one.
- brianpgordon 7y agoI think the idea is to show how much container functionality is actually available out-of-the-box from the operating system. It raises questions about Docker's billion dollar valuation when someone can come along and reproduce a bunch of it with a short shell script. Next someone needs to take on orchestration and show how silly it was for IBM to pay $34 billion for OpenShift. :P
- mirimir 7y agoCute. For me, the hardest thing about Docker was running my own registries. Or rather, configuring credentials so Docker would use them. Not to mention the hassle of doing that through Tor. So maybe Bocker isn't so picky about registries.
- lbj 7y agoWell, it definitely got a chuckle out of me. Good job!
- jerome-jh 7y ago"You won't believe line 4"
- zelly 7y agoI was thinking about something like this for build systems. Everything in Docker is regular Linux. I get why Docker is so big for its use case as cloud deployments, but what I actually want from it is such a small piece of it. Hermetic, reproducible builds that produce the same binary on the same kernel release. No dependency hell because the dependencies are all part of the same build. (I know Bazel has a lot of this already.) The Docker solution of pulling in an entire distro is overkill, and it doesn't even solve the problem because dependencies are just downloaded from the package manager.
- adgasf 7y ago> it doesn't even solve the problem because dependencies are just downloaded from the package manager. The advantage of Docker is that you can verify the container works locally as part of the build process rather than finding out it is broken due to some missing dep after a deployment. If you can verify that the image works then the mechanism for fetching the deps can be as scrappy as you like. Docker moves the dependency challenge from deployment-time to build-time.
- crdrost 7y agoDoes container mean something different to y’all than it does to me? I ask because I read your comment as saying “the advantage of Docker is that it uses (explanation of what containers are)” and the parent comment as saying “all I want from Docker is (explanation of what containers are)” and I am confused why (a) y’all are not just saying “containers” but rather “the part of docker that packages up my network of scripts so I can think about it like a statically linked binary” and (b) why you think this is a competitive advantage over other things you might have instead recommended here (Buildah, Makisu, BuildKit, img, Bazel, FTL, Ansible Container, Metaparticle... I am sure there are at least a dozen) to satisfy the parent comment’s needs. Is there really any container ecosystem which has write-an-image-but-you-can’t-run-it-locally semantics? How do you finally run that image?
- zelly 7y agoDocker is too general, too much of a Swiss army knife for this particular problem. The problem I am talking about is where a C++ program has all of its dependencies vendored into the source tree. When you run Make, everything including the dependencies build at the same time. All you need is a chroot, namespaces, cgroups, btrfs, squashfs--plain old Linux APIs--to make sure the compiler has a consistent view of the system. Assuming the compiler and filesystem are well behaved (e.g., don't insert timestamps), you should be able to take a consistent sha256sum of the build. And maybe even ZIP it up like a JAR and pass around a lightweight, source-only file that can compile and run (without a network connection) on other computers with the same kernel version. Again, Bazel is basically this already. But it would be nice to have something like OP's tool to integrate in other build systems. I could just make a Dockerfile and say that's my build system. But then I'm stuck with Docker. The only way to run my program would be through Docker. Docker doesn't have a monopoly on the idea of a fully-realized chroot.
- ponytech 7y agoShouldn't it be mentioned this is from 2015?
- dang 7y agoYes, and now it is. Thanks!
- maitredusoi 7y agoI am wondering if the same was done in ruby, what it would like like
- logophobia 7y agoHuh, clever use of btrfs volumes, it does make it a little dependent on the filesystem though. Quite informative overall, probably implements only a fraction of docker, but it does do most of what you'd typically need.
- tluyben2 7y agoUntil well into the 2000s we deployed all our services as chroots. I even sold a server side product successfully which was distributed as chroot so it would install almost on any Linux distro without having to care about dependencies. It worked really well and felt quite a bit more accessible and lighter than docker. We just had a script which basically did: wget https://bla/image.tgz tar xzf image.tgz rm image.tgz chroot image/start and the product would be installed and up & running. A lot of clients thought it was magic. No Apache install? No MySQL install?? It works on Fedora & Centos & Debian & Ubuntu & ... ? I use this currently still a lot to run new software on unsupported hardware/kernels. For instance, I run a lot of new (Ubuntu) software on my OpenPandora under the default OS on that device; I have an SD with a chroot with Ubuntu and things like dotnet core. Runs fine. Gives devices like that a second youth.
- somesortofsystm 7y agoISP's all over the valley did this in the 80's and early 90's as a way of supporting multiuser systems .. We should never forget that at the root of containerization is a desire to keep everyones shit sorted. That's been a Unix thing forever. EDIT: "Gives devices like that a second youth." Hell yeah, long live the OP!
- lukevp 7y agoAnyone interested in buying a first batch OP? I ordered it early in the process and then didn’t end up using it much
- somesortofsystm 7y agoStill got my two first-batch OP's, keeping an eye on the Pyra situation, but oh .. what a ride its been ..
- tluyben2 7y agoYeah, waiting for the Pyra as well. I got to test the Gemini which was crap (imho), the Pocket 1 (excellent machine imho), Pocket 2 (no nub and thus, quite shit; worse battery life too). I only bought the Pocket 1 because I liked it so much when testing, but the OpenPandora is still really one of my favorite machines for one the go hacking because I can just hold it in my hands instead of having to put it down on a surface.
- adgasf 7y agoDocker is designed to push you towards DockerHub, not to be the simplest or best container implementation.
- vpEfljFL 7y agoWhy do you think it's not a good practice? Docker is a good tool and deserves some resources to continue developing the product and funding marketing team. I wish docker hub to be more competitive among other rivals.
- adgasf 7y ago> Why do you think it's not a good practice? I didn't say it wasn't. Docker makes sense because everyone uses it. However, I would prefer a Docker alternative that is purely local and doesn't require so many permissions.
- my123 7y agopodman is one, and it can even work just fine without root permissions.
- MadWombat 7y agoReally? How can it run a web server container, for example, without root permissions? You need root to listen on port 80.
- takeda 7y agoWell, isn't that a specific case though? From my experience most containerized apps use higher port. In FreeBSD you would be able to also remove such restrictions if needed (not sure if something is also available on Linux) alternatively you could have your app listening on a higher port and use iptables to forward port 80 there.
- 7y ago
- nickjj 7y agoThe real value in Docker isn't process isolation at a low level. It's being a toolset to build and deploy containerized applications. In other words, being able to docker-compose up --build on any Linux, Windows and MacOS box is what's useful.
- crdoconnor 7y agodocker compose is the tool that winds me up the most.
- gureddio 7y agoMay I ask why?
- crdoconnor 7y agohttps://docs.docker.com/compose/startup-order/ https://docs.docker.com/compose/startup-order/ >The problem of waiting for a database (for example) to be ready is really just a subset of a much larger problem of distributed systems. In production, your database could become unavailable or move hosts at any time. Your application needs to be resilient to these types of failures. absolute bullshit. docker compose thinks that it can excuse its bugs by dint of the fact that we're supposed to build "more resilient" applications to accommodate them. and, their proposed workaround with "wait for" is disgusting. their tool should be able to handle readiness detection. it's so fucking basic. it's not only this but this is an example of the bullshit in this shitty tool excused with shitty reasons.
- nickjj 7y ago> my test of "does my application work given that the database is running" is explicitly not accommodated. Are you working with tools where this is a problem in practice? Most web frameworks I've used will keep trying to connect to the DB in a loop until it either connects and things start normally, or it times out after X amount of seconds where X by default is configured to some number that's way higher than it would normally take for your DB to be available, even if it's starting from scratch with no existing volume. No "wait for it" script needed (and I do agree that type of solution is very hacky). Although to be fair the same page you quoted said the best solution is to handle this at the app level, which is what most web frameworks do.
- sub7 7y agoContainerization makes sense for a tiny, tiny fraction of all products. It's just been overblown and overused, creating another point of potential failure for no great benefit at all. Just chroot the damn thing if you need to and keep it simple.
- pantulis 7y agoNow I only need someone to write Kubernetes in 500 lines of Bash.
- songzme 7y agoI am so interested in this, but there are so many things I don't understand. I wonder.... is there a service (Maybe a chrome extension) that lets you ask questions on the page you are in? For example, I want to highlight: `btrfs-progs` and ask... "What is a quick summary about this package? `ship a new enough version of util-linux` and ask.... "What features in the new util-linux allows this to work?" Then maybe someone who also visits this page with more knowledge can answer my questions. This tool would augment my learning 10x and I would be a paying customer. Does something like this exist?
- tmcb 7y agoLet me know if that is what you are looking for: https://web.hypothes.is/ https://web.hypothes.is/ As an example, WikiWikiWeb (http://wiki.c2.com/ http://wiki.c2.com/) has integration with it. I've never used it, but it seems quite powerful.
- songzme 7y agoWow thanks! Set this up smoothly and asked the questions I posted above. Will reach out to friends and start a community. Thank you for the find!
- zantana 7y agoThis bubbled up the other day which was interesting as well. https://blog.alexellis.io/building-containers-without-docker/ https://blog.alexellis.io/building-containers-without-docker... It does seem like Docker will be holding back containers from achieving their true promise, as it flounders looking for profitability and chasing k8s. Its sad that we're still moving around tar balls without advancing some of the tooling around it. One of my biggest gripes as a non-k8s container user are that we are still keeping all this versioned cruft with every container. I would like to see an easier way to just get the latest binaries not dozens of tar hashes with versions going back to the first iteration of the container. Probably closer to singularity https://singularity.lbl.gov/ https://singularity.lbl.gov/ Another area is the ability to update containers in place. Hass.io https://www.home-assistant.io/hassio/ https://www.home-assistant.io/hassio/ does some of this with the ability to manage the container version from within the container, but seems like a little bit of a hack from what I've seen. Watchtower is closer to what I'd like https://github.com/containrrr/watchtower https://github.com/containrrr/watchtower, but why can't we have a more integrated system built into the default tooling?
- efrecon 7y agoI have successfully used code built around my gist at https://gist.github.com/efrecon/8ce9c75d518b6eb863f667442d7bc679 https://gist.github.com/efrecon/8ce9c75d518b6eb863f667442d7b... to recreate containers that have the identical set of original options, but with a newer image.
- killbot5000 7y agojust wait until you see how easy it is with bocker_compose
- bunchOfCucks 7y agoBut it doesn’t have a cool logo
- yepthatsreality 7y agoThis was posted after a recent repost of another implementation in ~500 lines of code. See other HN discussion here: https://news.ycombinator.com/item?id=22232705 https://news.ycombinator.com/item?id=22232705
- thebeardisred 7y agoFun anecdote: Early in the lifespan of CoreOS (early 2014 IIRC) I was meeting with technology leadership at a Fortune 500 customer. I remember the head of developer services asking all kinds of Docker questions. Finally, he was cut off by his boss who said: "Bob¹*, unless we've solely staffed the organization with folks from coder camps a lot of the folks here should be able to carve out a couple of namespaces in the kernel. That's not interesting and that's not why we asked CoreOS to come in today. Let's talk about the actual problems they're trying to solve." <3 <3 <3 <3 That being said, it's great that folks continue to dig in to decompose the tooling and show what's actually happening behind the scenes. ¹Bob was definitely not that guy's name. EDIT: Added note of context appreciating the work by the author.
- gauthamzz 7y agoDocker also implemented using 27 lines of python. https://github.com/Zakaria-Ben/Pocker/blob/master/Pocker.py https://github.com/Zakaria-Ben/Pocker/blob/master/Pocker.py
- RocketSyntax 7y ago`entrypoint` directory is critical feature
- dang 7y agoDiscussed at the time: https://news.ycombinator.com/item?id=9925896 https://news.ycombinator.com/item?id=9925896
- deleted 7y ago[deleted]