TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Linux containers in a few lines of code

458 pointsby benjaminjosephwabout 5 years ago

18 comments

dmayleabout 5 years ago
A little bit of education about container systems in linux[1]. A container system is typically made up a number of components:<p><i>isolation layer</i>: the piece that limits privileges and resource usage. (On linux, this is usually handled by cgroups and the kernel, but could also be handled by something like kvm for vm-based containers)<p><i>raw container configuration</i>: Given an image and some metadata (like cpu limits), launch an isolated process. (On linux, this is usually handled by runc when working with cgroups)<p><i>container api daemon</i>: Manage the list of container processes and available images. Provide a unix socket based API for manipulating isolated processes, launching, deleting, connecting, etc. (In the case of docker, they provide a daemon which abstracts the containerd daemon, or you can use containerd alone without docker)<p><i>container command line tool</i>: Provide a user&#x2F;developer interface to the three things above. This is the docker command. When you install containerd without docker this is the ctr command.<p>Docker, which is probably the most famous container distribution, pairs the docker command with the docker daemon to abstract away the containerd daemon, runc, and cgroups.<p>If you use containerd alone, you get ctr&#x2F;containerd&#x2F;runc&#x2F;cgroups.<p>There&#x27;s a standalone command line tool (crictl) which replaces both ctr and docker and can be used on top of either the docker daemon or containerd.<p>[1] Container systems seem to have a relatively complex abstraction over what is a relatively simple architecture.
评论 #23167259 未加载
评论 #23171211 未加载
评论 #23172331 未加载
评论 #23173412 未加载
benjaminjosephwabout 5 years ago
I love minimal code like this as a way of really understanding how something works. The author is pretty consistent too - he&#x27;s got some great projects like an ultra-minimal electron alternative: <a href="https:&#x2F;&#x2F;github.com&#x2F;zserge&#x2F;webview" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;zserge&#x2F;webview</a>
评论 #23166312 未加载
mmastracabout 5 years ago
Big caution here. Do NOT use this style of code to invoke ip tools. This was the cause of a huge number of security vulnerabilities on Android in the first few years. Even if you&#x27;re hardcoding interfaces to start, it&#x27;s likely someone else will drive by later on and replace one of the args with %s.<p>&gt; system(&quot;ip link add veth0 type veth peer name veth1&quot;);<p>Always, always, always use exec*() APIs.
评论 #23171601 未加载
lidHanteykabout 5 years ago
Nice work. I&#x27;m reminded of bocker [0], which also implements this sort of functionality in only a few dozen lines of code. The function which corresponds to this post [1] is relatively short and readable.<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;p8952&#x2F;bocker" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;p8952&#x2F;bocker</a><p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;p8952&#x2F;bocker&#x2F;blob&#x2F;master&#x2F;bocker#L61-L90" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;p8952&#x2F;bocker&#x2F;blob&#x2F;master&#x2F;bocker#L61-L90</a>
fwipabout 5 years ago
Julia Evans has an excellent zine on how containers work, including a 15-line bash implementation: <a href="https:&#x2F;&#x2F;jvns.ca&#x2F;blog&#x2F;2020&#x2F;04&#x2F;27&#x2F;new-zine-how-containers-work&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jvns.ca&#x2F;blog&#x2F;2020&#x2F;04&#x2F;27&#x2F;new-zine-how-containers-work...</a><p>Definitely worth the $12.
评论 #23169551 未加载
ShorsHammerabout 5 years ago
DIY Containers on Linux is probably a better term here given that Linux Containers is already heavily in use around the world and included in ubuntu by Canonical?<p>For me this is enough to get a container running:<p><pre><code> lxd init lxc launch ubuntu mycontainer lxc exec mycontainer bash </code></pre> <a href="https:&#x2F;&#x2F;linuxcontainers.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;linuxcontainers.org&#x2F;</a>
评论 #23166132 未加载
评论 #23166410 未加载
mehrdadnabout 5 years ago
Could someone comment on how secure such a container is, at least nominally? Should I be able to theoretically run untrusted code on such a container if the system is bug-free and I add proper error-checking to the code? Or are there things that you&#x27;d need to worry about the code being able to access? Any considerations regarding sudo permissions?
评论 #23172262 未加载
评论 #23168566 未加载
评论 #23167865 未加载
评论 #23168583 未加载
评论 #23167904 未加载
评论 #23172242 未加载
notRobotabout 5 years ago
See also:<p>Linux containers in 500 lines of code (2016) <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22232705" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22232705</a>
bswaminaabout 5 years ago
A few weeks back had published on how to build a container in go programming ... In case interested here are the links:<p><a href="https:&#x2F;&#x2F;www.polarsparc.com&#x2F;xhtml&#x2F;Containers-1.html" rel="nofollow">https:&#x2F;&#x2F;www.polarsparc.com&#x2F;xhtml&#x2F;Containers-1.html</a> <a href="https:&#x2F;&#x2F;www.polarsparc.com&#x2F;xhtml&#x2F;Containers-2.html" rel="nofollow">https:&#x2F;&#x2F;www.polarsparc.com&#x2F;xhtml&#x2F;Containers-2.html</a>
davexunitabout 5 years ago
Small container implementation in Scheme: <a href="http:&#x2F;&#x2F;git.savannah.gnu.org&#x2F;cgit&#x2F;guix.git&#x2F;tree&#x2F;gnu&#x2F;build&#x2F;linux-container.scm" rel="nofollow">http:&#x2F;&#x2F;git.savannah.gnu.org&#x2F;cgit&#x2F;guix.git&#x2F;tree&#x2F;gnu&#x2F;build&#x2F;lin...</a>
accelbredabout 5 years ago
One thing to note, is that using a PID namespace in that way is incorrect. PID1 in a PID namespace has to perform the duties normally performed by a PID1, so you will normally want PID1 in the namespace to be a minimal init. If not, there may be issues, like unreaped zombie processes.
devxpyabout 5 years ago
While this is interesting, it doesn&#x27;t really show how containers <i>actually work</i>, only lists the specific syscall flags to <i>tell Linux create one</i>.<p>A similar snippet[1] exists for go, and it doesn&#x27;t do anything particularly special either.<p>I don&#x27;t know, maybe David beazley has altered my sense of what &quot;from scratch&quot; means.<p>[1] <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;lizrice&#x2F;a5ef4d175fd0cd3491c7e8d716826d27" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;lizrice&#x2F;a5ef4d175fd0cd3491c7e8d71682...</a>
评论 #23166575 未加载
p4bl0about 5 years ago
I love this kind of posts, it&#x27;s never something you will effectively use instead of the actual product (here, Docker), but it&#x27;s really a great way to learn new little things.
jeffbeeabout 5 years ago
Linux containers in one shell statement<p><pre><code> $ echo $$ &gt; tasks</code></pre>
评论 #23168686 未加载
streb-loabout 5 years ago
Anyone want to chime in on why pivot_root is preferable to a chroot jail? It&#x27;s kind of hand-waved in the article.
评论 #23172603 未加载
评论 #23167982 未加载
josteinkabout 5 years ago
The title is about <i>Docker</i> containers in a few lines of code.<p>Could the mods consider renaming the submission?
brauziabout 5 years ago
thanks for sharing!
jartabout 5 years ago
Software history should be written that Docker gave Phil Katz the Oliver Cromwell treatment.