<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Phui-Hock</title>
    <description>The latest articles on DEV Community by Phui-Hock (@phuihock).</description>
    <link>https://dev.to/phuihock</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F376875%2F08fd6d96-c85a-4b66-8b89-7371541b7736.jpeg</url>
      <title>DEV Community: Phui-Hock</title>
      <link>https://dev.to/phuihock</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/phuihock"/>
    <language>en</language>
    <item>
      <title>Building a non-root Docker container</title>
      <dc:creator>Phui-Hock</dc:creator>
      <pubDate>Thu, 30 Apr 2020 16:30:56 +0000</pubDate>
      <link>https://dev.to/phuihock/building-a-non-root-docker-container-29ah</link>
      <guid>https://dev.to/phuihock/building-a-non-root-docker-container-29ah</guid>
      <description>&lt;p&gt;Up until recently, I have been building container images with root and I run them as such, which is (of course) a very poor practice. &lt;/p&gt;

&lt;p&gt;I started learning how to build non-root container a few days ago. In many examples, an arbitrary user is created. Files and directories are created and permissions are set with &lt;code&gt;chmod&lt;/code&gt;. This makes &lt;code&gt;Dockerfile&lt;/code&gt; slightly bloated with commands which make the image building process look like system administration.&lt;/p&gt;

&lt;p&gt;The first question that comes to mind is, "Can't I use any one of the existing user in the container?" I opened up &lt;code&gt;/etc/passwd&lt;/code&gt; in both &lt;code&gt;ubuntu:18.04&lt;/code&gt; and &lt;code&gt;alpine:3.11&lt;/code&gt;, 2 base images that I commonly use. I found &lt;code&gt;nobody&lt;/code&gt;, with id 65534 defined in both images. &lt;/p&gt;

&lt;p&gt;Least privileged user? This is it.&lt;/p&gt;

&lt;p&gt;I use Docker, VS Code with Remote Development extension for development. It allows me to attach to a running container and develop inside it. The source code is mounted into the container using bind mount. &lt;/p&gt;

&lt;p&gt;Now, how do I save my files if the container runs as &lt;code&gt;nobody&lt;/code&gt; while the source files on my host owned by me (another user)? It turns out that there is an (sort of) easy solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Dockerfile
---

from ubuntu:18.04
ENV HOME /app
USER nobody
WORKDIR ${HOME}
COPY src ./src/
ENTRYPOINT ["./src/helloworld.sh"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose.yml
---

version: "3.7"

services:
    app:
        build:
            context: .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose.override.yml
---

version: "3.7"

services:
    app:
        user: "nobody:${GROUP}"
        volumes:
            - .:/app
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With the files above in the project directory, the first step is to update the source folder with the group writable permission recursively, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ chmod -R g+wX &amp;lt;project dir&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, I run the container as the same group as the host user's group, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ GROUP=$(id -g) docker-compose up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, I can attach to the docker container from VS Code using Remote Development extension and develop inside it. When the image is deployed, it runs as &lt;code&gt;nobody&lt;/code&gt;, which should be safer than running as &lt;code&gt;root&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That said, I am still not sure if this is the best approach to non-root container. But this seems to work for my development workflow as well as deployment (with/without swarm mode)&lt;/p&gt;

&lt;p&gt;Comments are welcomed.&lt;/p&gt;

</description>
      <category>docker</category>
    </item>
  </channel>
</rss>
