<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Podman on Carlos Vaz</title>
    <link>https://carlosvaz.com/tags/podman/</link>
    <description>Recent content in Podman on Carlos Vaz</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-US</language>
    <managingEditor>carlos@carjorvaz.com (Carlos Vaz)</managingEditor>
    <webMaster>carlos@carjorvaz.com (Carlos Vaz)</webMaster>
    <lastBuildDate>Sun, 06 Aug 2023 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://carlosvaz.com/tags/podman/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Rootless Podman and Docker-Compose on NixOS</title>
      <link>https://carlosvaz.com/posts/rootless-podman-and-docker-compose-on-nixos/</link>
      <pubDate>Sun, 06 Aug 2023 00:00:00 +0000</pubDate><author>carlos@carjorvaz.com (Carlos Vaz)</author>
      <guid>https://carlosvaz.com/posts/rootless-podman-and-docker-compose-on-nixos/</guid>
      <description>&lt;p&gt;At my university&amp;rsquo;s Computer Science and Engineering department, we want to allow students to use containers in the lab computers while not giving them root priviliges, as running Docker usually requires.&lt;/p&gt;&#xA;&lt;p&gt;In the past, on our Ubuntu systems, we had a setup of rootless-Docker I&amp;rsquo;m not sure ever worked properly.&#xA;There also seems to exist an option on NixOS to enable rootless-Docker, but we also had some issues in using it.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>At my university&rsquo;s Computer Science and Engineering department, we want to allow students to use containers in the lab computers while not giving them root priviliges, as running Docker usually requires.</p>
<p>In the past, on our Ubuntu systems, we had a setup of rootless-Docker I&rsquo;m not sure ever worked properly.
There also seems to exist an option on NixOS to enable rootless-Docker, but we also had some issues in using it.</p>
<p>So we went with Podman, a Docker alternative that&rsquo;s more ready to be run rootless by design.
Also, Docker can just be aliased to Podman and the experience is so identical that the students may not even notice that they&rsquo;re not running Docker proper.</p>
<p>Enabling Podman on NixOS is quite trivial but having it work in a rootless environment requires more configuration:</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>{ config<span style="color:#f92672">,</span> lib<span style="color:#f92672">,</span> pkgs<span style="color:#f92672">,</span> <span style="color:#f92672">...</span> }:
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>  virtualisation <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>    containers<span style="color:#f92672">.</span>enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>    containers<span style="color:#f92672">.</span>storage<span style="color:#f92672">.</span>settings <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>      storage <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>        driver <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;overlay&#34;</span>;
</span></span><span style="display:flex;"><span>        runroot <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;/run/containers/storage&#34;</span>;
</span></span><span style="display:flex;"><span>        graphroot <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;/var/lib/containers/storage&#34;</span>;
</span></span><span style="display:flex;"><span>        rootless_storage_path <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;/tmp/containers-$USER&#34;</span>;
</span></span><span style="display:flex;"><span>        options<span style="color:#f92672">.</span>overlay<span style="color:#f92672">.</span>mountopt <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;nodev,metacopy=on&#34;</span>;
</span></span><span style="display:flex;"><span>      };
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    oci-containers<span style="color:#f92672">.</span>backend <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;podman&#34;</span>;
</span></span><span style="display:flex;"><span>    podman <span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>      enable <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>      enableNvidia <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>      dockerCompat <span style="color:#f92672">=</span> <span style="color:#66d9ef">true</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#75715e"># extraPackages = [ pkgs.zfs ]; # Required if the host is running ZFS</span>
</span></span><span style="display:flex;"><span>    };
</span></span><span style="display:flex;"><span>  };
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  environment<span style="color:#f92672">.</span>systemPackages <span style="color:#f92672">=</span> <span style="color:#66d9ef">with</span> pkgs; [ docker-compose ];
</span></span><span style="display:flex;"><span>  environment<span style="color:#f92672">.</span>extraInit <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    if [ -z &#34;$DOCKER_HOST&#34; -a -n &#34;$XDG_RUNTIME_DIR&#34; ]; then
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">      export DOCKER_HOST=&#34;unix://$XDG_RUNTIME_DIR/podman/podman.sock&#34;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    fi
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">  &#39;&#39;</span>;
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div><p>We set the <code>DOCKER_HOST</code> environment variable on user login so that <code>docker-compose</code> works using the rootless Podman socket.</p>
<p>Because students log into our lab computers using Kerberos, they don&rsquo;t have <code>/etc/subuid</code> and <code>/etc/subgid</code> entries automatically created for them.
So, in this particular setup, we also have a <code>pam_exec</code> hook to create the entries for them, otherwise Podman won&rsquo;t work.</p>





<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-nix" data-lang="nix"><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>  <span style="color:#75715e"># subidappend is a script we wrote that adds subuid and subgid entries on user login</span>
</span></span><span style="display:flex;"><span>  security<span style="color:#f92672">.</span>pam<span style="color:#f92672">.</span>services<span style="color:#f92672">.</span>login<span style="color:#f92672">.</span>text <span style="color:#f92672">=</span> lib<span style="color:#f92672">.</span>mkDefault (lib<span style="color:#f92672">.</span>mkAfter <span style="color:#e6db74">&#39;&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    session optional pam_exec.so </span><span style="color:#e6db74">${</span>pkgs<span style="color:#f92672">.</span>subidappend<span style="color:#e6db74">}</span><span style="color:#e6db74">/bin/subidappend
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">  &#39;&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  security<span style="color:#f92672">.</span>pam<span style="color:#f92672">.</span>services<span style="color:#f92672">.</span>sshd<span style="color:#f92672">.</span>text <span style="color:#f92672">=</span> lib<span style="color:#f92672">.</span>mkDefault (lib<span style="color:#f92672">.</span>mkAfter <span style="color:#e6db74">&#39;&#39;
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">    session optional pam_exec.so </span><span style="color:#e6db74">${</span>pkgs<span style="color:#f92672">.</span>subidappend<span style="color:#e6db74">}</span><span style="color:#e6db74">/bin/subidappend
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">  &#39;&#39;</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#75715e"># Clean subuids and gids on boot</span>
</span></span><span style="display:flex;"><span>  systemd<span style="color:#f92672">.</span>tmpfiles<span style="color:#f92672">.</span>rules <span style="color:#f92672">=</span>
</span></span><span style="display:flex;"><span>    [ <span style="color:#e6db74">&#34;f+  /etc/subuid 0644 root root -&#34;</span> <span style="color:#e6db74">&#34;f+  /etc/subgid 0644 root root -&#34;</span> ];
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div><h2 id="closing-remarks">Closing remarks</h2>
<p>We find this setup to be quite robust, it tends to just work with most Docker workflows, with the execption of using ports under 1024.</p>
<p>This allows students to run almost everything they wish to run on the lab computers, while still not having root privileges.
The Nix package manager also helps with this.
More importantly, students can now easily run services like Nginx and PostgreSQL, which are not trivial at all to run on the host system without root.</p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://github.com/containers/podman/blob/main/docs/tutorials/rootless_tutorial.md">https://github.com/containers/podman/blob/main/docs/tutorials/rootless_tutorial.md</a></li>
<li><a href="https://major.io/p/rootless-container-management-with-docker-compose-and-podman/">https://major.io/p/rootless-container-management-with-docker-compose-and-podman/</a></li>
<li><a href="https://nixos.wiki/wiki/Podman">https://nixos.wiki/wiki/Podman</a></li>
</ul>
]]></content:encoded>
    </item>
  </channel>
</rss>
