TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: #!/usr/bin/env docker run

496 点作者 adtac超过 1 年前

27 条评论

pedrovhb超过 1 年前
For an actually intentional, non-cursed version of this, see the nix-shell shebang [0]:<p>&gt; #! &#x2F;usr&#x2F;bin&#x2F;env nix-shell &gt; #! nix-shell -i python3 -p python3Packages.pillow python3Packages.ansicolor &gt; &gt; # scale image by 50% &gt; import sys, PIL.Image, ansicolor &gt; path = sys.argv[1] &gt; image = PIL.Image.open(path) &gt; factor = 0.5 &gt; image = image.resize((round(image.width * factor), round(image.height * factor))) &gt; path = path + &quot;.s50.jpg&quot; &gt; image.save(path) &gt; print(ansicolor.green(f&quot;done {path}&quot;))<p>Just `chmod +x` and you have an executable with all dependencies you specify!<p>[0] <a href="https:&#x2F;&#x2F;nixos.wiki&#x2F;wiki&#x2F;Nix-shell_shebang" rel="nofollow">https:&#x2F;&#x2F;nixos.wiki&#x2F;wiki&#x2F;Nix-shell_shebang</a>
评论 #38991229 未加载
评论 #38993121 未加载
评论 #38989929 未加载
评论 #39000935 未加载
评论 #38989662 未加载
评论 #38996820 未加载
评论 #39023008 未加载
pushedx超过 1 年前
The readme compares it to the cross-architecture Cosmopolitan libc, but Docker is anything but cross-platform. On any other platform besides Linux it requires a Linux VM.<p>Linux containers are great (and I run Linux as my desktop OS), just pointing out the not-so-efficient nature of considering this cross-platform.
评论 #38987543 未加载
评论 #38987504 未加载
评论 #38988261 未加载
评论 #38991298 未加载
评论 #38987528 未加载
评论 #38988974 未加载
评论 #38988202 未加载
评论 #38987881 未加载
评论 #38988111 未加载
评论 #38987592 未加载
noname120超过 1 年前
The -S &#x2F; --split-string option[1] of &#x2F;usr&#x2F;bin&#x2F;env is a relatively recent addition to GNU Coreutils. It&#x27;s available starting from GNU Coreutils 8.30[2], released on 2018-07-01.<p>Beware of portability: it relies on a non-standard behavior from some operating systems. It only works on OSs that treat all the text after the first space as argument(s) to the shebanged executable; rather than just treating the whole string as an executable path (that can happen to contain spaces).<p>Fortunately this non-standard behavior is more the norm than the exception: it works at least on modern GNU&#x2F;Linux, BSDs, and macOS.<p>[1] <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;coreutils&#x2F;manual&#x2F;html_node&#x2F;env-invocation.html#g_t_002dS_002f_002d_002dsplit_002dstring-usage-in-scripts" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;coreutils&#x2F;manual&#x2F;html_node&#x2F;env-...</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;coreutils&#x2F;coreutils&#x2F;blob&#x2F;b09dc6306e7affaf002f67350b9787c550ddb5c2&#x2F;NEWS#L953-L955">https:&#x2F;&#x2F;github.com&#x2F;coreutils&#x2F;coreutils&#x2F;blob&#x2F;b09dc6306e7affaf...</a>
评论 #38989586 未加载
评论 #39010544 未加载
bionhoward超过 1 年前
This is genius and I love how this is a whole app meta-seed in a single file! I think I have docker trauma, why did we reach a point where we need computers inside our computers just for normal stuff to work?<p>Container packing is cool, but is it just a security thing preventing us from using our normal hardware? Or versioning (NixOS)? Is wasm capable of doing this and is wasm still alive? I just feel like needing to run tests inception style inside and outside docker gets complicated and annoying and always try to just use Linux directly these days.
评论 #38989289 未加载
评论 #38989888 未加载
评论 #38989842 未加载
throwaway892238超过 1 年前
Cute trick, but it&#x27;s not actually what the title claims.<p>Since this is actually <i>env</i> calling <i>bash</i> first, not docker, this should just be a Bash script. You can still feed the Dockerfile to <i>docker build</i> via STDIN. But you&#x27;d gain the ability to shellcheck the Bash, the code would be easier to read, write, maintain, add comments to, etc. You could keep the filename the same, run it the same way, etc. The way they&#x27;ve done it here is just unnecessarily difficult.
评论 #38987822 未加载
评论 #38988206 未加载
chubot超过 1 年前
Something like this should definitely exist, just not with Docker!<p>Podman is better but it&#x27;s also a bit coupled to a distro - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=38981844">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=38981844</a><p>The problem is the Linux kernel container primitives are a bit of a mess<p>bubblewrap is a lot closer, although last I heard it&#x27;s not in some distros for security reasons - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30823164">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=30823164</a>
评论 #38987908 未加载
评论 #38987886 未加载
评论 #38988601 未加载
kevincox超过 1 年前
This is cool hacking but I really don&#x27;t get this obsession with &quot;single file&quot;. Directories exist and can contain self-contained applications without the need to pack everything into some ugly script. They are into the slightest bit more difficult to ship around to different machines.
评论 #38990515 未加载
lwneal超过 1 年前
You can create this type of thing (a self-contained single-file project) for any language or infrastructure, with or without a clever shebang. All you need are heredocs.<p>For example, here&#x27;s the same app but packaged as a regular bash script:<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;lwneal&#x2F;a24ba363d9cc9f7a02282c3621afa43d" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;lwneal&#x2F;a24ba363d9cc9f7a02282c3621afa...</a>
评论 #38993247 未加载
评论 #38995202 未加载
评论 #38999481 未加载
jcul超过 1 年前
Reminds me of the &quot;self consuming script pattern&quot;. Seen in this super user answer.<p><a href="https:&#x2F;&#x2F;superuser.com&#x2F;a&#x2F;440059" rel="nofollow">https:&#x2F;&#x2F;superuser.com&#x2F;a&#x2F;440059</a><p>It embeds an awk (or any interpreter) script, and uses sed to cut out the script between tags in $0.<p>I agree with other comments that this kind of thing can get messy, but sometimes it makes a lot of sense and let&#x27;s you share a single file.
评论 #38990433 未加载
forrestthewoods超过 1 年前
Can someone explain what this is and what it does? I have no idea. I use Windows and have never needed to use Docker for anything.
评论 #38988509 未加载
评论 #38988442 未加载
rekado超过 1 年前
There&#x27;s also `guix shell` which can be used in shebang position. Example from the Guix manual:<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env -S guix shell python python-numpy -- python3 import numpy print(&quot;This is numpy&quot;, numpy.version.version) </code></pre> It also works with manifest files specifying more complex environments.
Kab1r超过 1 年前
This isn&#x27;t POSIX compliant is it? I feel like I tried to do something different but trying to put arguments in a shebang and ran into trouble there a year or two ago.
评论 #38987849 未加载
评论 #38989510 未加载
评论 #38988333 未加载
tzury超过 1 年前
As a rule, if you can write your code in a file, and run it as a script, it is better than writing scrolls between<p><pre><code> &lt;&lt;EOF … EOF </code></pre> The why is obvious.
评论 #38987419 未加载
评论 #38987438 未加载
评论 #38987605 未加载
mike-cardwell超过 1 年前
I did this in Nov 2021 - <a href="https:&#x2F;&#x2F;www.grepular.com&#x2F;Self_Building_and_Executing_Dockerfiles" rel="nofollow">https:&#x2F;&#x2F;www.grepular.com&#x2F;Self_Building_and_Executing_Dockerf...</a><p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env -S bash -c &quot;podman run --rm -w &#x2F;x -v &quot;\$PWD:&#x2F;x&quot; \$(podman build -q - &lt; \$0) \${@:1}&quot;</code></pre>
keepamovin超过 1 年前
Can we figure out a way to throw an exec into the shebang so the Docker process replaces the bash One?
teknopaul超过 1 年前
Simple file based solution. That&#x27;s not abuse that&#x27;s unix.<p>Well; thats Unix before Pottering and Microsoft.
benatkin超过 1 年前
I feel comfortable with fenced code blocks. Using heredocs all the time, not so much.<p><pre><code> ```js title=&quot;&#x2F;root&#x2F;server.js&quot; console.log(&#x27;test&#x27;) ``` </code></pre> or<p><pre><code> `&#x2F;root&#x2F;server.js` ```js console.log(&#x27;test&#x27;) ``` </code></pre> vs<p><pre><code> RUN &lt;&lt;EOF cat &gt;&#x2F;root&#x2F;server.js console.log(&#x27;test&#x27;) EOF </code></pre> However the Markdown one is better if the syntax highlighting theme makes the code fence a color that doesn&#x27;t stick out - either monochrome or closer to the background color.
评论 #38987595 未加载
评论 #38989159 未加载
a_t48超过 1 年前
That’s cute - though typically the docker images I build need supporting infra around them anyhow - I’d have to forward to the build script.
评论 #38987380 未加载
WhyNotHugo超过 1 年前
Using spaces in a shebang is not a standard thing and doesn’t work in most shells.
评论 #38988578 未加载
评论 #38988600 未加载
phone8675309超过 1 年前
Is this webscale?
评论 #38988282 未加载
评论 #38988615 未加载
fruktmix超过 1 年前
Can someone explain what this is about?
评论 #38995406 未加载
Igor_Wiwi超过 1 年前
what is the practical use of it?
评论 #38994125 未加载
评论 #38993043 未加载
WhackyIdeas超过 1 年前
I had no idea a shebang could be used like this! After all of these years…<p>Nice hack. Love it.
kitd超过 1 年前
For added excitement, you could go the whole hog, and generate, build and run via docker compose. Apart from anything else, you wouldn&#x27;t need the 2-step build&amp;run.<p>I mean, you could. Whether you should, well ...
mgaunard超过 1 年前
why not use docker build -q instead of that silly sha parsing?
评论 #38992497 未加载
IggleSniggle超过 1 年前
I like how the last code line reads<p><pre><code> ctx.stroke()</code></pre>
renewiltord超过 1 年前
Haha very clever. I like it. L