<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/feeds.xsl" type="text/xsl"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://chameth.com/">
    <title>Chameth.com - posts like debugging-beyond-the-debugger, docker-automatic-nginx-proxy, infinite-avatars, why-you-should-be-using-https but not docker-proxying-redux</title>
    <subtitle>Personal homepage of Chris Smith</subtitle>
    <link href="https://chameth.com/feeds/posts/like/debugging-beyond-the-debugger,docker-automatic-nginx-proxy,infinite-avatars,why-you-should-be-using-https/unlike/docker-proxying-redux/" rel="self"/>
    <link href="https://chameth.com/"/>
    <icon>https://chameth.com/favicon.png</icon>
    <updated>2025-08-08T00:00:00Z</updated>
    <id>https://chameth.com/</id>
    <author>
        <name>Chris Smith</name>
    </author>
    <entry>
        <title>Making a font of my handwriting</title>
        <link href="https://chameth.com/making-a-font-of-my-handwriting/"/>
        <updated>2025-08-08T00:00:00Z</updated>
        <id>https://chameth.com/making-a-font-of-my-handwriting/</id>
        <content xml:lang="en" type="html">&lt;p&gt;Recently I’ve been on a small campaign to try to make my personal website
more… personal. Little ways to make it obvious it’s &lt;em&gt;mine&lt;/em&gt; and &lt;em&gt;personal&lt;/em&gt;,
not just another piece of the boring corporate dystopia that is most of
the web these days. I don’t quite want to fully regress to the Geocities era
and fill the screen with animated under construction GIFs, but I do want to
capture some of that vibe.&lt;/p&gt;
&lt;p&gt;I’d added some bits and pieces along those lines: floating images in articles
now look like they’re stuck to the page with sellotape, related post
links have a wavy border that animates when you hover over them, and so on.
Next, I wanted to change the heading fonts from a monospace font to something
cursive, to resemble handwriting. Less terminal output, more handwritten letter.
I couldn’t find one I liked, though. So why not make my own? It can’t be that
hard, right?&lt;/p&gt;
&lt;h3 id=&#34;failing-to-do-it-myself&#34;&gt;Failing to do it myself&lt;/h3&gt;
&lt;p&gt;I set out to try to make the font myself using open source tools. After doing
a bit of research, it seemed like the general approach was to create vectors of
each character and then import them into a font editor. That seems to mean
either Adobe Illustrator and FontLab (if you have too much money) or Inkscape
and FontForge (if you like open source). I fall firmly into the latter category,
so I grabbed my graphics tablet and opened Inkscape.&lt;/p&gt;
&lt;!--more--&gt;
&lt;p&gt;I wrote out my first three letters: capital A, B and C. Saved them in Inkscape,
and attempted to import them into FontForge. Then I remembered one crucial
thing that had slipped my mind: I absolutely loathe using FontForge. It’s a bit
like when you open an old version of GIMP and get a bunch of weird looking
windows floating all over the place; it feels like you’re fighting against the
tool to do even the most basic operations. The difference is I have cause to
edit images a &lt;em&gt;lot&lt;/em&gt; more than I edit fonts, and GIMP has actually significantly
improved their UI over the years.&lt;/p&gt;
&lt;p&gt;Here are the rough steps I went through with FontForge:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Launch Font Forge. It shows a weird bit of art in one window, and an open
file dialog in another.&lt;/li&gt;
&lt;li&gt;I don’t want to open a file, so I close that dialog. The program exits.&lt;/li&gt;
&lt;li&gt;Relaunch Font Forge, and realise that within the “Open Font” dialog is a
“New” button. Click it.&lt;/li&gt;
&lt;li&gt;Get to the standard font-editing UI. Right-click on the “A” looking for
a way to import an SVG. Don’t see one.&lt;/li&gt;
&lt;li&gt;Click around a bit, exploring the menus. Everything feels a bit off.
You can’t open one menu then hover over the next to see its content, like
basically every UI toolkit in existence. I think FontForge has eschewed QT
and GTK in favour of doing things itself.&lt;/li&gt;
&lt;li&gt;Find the “Import” option in the File menu. Hope it’s for a single glyph not
the whole font.&lt;/li&gt;
&lt;li&gt;A file picker opens. Again it’s all a bit off from normal desktop
conventions. Try to resize it, and just get blank grey space at the bottom.&lt;/li&gt;
&lt;li&gt;Type the absolute path I want to go to in the text field.&lt;/li&gt;
&lt;li&gt;Get a dialog saying “Not a bdf file /home/chris/etc”. Press OK.&lt;/li&gt;
&lt;li&gt;Get a dialog saying “Could not find a bitmap font in”. Press OK.&lt;/li&gt;
&lt;li&gt;Press Ctrl+L to see if that lets me enter a path. Click everything in the
dialog to try to find a way to enter a path. Get annoyed. Give up. Click
through folder-by-folder to get to where I want to be.&lt;/li&gt;
&lt;li&gt;Get to the folder and don’t see any files. Change the format to “SVG”.
Double-click the newly-visible SVG file.&lt;/li&gt;
&lt;li&gt;Get a dialog saying “You must select a glyph before you can import an image
into it”. Press OK.&lt;/li&gt;
&lt;li&gt;The import dialog goes away, having not imported.&lt;/li&gt;
&lt;li&gt;Select the glyph in the main tool area, then repeat the File→Import dance.&lt;/li&gt;
&lt;li&gt;It’s actually there now! Open the glyph in the editor and see it’s
a complete mess of Bézier curves. I can’t click what I want without
accidentally moving a handle for an adjacent curve.&lt;/li&gt;
&lt;li&gt;Rage-quit.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I’m sure FontForge is less anger inducing once you’re used to it. And you
definitely could use it to build a font like this if you had much more patience
than me. I’d had enough of death-by-a-thousand-paper-cuts though.&lt;/p&gt;
&lt;p&gt;I briefly tried Inkscape’s built-in support for making an SVG font. It annoyed
me a lot less, but it’s fiddly: it seemed like each glyph had to be a single
path, so you had to convert the glyphs to paths, then merge them correctly.
If you merge them incorrectly then the wrong bits of your letters end up
filled (like the inside of the ‘B’). Path manipulation is getting towards the
limit of my knowledge of vector editing, and it took a bit of trial and error
for each letter that had more than a single stroke. I didn’t fancy doing that
for every letter.&lt;/p&gt;
&lt;p&gt;I’m usually a big advocate of open source, but this was one of those painful
times when it feels like it just falls short. Clunky, painful UI and processes
where commercial tools just let you get on with your work.&lt;/p&gt;
&lt;h3 id=&#34;you-can-exchange-money-for-goods-and-services&#34;&gt;You can exchange money for goods and services&lt;/h3&gt;
&lt;p&gt;When I’d been looking for open source tutorials, I found many mentions of
a closed source, hosted tool: &lt;a href=&#34;https://www.calligraphr.com/en/&#34;&gt;Calligraphr&lt;/a&gt;.
It has a free version with limitations (no ligatures, no variations, 75
glyphs per font), and a pro version for £8/month. I’d normally balk at the
idea of a subscription for this, but they have the perfect answer: you can
make a one-time payment, and your account automatically downgrades back to free
after a month. It’s not a hidden option, either, it’s the most prominent button
on the upgrade page. That made me happy to give them £8 to play around with
the service for a month.&lt;/p&gt;
&lt;p&gt;Calligraphr works by having you print templates, write out the letters, then
scan them in. It does some magical processing to extract the glyphs, provides
tools to tidy them up, align them, etc, and then produces a TTF file for you.
You can see some of my completed templates here:&lt;/p&gt;
&lt;figure class=&#34;image full&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/making-a-font-of-my-handwriting/template.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/making-a-font-of-my-handwriting/template.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/making-a-font-of-my-handwriting/template.jpg&#34; alt=&#34;Eight scanned template sheets, filled in with handwritten letters and ligatures&#34; loading=&#34;lazy&#34; width=&#34;1166&#34; height=&#34;841&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;Most of the templates I used for the font&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Calligraphr has a nice UI to generate the templates, allowing you to select
which glyphs to include. I added the “minimal English”, “basic punctuation”
and “Ligatures” sets. That gave me four pages to fill out, and I did them all
twice. That let me filter out versions that didn’t work well, and have
variants for some letters so the font wasn’t too repetitive. Later on, I went
back and added some custom ligatures based on blog post titles that didn’t look
quite right: “Re”, “To”, “ers”, “ey”, “hy”, “ra”, “re” and “ty”. Ligatures like
this help it look more natural: when we write we don’t just stamp out identical
letters regardless of their surroundings, instead they will connect to their
neighbours, or overlap slightly, or even share a stroke.&lt;/p&gt;
&lt;p&gt;I filled these templates in with a Sharpie, as I wanted a fairly informal,
scrap-booky look, and it would also give good solid shapes that should be easy
to pick out of the template. I scanned them with the “Scan Document” function
on my iPhone, and uploaded the PDFs to Calligraphr.&lt;/p&gt;
&lt;h3 id=&#34;iterating-and-tweaking&#34;&gt;Iterating and tweaking&lt;/h3&gt;
&lt;p&gt;The Calligraphr UI allows you to preview the font, but I found it a lot more
useful to just download a copy and use it on a local copy of my website.
That let me test it with real text, and see how it’d look at the different font
sizes I use on the site.&lt;/p&gt;
&lt;p&gt;The first version was not great. Despite the guidelines on the template, I
apparently wasn’t good at sticking to them. Some letters were floating way off
the baseline, and some were sunken below. When those opposites met it looked
terrible. Fortunately Calligraphr has a pretty easy tool to slide each letter up
and down, and scale it up or down if needed, and you can see it next to other
letters as you do it. It took a little bit of time to go through all the
variants of all the letters, but the next version looked a lot better.&lt;/p&gt;
&lt;p&gt;Another tweak I ended up doing was reducing the spacing between letters. The
defaults Calligraphr uses are probably good for a blocky font, but I wanted to
put the letters close together to give it more of a joined-up look.
Again, this is an easy tool to use, you just drag the sides in or out as
desired. While these tweaking steps were probably as fiddly as some of the
Inkscape steps I refused to do earlier, they’re a lot more rewarding as you
see things improving with each one. It’s a lot easier for me to commit time
and effort to improving something that’s already working reasonably, than put
that time and energy into an unknown.&lt;/p&gt;
&lt;p&gt;Later, I noticed that occasionally there would be a huge gap in a title. Not
“the kerning is slightly off” but “there’s enough room to park a bus”. It took
me a while to figure out what was happening: a couple of glyphs
hadn’t been isolated perfectly and had picked up a few pixels from the template
lines at the edge of their boxes. That meant the glyph had a width that covered
the actual written glyph, a big gap, and then the rogue marks. At first, I fixed
this by just adjusting the width, but that left the little pixels floating
awkwardly down-sentence. The proper fix was to use the editing tool and simply
delete them, and then Calligraphr snapped the width back to what it should be.&lt;/p&gt;
&lt;p&gt;These iterations took a while to do, but I just dipped in and out occasionally
over the course of a week, so it didn’t actually feel like too much work. I
quite enjoy the process of refining things, too.&lt;/p&gt;
&lt;h3 id=&#34;result-and-a-surprise&#34;&gt;Result and a surprise&lt;/h3&gt;
&lt;p&gt;If you’re viewing this post on my website&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:1&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;, you can see the font in the
headers, captions, and a few other places. Here’s how it compares to my actual
handwriting:&lt;/p&gt;
&lt;figure class=&#34;image full&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/making-a-font-of-my-handwriting/sample.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/making-a-font-of-my-handwriting/sample.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/making-a-font-of-my-handwriting/sample.jpg&#34; alt=&#34;A hand-written line of text saying &amp;#39;Hello World! This is Chris Hand&amp;#39; above the same line of text in the Chris Hand font&#34; loading=&#34;lazy&#34; width=&#34;763&#34; height=&#34;194&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;My handwriting vs my handwriting font&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;It’s not close enough to forge documents, but I think it definitely gets
across my style, and that’s exactly what I wanted. It’s surprisingly legible
even at smaller font sizes — I think the weight of the Sharpie helps here — and
at £8 and a bit of manual work was a lot more economical than spending days
wresting with open source tools.&lt;/p&gt;
&lt;p&gt;A few weeks after I put the finishing touches on the font, I got an e-mail
from Calligraphr. As my account had lapsed back to the free version, I was
no longer eligible for the “server-side backup” feature. So what did they do?
They e-mailed me an exported copy! It’s a JSON file with the properties of each
glyph and a base64 encoded image. Not only can I re-upload this to Calligraphr
if I resubscribe, I can probably hook something up to edit it should I ever
need to. I’m blown away by how pro-user Calligraphr’s business practices are.
They’re up-front about pricing, don’t try to get you stuck on an auto-renewing
subscription, and automatically export your data. It’s like a breath of fresh
air compared to the barrage of dark patterns that other websites foist on us.
If you want to make this kind of font, I’d definitely recommend them just
because of how &lt;em&gt;nice&lt;/em&gt; they are.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;And I haven’t changed everything since writing this post… &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:1&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content>
    </entry>
    <entry>
        <title>Coming around on LLMs</title>
        <link href="https://chameth.com/coming-around-on-llms/"/>
        <updated>2025-05-28T00:00:00Z</updated>
        <id>https://chameth.com/coming-around-on-llms/</id>
        <content xml:lang="en" type="html">&lt;figure class=&#34;image right&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/coming-around-on-llms/claude-hello.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/coming-around-on-llms/claude-hello.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/coming-around-on-llms/claude-hello.png&#34; alt=&#34;A screenshot of the Claude web UI with the prompt &amp;#39;Say &amp;#34;Hello!&amp;#34;&amp;#39;. The response is &amp;#34;Hello!&amp;#34;&#34; loading=&#34;lazy&#34; width=&#34;176&#34; height=&#34;172&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;Claude says hi.&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;For a long time I’ve been a sceptic of LLMs and how they’re being used and
marketed. I tried ChatGPT when it first launched, and was totally underwhelmed.
Don’t get me wrong: I find the technology damn impressive, but I just couldn’t
see any use for it.&lt;/p&gt;
&lt;p&gt;Recently I’ve seen more and more comments along the lines of “people who
criticise LLMs haven’t used the latest models”, and a good number of developers
that I respect have said they use coding models in some capacity. So it seemed
like it was time to give them another shake.&lt;/p&gt;
&lt;p&gt;The first decision to make was which model to try. OpenAI are no longer the
only player in the game, every tech company of a certain size is now also
somehow an AI company&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:1&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;. I looked a bit at some benchmarks, and then mostly
ignored them and went with the only company that I didn’t outright hate:
Anthropic, and their model Claude.&lt;/p&gt;
&lt;h3 id=&#34;initial-impressions&#34;&gt;Initial impressions&lt;/h3&gt;
&lt;p&gt;The latest Claude models do feel a lot more “capable” than the earlier ChatGPT
versions I remember, but they also still have a lot of the same problems. At
their heart, they’re still text-prediction models, and still seem to be trained
to predict text that will please the user rather than be factually accurate or
useful.&lt;/p&gt;
&lt;!--more--&gt;
&lt;p&gt;One of the improvements that people kept mentioning was the ability for models
to access the web directly. I’ve seen it kick in a bit naturally, and asked
for it explicitly sometimes, and it’s… nothing special? Are people just really
bad at searching the web? Maybe they should try &lt;a href=&#34;https://kagi.com/&#34;&gt;Kagi&lt;/a&gt;? I
feel like about 80% of the time I could have found the information just as
quickly myself, about 10% of the time it searched and then hallucinated an
answer, and the remaining 10% it found something quicker than I otherwise
would have. Those aren’t great results, especially when you consider how
much cheaper and simpler just searching the web is.&lt;/p&gt;
&lt;p&gt;There is another way to trigger web searches in Claude: using “research mode”.
When you enable it, it splits off into different models: a “lead researcher” to
come up with a plan, and then some minions that execute it. It ends up doing
hundreds of web queries in the span of a few seconds. Suddenly I understand why
things like &lt;a href=&#34;https://anubis.techaro.lol/&#34;&gt;Anubis&lt;/a&gt; need to exist. I’ve not used
the “please launch a DoS attack” button since.&lt;/p&gt;
&lt;p&gt;What did impress me, though, was its ability to churn out reasonable-ish
code. It can hack together a bash script as well as I can, and do it far
faster than I’d be able to. Sometimes they even work. That made me wonder
what it would be like doing actual coding with it. Anthropic have a CLI tool
called &lt;code&gt;claude-code&lt;/code&gt;, so I paid them lots of money and gave it a spin.&lt;/p&gt;
&lt;h3 id=&#34;coding-with-claude&#34;&gt;Coding with Claude&lt;/h3&gt;
&lt;p&gt;The first thing I notice about &lt;code&gt;claude-code&lt;/code&gt; is that I really like the
interface. It’s basically an input box in a terminal. It doesn’t force me to
use a certain IDE or do things in a certain way. By default it asks before
making any changes, showing you a side-by-side diff of what it’s doing and
allowing you to provide feedback. If I had to design a way to interact with
a coding agent from scratch, I can’t think of many things I’d improve.&lt;/p&gt;
&lt;figure class=&#34;image full&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/coming-around-on-llms/code-session.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/coming-around-on-llms/code-session.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/coming-around-on-llms/code-session.png&#34; alt=&#34;A screenshot of claude-code. I ask it for the permalink for the latest post, and it gives a wrong answer. After prompting it again it gets it right.&#34; loading=&#34;lazy&#34; width=&#34;1121&#34; height=&#34;554&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;A simple example of a claude-code session&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The power in &lt;code&gt;claude-code&lt;/code&gt; versus just using the web UI is that it can use
tools. It can query &lt;code&gt;git&lt;/code&gt;, run &lt;code&gt;grep&lt;/code&gt; commands, even use &lt;code&gt;sed&lt;/code&gt; if it wants to
change something in lots of files at once. It’s very good at figuring out its
way around a codebase, even without any explicit instructions. You can see in
the screenshot that with a little prompting it managed to get the permalink
to this post; I didn’t tell it where the posts were stored, or how to work out
the latest, I just told it when it was wrong. If I’d run &lt;code&gt;/init&lt;/code&gt; before it
would have probably picked up on the fact that my posts have custom permalinks,
and noted it in &lt;code&gt;CLAUDE.md&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But how good is it at actually writing code? It’s like having a keen but not
particularly thorough Junior Engineer at your beck and call. If you give it
a clearly defined task and guidance on how to implement it (and maybe some
feedback as it suggests changes), it’s more than capable of doing it. If you
don’t give it enough guidance it tends to go more off the rails. I tried
having it generate a simple application from scratch with minimal technical
guidance and no review of what it was doing, and it made such a mess of it I
decided it was quicker to throw it away and start again by hand.&lt;/p&gt;
&lt;h3 id=&#34;the-man-behind-the-curtain&#34;&gt;The man behind the curtain&lt;/h3&gt;
&lt;p&gt;Even with sufficient guidance, at times it’s &lt;em&gt;really&lt;/em&gt; obvious that it’s an
LLM generating pleasing-token-strings and not something that genuinely
understands what it’s doing. It will spit out code like this:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-nx&#34;&gt;err&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-o&#34;&gt;!=&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;chroma-k&#34;&gt;if&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-nx&#34;&gt;err&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-o&#34;&gt;==&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-nx&#34;&gt;sql&lt;/span&gt;&lt;span class=&#34;chroma-p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;chroma-nx&#34;&gt;ErrNoRows&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-w&#34;&gt;       &lt;/span&gt;&lt;span class=&#34;chroma-k&#34;&gt;return&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;chroma-p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-nx&#34;&gt;err&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;chroma-p&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-w&#34;&gt;    &lt;/span&gt;&lt;span class=&#34;chroma-k&#34;&gt;return&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-kc&#34;&gt;nil&lt;/span&gt;&lt;span class=&#34;chroma-p&#34;&gt;,&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt; &lt;/span&gt;&lt;span class=&#34;chroma-nx&#34;&gt;err&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-p&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;chroma-w&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Why’s that check for &lt;code&gt;sql.ErrNoRows&lt;/code&gt; there? It’s entirely pointless. There was
no instruction to check for it, none of the existing code checked for it, but
I assume it comes up quite a bit in the training data. But it didn’t
&lt;em&gt;understand&lt;/em&gt; why, so it put the check in, and returned the exact same thing as
if it hadn’t.&lt;/p&gt;
&lt;p&gt;It also occasionally tries to “cheat” or solve problems the wrong way. It
sometimes feels a bit like you’re asking for wishes from a Monkey Paw. “Stop
the unit tests failing”, you’ll say; Claude will respond with a request to
delete the failing test. The man behind the curtain isn’t particularly well
hidden, and the training data and pattern matching often shows through.&lt;/p&gt;
&lt;p&gt;This lack of understanding also makes it very hard to get Claude to use
comments in a sensible way. It absolutely loves doing nonsense like this:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-c1&#34;&gt;// render form
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-kd&#34;&gt;function&lt;/span&gt; &lt;span class=&#34;chroma-nx&#34;&gt;renderForm&lt;/span&gt;&lt;span class=&#34;chroma-p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;chroma-p&#34;&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;    &lt;span class=&#34;chroma-c1&#34;&gt;// ...
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&lt;span class=&#34;chroma-p&#34;&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Even with explicit instructions to avoid useless comments, or comments that only
explain what the code does (not why it does it), or other prompts. Again, I’m
chalking it up to a mixture of training data that does that, and lack of any
actual understanding about why a human developer may want a comment to exist.
Of course, there are plenty of flesh-and-blood devs out there that also don’t
comment effectively, so maybe I shouldn’t give Claude too much of a hard time
on this.&lt;/p&gt;
&lt;h3 id=&#34;-and-yet-&#34;&gt;… and yet …&lt;/h3&gt;
&lt;p&gt;So with all those problems, it sounds like it’s just not worth it, right?
Well, not quite. I feel like programming mostly consists of two distinct tasks:
thinking how to implement things; actually implementing them; and then debugging
why you’re off-by-one somewhere. Letting an LLM do the thinking is a big no-go
for me: it’s evidently not good at it, and frankly that’s one of the things
I most like about programming. Having let it loose on small projects, I shudder
to think what the codebases of all the “vibe coded” projects that are popping up
are like.&lt;/p&gt;
&lt;p&gt;For the second stage, though, it’s actually quite nice. If I’ve thought through
how I want something to be implemented, I can feed those steps to Claude and have
it churn out the otherwise not-too-interesting code. This requires far less
time and concentration on my part than writing code, to the extent that I can
be thinking about the next feature, or doing something else at the same time. I
don’t feel like I’m going to end up deskilling myself this way, as I’ve already
formed the idea of what I want to code, I’m just using the LLM to spit it out
faster than I can type it.&lt;/p&gt;
&lt;p&gt;A lot of the most boring bits of coding like implementing CRUD-y operations can
be summarised as “Look at this file/function. Do the same thing but slightly
differently elsewhere”. Claude is great at this, especially when explicitly
prompted like that. You’re basically playing to its pattern-matching strengths,
rather than asking it to come up with anything novel. Most of my prompts tend
to be prefixed with “Look at @some_file.go and @other_file.go.” to cue up the
patterns I want it to use, and I find this works well.&lt;/p&gt;
&lt;p&gt;As for debugging, it’s a mixed bag. It’s sometimes amazingly insightful, and
sometimes just runs around in circles trying the wrong things over and over.
It’s worth asking the question, but I definitely wouldn’t rely on it over my
own abilities.&lt;/p&gt;
&lt;h3 id=&#34;the-future&#34;&gt;The future&lt;/h3&gt;
&lt;p&gt;I’m probably going to carry on using &lt;code&gt;claude-code&lt;/code&gt;, at least for personal
projects. I’ve got so much done that I just wouldn’t have been
&lt;em&gt;bothered&lt;/em&gt; to do if I was doing it all by hand. I very much enjoy being in
the more “diffuse thinking” mindset, planning how things are going to work,
rather than being stuck in the mines digging out SQL queries. After all, who
wouldn’t want an over-eager assistant to work on all their hobby projects?&lt;/p&gt;
&lt;p&gt;Work is a slightly different matter: a private project or even an open source
project that disclaims any liability is different to something I’m being
paid to deliver, and bear responsibility for fixing if it’s not done correctly.
I’m not saying I won’t use it at all, but if I do it’ll be much more constrained
than I would in personal projects.&lt;/p&gt;
&lt;p&gt;As for non-code usages: I’m not sold. My sceptic hat is still firmly in place.
I don’t think chat is a particularly good interface for many things, and
hallucinations are still a big problem despite what people say. I hate the tide
of AI slop that’s taking over the Internet, and how LLM-powered chat
agents are being forced into every random product. You’re definitely not going
to be seeing any AI-generated blog posts from me!&lt;/p&gt;
&lt;p&gt;One topic I’ve not gone into here is the ethical concerns about using LLMs. They
obviously exist, and I do have thoughts, but that’s a topic for another day.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;I’m surprised it’s not gone more mainstream: why’s there
no Tesco Value LLM model, yet? &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:1&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content>
    </entry>
    <entry>
        <title>Generating infinite avatars</title>
        <link href="https://chameth.com/infinite-avatars/"/>
        <updated>2022-12-30T00:00:00Z</updated>
        <id>https://chameth.com/infinite-avatars/</id>
        <content xml:lang="en" type="html">&lt;figure class=&#34;image right&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/unique.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/unique.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/infinite-avatars/unique.jpg&#34; alt=&#34;A computer render of the author, with a &amp;#34;UNIQUE LIMITED EDITION&amp;#34; badge&#34; loading=&#34;lazy&#34; width=&#34;256&#34; height=&#34;256&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;An example of one of the unique avatars&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;I recently added a new ‘about’ section to the top of my
website. Like most about pages, it has a picture. Instead of
a normal photograph, however, you’ll see an AI-generated
avatar. This is admittedly fairly trendy at the minute —
apps like Lensa offer to make you profile pictures if you
give them a set of photos and some cash — but I’ve done
something a bit different.&lt;/p&gt;
&lt;p&gt;You see, there is not just one image that has been carefully
curated, edited, and uploaded. No, the image you see quite
possibly has never been seen before and will never be seen
again. It’s unique. Just for you.&lt;/p&gt;
&lt;h3 id=&#34;background-stable-diffusion-dreambooth-et-al&#34;&gt;Background: Stable Diffusion, DreamBooth, et al&lt;/h3&gt;
&lt;p&gt;You’ve probably heard of &lt;a href=&#34;https://github.com/CompVis/stable-diffusion&#34;&gt;Stable Diffusion&lt;/a&gt;, the open
text-to-image model developed by LMU Munich. Given a text
prompt it starts with a random array of static and repeatedly
transforms it, each step moving away from pure entropy and
towards a real image that befits the prompt&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:1&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;. It stands
in contrast to competitors like DALL-E and Midjourney
in both the code and the model being freely and publicly
available.&lt;/p&gt;
&lt;!--more--&gt;
&lt;p&gt;One interesting side effect of that is that you can take
the pre-trained Stable Diffusion model, and run your own
further training on top. It takes hundreds of thousands of
GPU hours to train such a model from scratch, but only a few
to add some specific tweaks on top. Earlier this year
researchers from Boston University and Google Research published
a paper titled &lt;a href=&#34;https://dreambooth.github.io/&#34;&gt;DreamBooth&lt;/a&gt;
which presents a model for doing exactly that.&lt;/p&gt;
&lt;p&gt;This research has spawned a slew of startups that do the
training and/or generation for you in return for cold, hard
cash. The most popular of these at present is Lensa, a mobile
app that will generate 200 avatars for you in pre-set styles
for £9.99. You can’t change the styles or regenerate any
you don’t like, but from what I hear 200 is just about enough
that you’ll find one or two that you like.&lt;/p&gt;
&lt;figure class=&#34;image left&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/training.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/training.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/infinite-avatars/training.jpg&#34; alt=&#34;A screenshot of the DreamBooth notebook while training is underway&#34; loading=&#34;lazy&#34; width=&#34;210&#34; height=&#34;113&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;The lovely ASCII art shown while training is underway&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;While £9.99 isn’t much money, if you’re technically inclined
then it’s not very difficult to do the work yourself for free.
That also lets you come up with unique prompts, creating pictures
in different styles, with different backgrounds, and so on.
I used the wonderful &lt;a href=&#34;https://github.com/TheLastBen/fast-stable-diffusion&#34;&gt;notebooks from TheLastBen&lt;/a&gt;
that run in Google Colab. The generous free tier offered by Colab
is plenty enough to run a DreamBooth training session, and the
notebook walks you through pretty much everything&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:2&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;If you train a custom model then you end up with a weighty
file called a “checkpoint”, which you can provide to most
Stable Diffusion tools to use when generating images. I use
&lt;a href=&#34;https://github.com/AUTOMATIC1111/stable-diffusion-webui&#34;&gt;AUTOMATIC1111’s stable-diffusion-webui&lt;/a&gt;
which not only offers a simple web UI, but also a REST API
for accessing it programmatically. I installed this on my
laptop and spent a happy hour or two generating weird and
wonderful pictures of me.&lt;/p&gt;
&lt;h3 id=&#34;automating-it&#34;&gt;Automating it&lt;/h3&gt;
&lt;p&gt;When I was training the model, I was planning on finding
a single nice avatar to use. After playing around with it
for a while, though, I wanted to expose all the wacky and
unique pictures that it was generating. I came up with the
rough idea of batch generating a number of avatars, then
having a custom webserver that served you one and deleted
it.&lt;/p&gt;
&lt;p&gt;My first attempt at this was to try and run the Stable
Diffusion process entirely on CPU on a server. I’d previously
run an SD generator on my laptop without CUDA support, and
while it was deathly slow it still worked. With this custom
model, though, it took longer to initialise than I was
prepared to wait — and I hate to think how long the subsequent
image generation would have taken!&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:3&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;I obviously needed something with a GPU, but I didn’t want to
use my laptop as it may be unavailable or doing other more
important things with its GPU like playing games. So I turned
to AWS, and found they have GPU-enabled instances that can be
obtained for reasonable amounts of money. As I didn’t really
care when the batch processing ran, I could use “spot” instances
which offer a decent discount in exchange for only being able
to run when there aren’t reserved instances that need the
resources.&lt;/p&gt;
&lt;p&gt;After much fiddling in the AWS console, I got a spot reservation
set up for a GPU-enabled instance. After waiting a while and not
seeing any instances appear, I checked the logs and found it was
erroring because I was trying to exceed my vCPU limit. Odd. A bit
of googling&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:4&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt; later and I discover there’s a separate limit for
that type of machine and the default limit is 0. There’s a whole
mini application in AWS for requesting limit increases, so I
requested a modest increase to 8 vCPUs (the minimum configuration
for the “accelerated computing” images is 4 or 8 vCPUs depending
on the exact type). After a brief wait, Amazon declined
my request:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I am sorry but at this time we are unable to approve your service quota increase request.&lt;/p&gt;
&lt;p&gt;Service quotas are put in place to help you gradually ramp up activity and decrease the likelihood of large bills due to sudden, unexpected spikes.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;I’m not entirely sure how you’re meant to ramp up without being
able to run a single instance. There are lots of theories online
about account age requirements, minimum spends, etc, but I wasn’t
willing to jump through inscrutable hoops in order to try to give
Jeff Bezos more money. Instead, I looked at
&lt;a href=&#34;https://paperspace.com&#34;&gt;Paperspace&lt;/a&gt;, a service I’d come across
previously when trying to run a GPU-enabled Windows box. They have
a variety of GPUs on offer, and a lovely API to remotely manage
machines. [If you want to try Paperspace you can use
&lt;a href=&#34;https://console.paperspace.com/signup?R=DSI7ABP&#34;&gt;this referral link&lt;/a&gt;
to get $10 off. In doing so you’ll give me enough credit to generate
around 10,000 avatars. If that’s not a worthy cause, I don’t know
what is.]&lt;/p&gt;
&lt;p&gt;I went a bit overboard investigating the different GPU offerings
and their relative bang for the buck:&lt;/p&gt;
&lt;figure class=&#34;image center&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/spreadsheet.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/spreadsheet.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/infinite-avatars/spreadsheet.png&#34; alt=&#34;Picture of a spreadsheet showing performance and price comparisons for paperspace GPUs&#34; loading=&#34;lazy&#34; width=&#34;1175&#34; height=&#34;453&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;A slightly over-the-top analysis of the GPUs offered by paperspace&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The A4000 comes out on top: it’s built on a modern architecture with
a large number of CUDA cores, and is really competitively priced. Paperspace
only give you access to the M4000 and P4000 initially and make you request
access to the higher tier units. I dutifully filled out the very brief form,
and a day later it was approved. At least someone is willing to accept
my money!&lt;/p&gt;
&lt;h3 id=&#34;writing-some-code&#34;&gt;Writing some code&lt;/h3&gt;
&lt;p&gt;After setting up the machine on Paperspace and copying over my custom model,
I set about writing code to handle the generating and the serving. I eventually
settled on having two buckets of images: ones that will be shown to only one
person and deleted on use, and a fallback bucket that will be used multiple times.
The fallback bucket is so that I can limit how many avatars I need to generate
(and thus how much money I pay for GPU time&lt;sup id=&#34;fnref:5&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:5&#34; role=&#34;doc-noteref&#34;&gt;5&lt;/a&gt;&lt;/sup&gt;). I set a global limit of one avatar
used every 10 minutes, as well as a per-IP limit of one unique avatar per 24 hours.&lt;/p&gt;
&lt;p&gt;In order to distinguish whether you’re seeing a unique avatar, the server adds
a border around it and a “UNIQUE LIMITED EDITION” label at the bottom. If you
see that text, you’re looking at an image that has never been seen before and
that has already been deleted. The server sends some aggressive caching headers,
so in normal day-to-day operations you should see a different unique avatar every
day you visit the site.&lt;/p&gt;
&lt;p&gt;The generating side is a bit more interesting. It monitors the contents of the
two avatar buckets, and springs into action if they fall below a configured minimum.
It starts the process by calling Paperspace and requesting the machine is started up,
then repeatedly polls the status endpoint until it’s ready. It then generates images
individually using the REST API until it hits the bucket’s configured maximum.
Once it’s done, it asks Paperspace to shut the machine back down.&lt;/p&gt;
&lt;p&gt;Initially I just hardcoded a set of prompts for the generator to use, but they
resulted in a lot of fairly similar images. To make things more interesting, I
started dynamically generating the prompt using a combination of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A prefix such as “A painting of”, “A sketch of”, “A photograph of”&lt;/li&gt;
&lt;li&gt;In 80% of prompts, an artist reference such as “in the style of Andy Warhol”&lt;/li&gt;
&lt;li&gt;In 30% of prompts, a film reference such as “from the film The Matrix”&lt;/li&gt;
&lt;li&gt;1-10 random suffixes such as “bokeh”, “8K”, “trending in Artstation”&lt;sup id=&#34;fnref:6&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:6&#34; role=&#34;doc-noteref&#34;&gt;6&lt;/a&gt;&lt;/sup&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Initially I had the film and artist prompts independent, but the occasions where
neither appeared in the prompt lead to pretty bad images. Instead, there’s now a
20% chance of a film reference, a 70% chance of an artist reference, and a 10% chance
of both&lt;sup id=&#34;fnref:7&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:7&#34; role=&#34;doc-noteref&#34;&gt;7&lt;/a&gt;&lt;/sup&gt;. There’s a list of around 10 prefixes, 70 artists, 20 films and 20 suffixes
which gives a large pool of random prompts.&lt;/p&gt;
&lt;h3 id=&#34;end-results&#34;&gt;End results&lt;/h3&gt;
&lt;p&gt;Everything I’ve described is now live on &lt;a href=&#34;https://chameth.com/&#34;&gt;chameth.com&lt;/a&gt; – if you
visit you might get a unique, never-been-seen before version of me. The code for the
generating and serving is &lt;a href=&#34;https://github.com/csmith/avatargen&#34;&gt;available on GitHub&lt;/a&gt;
if you’re interested or want to replicate this for yourself.&lt;/p&gt;
&lt;aside class=&#34;update raised-box&#34;&gt;
  &lt;h5 class=&#34;plain-header&#34;&gt;Update 2024-12-06:&lt;/h5&gt;
  &lt;p&gt;After almost two years, the novelty of infinite avatars has worn off and I’ve
retired the avatar generator on &lt;a href=&#34;https://chameth.com/&#34;&gt;chameth.com&lt;/a&gt;, going back to a plain old
static avatar.&lt;/p&gt;
&lt;/aside&gt;
&lt;p&gt;To finish off, I ran off a batch of 200 avatars and have selected the most
interesting ones:&lt;/p&gt;
&lt;figure class=&#34;image center&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/avatars.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/infinite-avatars/avatars.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/infinite-avatars/avatars.jpg&#34; alt=&#34;A grid of Standard Diffusion produced avatars of the author&#34; loading=&#34;lazy&#34; width=&#34;640&#34; height=&#34;640&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;A selection of avatars produced by the generator&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;As you can see there was one output that appears to be a cat with a ball of yarn,
rather than a picture of me. That seems to happen occasionally when the various parts
of the prompt don’t gel well, but I’m happy with 0.5% or so of the images being
somewhat random! The batch of 200 avatars took just shy of 17 minutes to generate,
which will result in a bill of $0.22 from Paperspace.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;The famous quote from Arthur C Clark comes to mind when I
think too much about how this works: “Any sufficiently advanced
technology is indistinguishable from magic”. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:1&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;
&lt;p&gt;The only thing it doesn’t help with is &lt;em&gt;finding&lt;/em&gt; enough pictures
of yourself to use for the training data. That’s presumably easier
if you’re more of a “selfie person” than I am. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:2&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:3&#34;&gt;
&lt;p&gt;There were a lot of differences that could account for the extra
slowness: my custom model was based on the larger 2.1 SD model rather
than 1.5; I was using different software; and my laptop CPU is far more
modern than the server’s. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:3&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:4&#34;&gt;
&lt;p&gt;In the genericised sense: I used Duck Duck Go. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:4&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:5&#34;&gt;
&lt;p&gt;One of my biggest concerns here was to avoid putting a “make Chris pay
money” button on the Internet. That felt like a bad idea. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:5&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:6&#34;&gt;
&lt;p&gt;AKA the random detritus that gets appended to prompts to make images
better in mysterious ways. The English pedant in me hates this nonsense,
but the results when you spam rubbish modifiers are inarguably better than
when using straight forward prose. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:6&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:7&#34;&gt;
&lt;p&gt;Imagining the Venn Diagrams is left as an exercise for the reader. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:7&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content>
    </entry>
    <entry>
        <title>An introduction to containers</title>
        <link href="https://chameth.com/intro-to-containers/"/>
        <updated>2020-03-01T00:00:00Z</updated>
        <id>https://chameth.com/intro-to-containers/</id>
        <content xml:lang="en" type="html">&lt;figure class=&#34;image right&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/intro-to-containers/containers.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/intro-to-containers/containers.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/intro-to-containers/containers.jpg&#34; alt=&#34;Containers in port&#34; loading=&#34;lazy&#34; width=&#34;300&#34; height=&#34;396&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;So. Many. Containers.&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;I’m a huge fan of (software) containers. Most people I know fall in to one of two camps:
either they also use, and are fans of, containers, or they haven’t yet really figured them
out and view them as some kind of voodoo that they don’t really want or need.&lt;/p&gt;
&lt;p&gt;I’m writing this short guide to explain a little how containers work - and how running
something in a container isn’t really that much different to running it normally -
to hopefully enable more people in that second group to give them a try. It’s aimed at
people who have a fairly good grasp of how Linux works.&lt;/p&gt;
&lt;p&gt;Containers are often mentioned in the same breath as VMs, which is not a helpful comparison
or analogy. Think of containers as standard units of software, much like how
&lt;a href=&#34;https://en.wikipedia.org/wiki/Intermodal_container&#34;&gt;Intermodal containers&lt;/a&gt; are standard
units of freight transport across the world. When a company internationally ships goods in
volume there isn’t a question about how they’re packaged - they go in an intermodal container.
The same container can be deployed on a freight train, a lorry, or a ship. The haulage company
doesn’t need to care what’s in the container because they’re completely standardised.
Likewise, with software containers you don’t really need to care about what’s inside: the
software you’re deploying could be written in Go, Python2, Python3, Bash, PHP, LOLCODE, or
anything&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:1&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;!--more--&gt;
&lt;h3 id=&#34;what-does-a-running-container-look-like&#34;&gt;What does a running container look like?&lt;/h3&gt;
&lt;p&gt;When you run a container, you’re just running a process. In a lot of ways it’s not
very different to what happens if you were to start the same process on the host
computer.&lt;/p&gt;
&lt;p&gt;For example I have a container that just runs &lt;code&gt;cat(1)&lt;/code&gt;. With no arguments, &lt;code&gt;cat&lt;/code&gt;
will read from stdin until it receives an EOF, so it’s handy to test with.
If I run &lt;code&gt;ps a&lt;/code&gt; on my computer, I can see the cat process in amongst everything
else I’m currently running:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;    PID TTY      STAT   TIME COMMAND
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;   7199 pts/1    Ss     0:01 /usr/bin/zsh
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; 323806 pts/0    Ss+    0:00 /bin/cat
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; 324120 pts/4    R+     0:00 ps a
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The &lt;code&gt;/bin/cat&lt;/code&gt; process is in a container, and the &lt;code&gt;ps a&lt;/code&gt; underneath it is just running
like normal on my desktop. They look very similar, right? If I look under &lt;code&gt;/proc/323806&lt;/code&gt;
I can see all the usual attributes, the same as any other process running.&lt;/p&gt;
&lt;p&gt;If I run &lt;code&gt;ps&lt;/code&gt; in a container, though, it’s a different story:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;    PID TTY      STAT   TIME COMMAND
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;      1 pts/0    Rs+    0:00 ps a
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So inside the container it looks like there’s only one process running. It can’t see
anything running “outside” on my desktop. The secret here is that this isn’t a special
container trick: this is just a feature of the Linux kernel called &lt;em&gt;namespacing&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If we go back to procfs and look at the &lt;code&gt;ns/pid&lt;/code&gt; node we can see the process in the
container is in a separate PID (process ID) namespace to the one on my desktop:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# readlink /proc/323806/ns/pid  
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;pid:[4026534564]
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# readlink /proc/7199/ns/pid
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;pid:[4026531836]
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Almost all&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:2&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; processes running ’normally’ on my desktop have the same PID namespace,
whereas each container gets their own by default. PID namespaces are hierarchical:
a new process is assigned a PID in its own namespace, and the parent namespace, and
the grandparent namespace, and so on. That’s why I can see the process running in
the container from my normal shell - the container’s namespace is a child of the
main namespace all of my desktop software is running in.&lt;/p&gt;
&lt;p&gt;Linux supports - and container software makes use of - a bunch of other namespaces too:
mount points, network, UTS&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a class=&#34;footnote-ref&#34; href=&#34;#fn:3&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;, cgroups, and more. These all play a part in isolating
a container from the system it is running on.&lt;/p&gt;
&lt;p&gt;You can manually run a process with &lt;code&gt;unshare(1)&lt;/code&gt; to “unshare” some namespaces from
the parent process. For example if I run &lt;code&gt;unshare -fp --mount-proc ps a&lt;/code&gt;, it
looks very similar to running ps instead the container:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;    PID TTY      STAT   TIME COMMAND
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;      1 pts/4    R+     0:00 ps a
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So: a process running inside a container is just a heavily namespaced process running
otherwise normally in the operating system. No voodoo magic here!&lt;/p&gt;
&lt;h3 id=&#34;what-about-the-filesystem-what-are-images&#34;&gt;What about the filesystem? What are ‘images’?&lt;/h3&gt;
&lt;p&gt;Containers run in their own mount namespace meaning mount points can be different
inside the container to those on the host. This means the container can have a different
&lt;code&gt;/&lt;/code&gt; mounted to the host, effectively giving it its own filesystem.&lt;/p&gt;
&lt;p&gt;The root filesystem of the container is defined in the container’s image. If I
use Docker to run a container using the Ubuntu image (&lt;code&gt;docker run ubuntu&lt;/code&gt;), the root
filesystem inside that container will resemble a minimal ubuntu install. Note
that this is just the filesystem: the container doesn’t have its own kernel.&lt;/p&gt;
&lt;p&gt;You might be thinking that sounds pretty inefficient. Downloading Ubuntu is definitely
not instant, and doing it for every application you run would be insane! Quite.
Containers solve this by using filesystem &lt;strong&gt;layers&lt;/strong&gt;. These are stacked on top of
one another to create the final filesystem. Each layer can be retrieved and
cached independently of all others.&lt;/p&gt;
&lt;p&gt;Say (for simplicity) that the Ubuntu image is a single layer. If I run one container
with that image, then the layer will be downloaded and cached once. If I run three
hundred containers with that image then the layer will be downloaded and cached once.
Even better, if I use another image that’s based on Ubuntu but adds some software
on top, only the “on top” layer will be downloaded if I already have the relevant
“Ubuntu” layer cached.&lt;/p&gt;
&lt;p&gt;If all the layers are cached, what happens when you change a file? This is dealt
with using the copy-on-write technique: when you modify a file it is copied from
the source layer and the changes are only made in a new layer. This is handled
by the OverlayFS filesystem which is part of the mainline kernel.&lt;/p&gt;
&lt;p&gt;When a container is running, changes made to its filesystem are temporary, and
do not persist across container restarts. To persist data - or introduce new
data to a container - you can mount volumes. How this works varies depending
on how you’re running your container, but at the basic level it is pretty much
the same as bind-mounting (&lt;code&gt;mount -o bind&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;You may be familiar with using &lt;code&gt;chroots&lt;/code&gt; to change the apparent root directory
of processes, perhaps with full-blown “jails” built on top. Containers offer
much better isolation thanks to the use of namespaces. Instead of being
constrained to a portion of the host’s filesystem, they don’t even have
it mounted! Containers also get to specify their environment - if they
expect in certain places, for example - instead of the sysadmin having to
manually set up the chroot. Finally, containers offer much more fine-grained
control over what processes can do (if you want it), and allow much more
advanced use-cases such as inter-container networking.&lt;/p&gt;
&lt;p&gt;Images and filesystems employ a little magic to ensure that layers are reusable
and cacheable, but again there’s nothing terribly special about them: a container
has a filesystem that appears to it to work the same way as a filesystem on
the host, and it’s using a standard filesystem shipped with the kernel.&lt;/p&gt;
&lt;h3 id=&#34;how-about-networking&#34;&gt;How about networking?&lt;/h3&gt;
&lt;p&gt;Again, networking is namespaced, so a container has its own network stack,
its own virtual network interface, its own IP address and so on. How that network
interacts with your real network depends on how you’re running the container.
Docker, for example, can add iptables rules to NAT traffic between containers&amp;#39;
networks and the outside world.&lt;/p&gt;
&lt;p&gt;Containers can generally be connected into networks, and can communicate
amongst themselves without the traffic actually leaving the host machine.
This allows you to, for example, run a SQL database and connect it to
a web application without ever exposing the database to the outside world.
Moreover, as well as being isolated from the outside world, it’s isolated
from other containers in other networks. If one of your applications has a crazy bug
or is compromised, this significantly limits the damage it can do.&lt;/p&gt;
&lt;p&gt;You have to explicitly opt in to “publishing” ports from a container, which
exposes them to the outside world (either directly, or via a load balancer
or some other middle-man, depending on how you’re running the container).
This means you can pick and chose how the outside world sees the app you’re
deploying: if it’s a web service that listens on both port 443 and port 80,
you can chose to only expose the encrypted port.&lt;/p&gt;
&lt;p&gt;If you run some containers and create some networks, you can see the
interfaces and bridges on the host:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ ip l
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;...
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;6: br-2405a8cc0445: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu 1500 qdisc noqueue state UP mode DEFAULT group default 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;    link/ether 02:42:3e:fa:23:62 brd ff:ff:ff:ff:ff:ff
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;8: veth8ed0735@if7: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu 1500 qdisc noqueue master br-2405a8cc0445 state UP mode DEFAULT group default 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;    link/ether b2:c1:5d:55:26:9b brd ff:ff:ff:ff:ff:ff link-netnsid 2
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;10: veth541d84b@if9: &amp;lt;BROADCAST,MULTICAST,UP,LOWER_UP&amp;gt; mtu 1500 qdisc noqueue master br-9d7bc4024c1a state UP mode DEFAULT group default 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;    link/ether 86:aa:5f:ee:da:1a brd ff:ff:ff:ff:ff:ff link-netnsid 1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This shows a bridge and two virtual NICs, just the same as if you’d manually created
them (albeit with seemingly random names). So: as before, nothing special here.&lt;/p&gt;
&lt;h3 id=&#34;docker-compose-k8s-floccinaucinihilipilification&#34;&gt;Docker? Compose? K8s? Floccinaucinihilipilification?&lt;/h3&gt;
&lt;p&gt;(OK, Floccinaucinihilipilification isn’t actually a container technology, as far
as I’m aware.)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Docker&lt;/strong&gt; is the most popular &lt;em&gt;container engine&lt;/em&gt; - that is, the bit of software
that actually runs containers. It’s responsible for setting up all those namespaces
we found, downloading and caching image layers, and actually starting and stopping
the processes. Docker runs as a system-wide daemon - when you run a command like
&lt;code&gt;docker run ubuntu&lt;/code&gt; it actually just instructs the daemon to do the work.&lt;/p&gt;
&lt;p&gt;There are several alternatives to Docker for running containers; one interesting one
is &lt;a href=&#34;https://podman.io/&#34;&gt;Podman&lt;/a&gt; which runs containers without a daemon. Container
engines have all standardised around the same image format looked after by the
&lt;a href=&#34;https://www.opencontainers.org/&#34;&gt;Open Container Initiative&lt;/a&gt;, so an image you build
in Docker can be used in Podman, or pretty much any other engine.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Docker compose&lt;/strong&gt; is a tool for defining and running multiple-container applications.
I mentioned earlier running a database alongside a webapp - in practice to do this
you’re going to have to configure a network for them, configure a mount point for
the database to persist its data on, pass credentials in to both the database and
the application, and so on. Doing all that by hand is tedious and error prone.&lt;/p&gt;
&lt;p&gt;Docker compose lets you write “compose files”, which are simple yaml descriptions
of the containers you wish to run, their properties, and details about any volumes
or networks you may want. Out of the box, docker-compose will create a default
network for each compose file you run so the containers within it can communicate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Kubernetes&lt;/strong&gt;  is a container &lt;em&gt;orchestrator&lt;/em&gt;, designed to automate deployment
and management of large numbers of containers. It works with Docker under the
hood, but provides a huge amount of tooling on top to allow you to deploy
applications and manage their dependencies. It runs across multiple physical
(or virtual) machines (while still allowing containers to communicate privately),
and can support massive workloads by scaling out services (running multiple copies
of a container on different hosts) and load balancing. Kubernetes is sometimes
shorted to &lt;strong&gt;k8s&lt;/strong&gt; (as in &lt;code&gt;K&lt;/code&gt; - 8 elided letters - &lt;code&gt;s&lt;/code&gt;) because computer people
don’t like long words.&lt;/p&gt;
&lt;h3 id=&#34;ok-they-make-sense-now-but-why-bother&#34;&gt;OK, they make sense now. But why bother?&lt;/h3&gt;
&lt;p&gt;Hopefully if you’ve read this far you’ve already picked up on some of the potential
benefits, but this is my personal list:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Isolation&lt;/strong&gt;. If I run some software in a container, there is very little it
can do to upset me. It’s not going to leave bits of itself all over my filesystem,
it can’t steal all of the secrets in my home directory, I can even limit its CPU
and memory resources if I want. If I decide to stop running it, I just delete
the container and it is completely gone: no trace remains.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Ease of use&lt;/strong&gt;. If you give me a container image I have a very good idea of how
to run it already. I might need to do some minor configuration to expose ports
or mount volumes, but there’s no question about how to run it, how to make it
automatically start, and there’s no “installation” procedure. If I want to
then swap it with an alternative (say, move from MySQL to MariaDB), it’s
potentially just a case of changing the name of the image I pull.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Dependencies included&lt;/strong&gt;. Containers just &lt;em&gt;run&lt;/em&gt;. Python 2 software includes
Python 2 and just work. Python 3 software includes Python 3 and just work.
I don’t have a massive headache trying to run both at the same time, because
they take care of their own messes. Similarly I’m not going to have to install
&lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;cargo&lt;/code&gt; or &lt;code&gt;composer&lt;/code&gt; to pull in dependencies for an application:
that’s going to have been done in the build process.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Reproducibility&lt;/strong&gt;. As a fallout from having dependencies included and being
isolated from everything else, containers give you amazing reproducibility.
If it “works on your machine” in a container, it’ll almost certainly work in
production because it’s the exact same environment.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Standardisation&lt;/strong&gt;. At the start of this article I called containers standard
units of software. One of my favourite advantages of containers is that you
basically get an API to list all the software you’re running. Most container
engines let you supply labels attached to containers as well, so you can add
your own annotations. I use this to annotate services which expose HTTP
endpoints, and I have a tool that automatically generates SSL certificates
for them and configures haproxy to route traffic to them. I can’t imagine
how I’d do this without containers - I imagine it’d involve a lot of
manual work.&lt;/p&gt;
&lt;p&gt;Hopefully this has helped demystify containers a little. If you feel like
I’ve missed something important out, or I’ve left you more confused than
when you started, feel free to drop me a note using the feedback form below.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr/&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;
&lt;p&gt;OK, maybe you should care if you’re deploying something written in crazy languages like PHP. &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:1&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;
&lt;p&gt;Some multi-process apps, such as web browsers, are starting to use namespaces to enhance security,
as do certain package systems like Flatpak &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:2&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:3&#34;&gt;
&lt;p&gt;“Unix timesharing system”; in practice this means having a separate hostname &lt;a class=&#34;footnote-backref&#34; href=&#34;#fnref:3&#34; role=&#34;doc-backlink&#34;&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content>
    </entry>
    <entry>
        <title>Debugging beyond the debugger</title>
        <link href="https://chameth.com/debugging-beyond-the-debugger/"/>
        <updated>2019-05-08T00:00:00Z</updated>
        <id>https://chameth.com/debugging-beyond-the-debugger/</id>
        <content xml:lang="en" type="html">&lt;figure class=&#34;image right&#34;&gt;
  &lt;picture&gt;
      &lt;source srcset=&#34;https://chameth.com/debugging-beyond-the-debugger/tools.avif&#34; type=&#34;image/avif&#34;/&gt;
      &lt;source srcset=&#34;https://chameth.com/debugging-beyond-the-debugger/tools.webp&#34; type=&#34;image/webp&#34;/&gt;
      &lt;img src=&#34;https://chameth.com/debugging-beyond-the-debugger/tools.jpg&#34; alt=&#34;Collection of tools hanging on a wall&#34; loading=&#34;lazy&#34; width=&#34;300&#34; height=&#34;396&#34;/&gt;
  &lt;/picture&gt;
  &lt;figcaption&gt;&lt;p&gt;Real-life debugging tools&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;Most programming — and sysadmin — problems can be debugged in a
fairly straight forward manner using logs, print statements,
educated guesses, or an actual debugger. Sometimes, though, the
problem is more elusive. There’s a wider box of tricks that can
be employed in these cases but I’ve not managed to find a nice
overview of them, so here’s mine. I’m mainly focusing on Linux
and similar systems, but there tend to be alternatives available
for other Operating Systems or VMs if you seek them out.&lt;/p&gt;
&lt;h3 id=&#34;networking&#34;&gt;Networking&lt;/h3&gt;
&lt;h4 id=&#34;tcpdump&#34;&gt;tcpdump&lt;/h4&gt;
&lt;p&gt;&lt;code&gt;tcpdump&lt;/code&gt; prints out descriptions of packets on a network interface. You can
apply filters to limit which packets are displayed, chose to dump the entire
content of the packet, and so forth.&lt;/p&gt;
&lt;!--more--&gt;
&lt;p&gt;Typical usage might look something like:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# tcpdump -nSi eth0 port 80
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;16:03:35.577781 IP6 2001:db8::1.54742 &amp;gt; 2001:db8::2.80: Flags [S], seq 2815779044, win 64800, options [mss 1440,sackOK,TS val 2378811665 ecr 0,nop,wscale 7], length 0
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;16:03:35.586853 IP6 2001:db8::2.80 &amp;gt; 2001:db8::1.54742: Flags [S.], seq 1522609102, ack 2815779045, win 28560, options [mss 1440,sackOK,TS val 3063610173 ecr 2378811665,nop,wscale 7], length 0
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;16:03:35.586877 IP6 2001:db8::1.54742 &amp;gt; 2001:db8::2.80: Flags [.], ack 1522609103, win 507, options [nop,nop,TS val 2378811674 ecr 3063610173], length 0
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;16:03:35.620678 IP6 2001:db8::1.54742 &amp;gt; 2001:db8::2.80: Flags [P.], seq 2815779045:2815779399, ack 1522609103, win 507, options [nop,nop,TS val 2378811708 ecr 3063610173], length 354: HTTP: GET / HTTP/1.1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here you can see the start of a plaintext HTTP request: the three-way
handshake as the TCP connection is established followed by a GET request.
Even if the data is encrypted as it will be in most cases, it’s often useful
to see the “shape” of the transmissions: did the client start sending data
when it connected, did the server ever respond, etc.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://danielmiessler.com/study/tcpdump/&#34;&gt;Daniel Miessler has a good tutorial on tcpdump&lt;/a&gt;
if you’re not familiar with it and don’t want to jump straight into the man
page.&lt;/p&gt;
&lt;h5 id=&#34;-with-docker&#34;&gt;… with Docker&lt;/h5&gt;
&lt;p&gt;Docker sets up separate network namespaces for each container. To see the
traffic across the interfaces of a single container you can &lt;code&gt;nsenter&lt;/code&gt; the
container’s network namespace:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# nsenter -t $(docker inspect --format &amp;#39;{{.State.Pid}}&amp;#39; my_container) -n tcpdump -nS port 80
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This retrieves the PID for the container, and tells &lt;code&gt;nsenter&lt;/code&gt; to enter the
network (&lt;code&gt;-n&lt;/code&gt;) namespace from the given target (&lt;code&gt;-t&lt;/code&gt;) PID, and then run the
given command (in this case &lt;code&gt;tcpdump ...&lt;/code&gt;).&lt;/p&gt;
&lt;h4 id=&#34;openssl-s-client--s-server&#34;&gt;openssl s_client / s_server&lt;/h4&gt;
&lt;p&gt;When a connection is using TLS it’s often useful to try connecting to the
server and see what certificate it presents, algorithms it negotiates, and
so forth. OpenSSL offers two useful subcommands which can help with this:
&lt;code&gt;s_client&lt;/code&gt; for connecting as a client, and &lt;code&gt;s_server&lt;/code&gt; for listening to
connections.&lt;/p&gt;
&lt;p&gt;For example, using &lt;code&gt;s_client&lt;/code&gt; to connect to &lt;code&gt;google.com&lt;/code&gt; on the standard
HTTPS port shows us details about the server cert and its verification
status:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ openssl s_client -connect google.com:443
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;CONNECTED(00000003)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;verify return:1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;depth=1 C = US, O = Google Trust Services, CN = Google Internet Authority G3
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;verify return:1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = *.google.com
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;verify return:1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;Certificate chain
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; 0 s:C = US, ST = California, L = Mountain View, O = Google LLC, CN = *.google.com
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;   i:C = US, O = Google Trust Services, CN = Google Internet Authority G3
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; 1 s:C = US, O = Google Trust Services, CN = Google Internet Authority G3
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;   i:OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Whereas connecting to my webserver and providing an unknown host in the SNI
field results in an SSL alert 112 (“The server name sent was not recognized”)
and no server certificate is sent:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ openssl s_client -connect chameth.com:443 -servername example.com
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;CONNECTED(00000003)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;140384831313024:error:14094458:SSL routines:ssl3_read_bytes:tlsv1 unrecognized name:../ssl/record/rec_layer_s3.c:1536:SSL alert number 112
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;no peer certificate available
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;---
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Often if you hit this kind of alert in an application the exact error will be
lost somewhere in the many layers between the SSL library and the logs, so
being able to directly connect and test can help diagnose a lot of issues.&lt;/p&gt;
&lt;p&gt;Once a connection is established you can read and write plain text and it
will be encrypted and decrypted automatically.&lt;/p&gt;
&lt;h4 id=&#34;java-apps&#34;&gt;Java apps&lt;/h4&gt;
&lt;p&gt;If a Java app is involved in the connection, you can enable a lot of built-in
debugging with a simple JVM property: &lt;code&gt;javax.net.debug&lt;/code&gt;. You can tweak
what exactly gets logged, but the easiest thing to do is just set the property
to &lt;code&gt;all&lt;/code&gt; and you’ll see information about certificate chains, verification,
and packet dumps:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ java -Djavax.net.debug=all -jar ....
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;found key for : duke
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;chain [0] = [
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;[
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;  Version: V1
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;  Subject: CN=Duke, OU=Java Software, O=&amp;#34;Sun Microsystems, Inc.&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;  L=Cupertino, ST=CA, C=US
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;More information about Java’s debugging options is available on
&lt;a href=&#34;https://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/ReadDebug.html&#34;&gt;docs.oracle.com&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&#34;thread-and-core-dumps&#34;&gt;Thread and core dumps&lt;/h3&gt;
&lt;p&gt;Higher-level languages frequently provide an interactive way to dump the
current execution state of all of their threads (a “thread dump”). This
is useful to spot deadlocks, some types of race conditions, and as a
quick and dirty method of investigating hangs or excessive CPU usage.&lt;/p&gt;
&lt;p&gt;With both Java and Go applications you can send a QUIT signal to have a
thread dump printed out; Go applications will quit after doing so, Java
ones will carry on running. At most terminals you can hit &lt;code&gt;Ctrl&lt;/code&gt; and &lt;code&gt;\&lt;/code&gt; to
send a QUIT signal.&lt;/p&gt;
&lt;p&gt;For Java you can also use the &lt;code&gt;jstack&lt;/code&gt; tool from the JDK to dump threads
by PID; this can be useful if the application is running in the background
or has redirected sysout:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ jstack 8321
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;Attaching to process ID 8321, please wait...
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;Debugger attached successfully.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;Client compiler detected.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;Thread t@5: (state = BLOCKED)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; - java.lang.Object.wait(long) @bci=-1107318896 (Interpreted frame)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; - java.lang.Object.wait(long) @bci=0 (Interpreted frame)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; - java.lang.ref.ReferenceQueue.remove(long) @bci=44, line=116 (Interpreted frame)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; - java.lang.ref.ReferenceQueue.remove() @bci=2, line=132 (Interpreted frame)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt; - java.lang.ref.Finalizer$FinalizerThread.run() @bci=3, line=159 (Interpreted frame)
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;A core dump provides more complete information about the state of a process,
but is often more complex to interpret. The &lt;code&gt;gcore&lt;/code&gt; utility from GDB will
create a core dump of a process with a given PID. You can then generally
load the core file using your normal debugger, depending on the language
in question.&lt;/p&gt;
&lt;h3 id=&#34;system-calls&#34;&gt;System calls&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;strace&lt;/code&gt; is the swiss army knife for seeing what a process is doing. It
details each system call made by a program (you can filter them down, of
course). For example:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ strace -e read curl https://google.com/
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;read(3, &amp;#34;\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0&amp;gt;\0\1\0\0\0 \236\0\0\0\0\0\0&amp;#34;..., 832) = 832
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;read(3, &amp;#34;\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0&amp;gt;\0\1\0\0\0P!\0\0\0\0\0\0&amp;#34;..., 832) = 832
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;read(3, &amp;#34;\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0&amp;gt;\0\1\0\0\0\200l\2\0\0\0\0\0&amp;#34;..., 832) = 832
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;read(3, &amp;#34;\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0&amp;gt;\0\1\0\0\0\20Q\0\0\0\0\0\0&amp;#34;..., 832) = 832
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;read(3, &amp;#34;\0\0\0\0\0\0\0\4\25\345\366\302\273sE6\365wI\225\321|\3435Z\362\216\372\215\251aO&amp;#34;..., 253) = 253
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&amp;lt;HTML&amp;gt;&amp;lt;HEAD&amp;gt;&amp;lt;meta http-equiv=&amp;#34;content-type&amp;#34; content=&amp;#34;text/html;charset=utf-8&amp;#34;&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&amp;lt;TITLE&amp;gt;301 Moved&amp;lt;/TITLE&amp;gt;&amp;lt;/HEAD&amp;gt;&amp;lt;BODY&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&amp;lt;H1&amp;gt;301 Moved&amp;lt;/H1&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;The document has moved
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&amp;lt;A HREF=&amp;#34;https://www.google.com/&amp;#34;&amp;gt;here&amp;lt;/A&amp;gt;.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;read(3, &amp;#34;\27\3\3\0!&amp;#34;, 5)                = 5
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;a href=&#34;http://www.brendangregg.com/blog/2014-05-12/strace-wow-much-syscall.html&#34;&gt;Brendan Gregg&lt;/a&gt;
has a nice guide on &lt;code&gt;strace&lt;/code&gt; and alternatives.&lt;/p&gt;
&lt;h4 id=&#34;-with-docker-1&#34;&gt;… with docker&lt;/h4&gt;
&lt;p&gt;When the application is running in docker you can usually just &lt;code&gt;strace&lt;/code&gt; it
from the host with the correct PID
(from e.g. &lt;code&gt;docker inspect --format &amp;#39;{{.State.Pid}}&amp;#39; my_container&lt;/code&gt;).
Sometimes you may need to trace the startup of an application though, which is
a bit trickier. Instead you can run a new container using the same PID
namespace as your target, and the permissions needed to &lt;code&gt;strace&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ docker run --rm -it --pid=container:my_container \
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;  --net=container:my_container \
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;  --cap-add sys_admin \
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;  --cap-add sys_ptrace \
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;  alpine
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;From within the new container you can install strace, and trace any running
program within the target container using &lt;code&gt;strace -p&lt;/code&gt; as normal. To start a
new program you need access to the target container’s filesystem, which you
can get to via &lt;code&gt;/proc/1/root&lt;/code&gt; (PID &lt;code&gt;1&lt;/code&gt; being the main process that docker
started in the target container).&lt;/p&gt;
&lt;h3 id=&#34;files&#34;&gt;Files&lt;/h3&gt;
&lt;p&gt;Sometimes the problem might relate to file access. There are a couple of
straight forward — but nonetheless useful — tools which might help here.
&lt;code&gt;inotifywait&lt;/code&gt; uses the Linux &lt;code&gt;inotify&lt;/code&gt; subsystem to watch files or directories
for operations. For example:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ inotifywait -mr site/content
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;Setting up watches.  Beware: since -r was given, this may take a while!
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;Watches established.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;site/content/post/ MODIFY 2019-05-08-debugging-beyond-the-debugger.md
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;site/content/post/ OPEN 2019-05-08-debugging-beyond-the-debugger.md
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;site/content/post/ MODIFY 2019-05-08-debugging-beyond-the-debugger.md
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;site/content/post/ MODIFY 2019-05-08-debugging-beyond-the-debugger.md
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;site/content/post/ CLOSE_WRITE,CLOSE 2019-05-08-debugging-beyond-the-debugger.md
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here the &lt;code&gt;-m&lt;/code&gt; switch makes &lt;code&gt;inotifywait&lt;/code&gt; monitor the files forever (instead
of exiting on the first modification, which is the normal behaviour) and &lt;code&gt;r&lt;/code&gt;
makes it recurse into the directory and monitor each file and subdirectory in
there.&lt;/p&gt;
&lt;p&gt;If you want to see what processes currently have a file open, &lt;code&gt;fuser&lt;/code&gt; is the
go-to tool. For example:&lt;/p&gt;
&lt;pre class=&#34;chroma-chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;$ fuser -v /
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;                     USER PID ACCESS COMMAND
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;/:                   root     kernel mount /
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;                     chris      2961 .rc.. systemd
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;                     chris      2986 .r... gdm-x-session
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;                     chris      2994 .r... dbus-daemon
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;                     chris      3001 .r... gnome-session-b
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;chroma-line&#34;&gt;&lt;span class=&#34;chroma-cl&#34;&gt;# ...
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h3 id=&#34;honourable-mentions&#34;&gt;Honourable mentions&lt;/h3&gt;
&lt;p&gt;These aren’t really debugging tools, but I feel it’s worth mentioning as
they often feature somewhere along the debugging-of-weird-problems journey.&lt;/p&gt;
&lt;p&gt;I’ve seen some weird and wonderful problems happen
because a disk is full, so a quick &lt;code&gt;df&lt;/code&gt; early on in the debugging process
never hurts. Some apps may hang, some may corrupt their config, some may
fall over and die; sometimes the manner in which they fail doesn’t obviously
point to a disk space issue.&lt;/p&gt;
&lt;p&gt;Another issue that comes up now and then — especially inside VMs or
other environment that don’t have a decent amount of “noise” happening —
is entropy exhaustion. A quick look at &lt;code&gt;/proc/sys/kernel/random/entropy_avail&lt;/code&gt;
should be enough to confirm that everything is ticking along nicely. If it’s
exceedingly low then you may find that anything involving random number
generation stalls (TLS connections for example).&lt;/p&gt;
</content>
    </entry>
</feed>
