<?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>Forem: Marc Ziel</title>
    <description>The latest articles on Forem by Marc Ziel (@marzelin).</description>
    <link>https://forem.com/marzelin</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%2F70088%2F6926f2c2-c88c-4cfa-8e47-ae3760636a55.png</url>
      <title>Forem: Marc Ziel</title>
      <link>https://forem.com/marzelin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/marzelin"/>
    <language>en</language>
    <item>
      <title>Declarative Programming in React</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Thu, 26 Aug 2021 17:41:30 +0000</pubDate>
      <link>https://forem.com/marzelin/what-declarative-means-in-react-lnj</link>
      <guid>https://forem.com/marzelin/what-declarative-means-in-react-lnj</guid>
      <description>&lt;p&gt;When you go to &lt;a href="https://reactjs.org"&gt;React.js front page&lt;/a&gt; you can read that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Declarative.&lt;br&gt;
React makes it painless to create interactive UIs. Design simple views for each state in your application, and React will efficiently update and render just the right components when your data changes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then you look at some example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"square"&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and start to wonder how this code can be considered declarative? Is there any truth in the description? Let's find out.&lt;/p&gt;

&lt;p&gt;Every app has state and logic. Logic can be expressed in declarative or imperative style.&lt;/p&gt;

&lt;p&gt;People have different opinions about CSS language but everybody agrees that it's declarative, so we'll begin with CSS code and compare it to React code to see if there are any similarities.&lt;/p&gt;

&lt;p&gt;This CSS code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.trendy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.fancy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be translated to pseudocode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;when element class is `trendy` its text color should be red
when element class is `fancy` its text color should be blue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React expects similar declarative description from you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//       declarative view description&lt;/span&gt;
&lt;span class="c1"&gt;//                   ↓&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hi&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt; &lt;span class="nx"&gt;rootEl&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The format of the view description that React uses is commonly known as VDOM and looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;type:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;props:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="err"&gt;children:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Hi"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's what JSX evaluates to.&lt;/p&gt;

&lt;p&gt;The previous code snippet can be expressed in pseudocode  as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;the view should be &amp;lt;div&amp;gt;Hi&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this isn't really useful. We don't want our view to be static, that's why we chose React instead of just writing HTML code. We want something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;when app state is `1` then the view should be &amp;lt;div&amp;gt;1&amp;lt;/div&amp;gt;
when app state is `2` then the view should be &amp;lt;div&amp;gt;2&amp;lt;/div&amp;gt;
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, the code it declarative but it isn't manageable (writing a clause for every possible number? No, thank you).&lt;/p&gt;

&lt;p&gt;Can we do better? How about this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;when component state is `n` then the view should be &amp;lt;div&amp;gt;{n}&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With just a single line of pseudocode we have all the numbers covered. This code is still declarative - it's equivalent to the previous pseudocode but a lot more succinct.&lt;/p&gt;

&lt;p&gt;In CSS you can encounter special kind of declarations that are applied based on some data, like the position of an element.&lt;/p&gt;

&lt;p&gt;Let's say you want every alternate &lt;code&gt;div&lt;/code&gt; to have gray text, instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;5&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;2&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt; &lt;span class="nt"&gt;-&lt;/span&gt; &lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c"&gt;/* or even better */&lt;/span&gt;
&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="nd"&gt;:nth-child&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;odd&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is there something similar in React? Well, yes - a component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a declarative function that describes the relation between the state and the view. So indeed, this is a declarative code. Whenever React needs to know how the current view should look like it fires up &lt;code&gt;Component&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There you have it: React components are just like sophisticated CSS declarations and when you write in React, you're writing declarative code.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>functional</category>
    </item>
    <item>
      <title>Create TLS / SSL Certificates for Local Development</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Mon, 23 Aug 2021 14:51:14 +0000</pubDate>
      <link>https://forem.com/marzelin/create-tls-ssl-certificates-for-local-development-1c8</link>
      <guid>https://forem.com/marzelin/create-tls-ssl-certificates-for-local-development-1c8</guid>
      <description>&lt;p&gt;When it comes to TLS certificates, there are a few tools such as &lt;a href=""&gt;&lt;code&gt;mkcert&lt;/code&gt;&lt;/a&gt; to generate them but I like to proceed with caution.&lt;/p&gt;

&lt;p&gt;The safest way would be to use &lt;code&gt;openssl&lt;/code&gt; directly, but this is a low-level tool with many options that requires specific knowledge to use it properly.&lt;/p&gt;

&lt;p&gt;The second best choice is to use the tool recommended by some authority in this area like &lt;a href="https://letsencrypt.org/docs/certificates-for-localhost/"&gt;let's encrypt&lt;/a&gt; so &lt;em&gt;let's&lt;/em&gt; see what they suggest:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you want a little more realism in your development certificates, you can use &lt;a href="https://github.com/jsha/minica"&gt;minica&lt;/a&gt; to generate your own local root certificate, and issue end-entity (aka leaf) certificates signed by it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So I'm going to use &lt;a href="https://github.com/jsha/minica"&gt;minica&lt;/a&gt; here.&lt;/p&gt;

&lt;p&gt;The tool itself is very easy to use, but first we need to build it from source (and probably take a look at the source code first). It's written in go, so we'll use &lt;a href="https://hub.docker.com/_/golang"&gt;golang container image&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# ./certs is the dir where we'll store certificates&lt;/span&gt;
podman run &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="nt"&gt;-it&lt;/span&gt; &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;pwd&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;/certs:/certs golang
&lt;span class="c"&gt;# from the container; get the source code:&lt;/span&gt;
go get github.com/jsha/minica
git clone https://github.com/jsha/minica.git
&lt;span class="c"&gt;# build binary file&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;minica &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; go build
&lt;span class="c"&gt;# generate the certificates&lt;/span&gt;
minica &lt;span class="nt"&gt;--domains&lt;/span&gt; &lt;span class="s1"&gt;'localhost,app.test,*.app.test'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
       &lt;span class="nt"&gt;--ip-addresses&lt;/span&gt; 127.0.0.1
&lt;span class="c"&gt;# move the certificates to the mounted directory&lt;/span&gt;
&lt;span class="nb"&gt;mv &lt;/span&gt;minica.pem localhost/&lt;span class="k"&gt;*&lt;/span&gt; /certs/
&lt;span class="nb"&gt;exit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The certificates should be in &lt;code&gt;./certs&lt;/code&gt; directory on the host.&lt;/p&gt;

&lt;p&gt;Now we can add &lt;code&gt;minica.pem&lt;/code&gt; to the browser we use for development. For Chrome-based browsers it's:&lt;br&gt;
&lt;em&gt;Settings -&amp;gt; Security -&amp;gt; Manage certificates -&amp;gt; Authorities&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;and use &lt;code&gt;cert.pem&lt;/code&gt; and &lt;code&gt;key.pem&lt;/code&gt; to authenticate the development server.&lt;/p&gt;

&lt;p&gt;When we exited the golang container we destroyed it along with every file it contained, especially &lt;code&gt;minica.key&lt;/code&gt;. This means that we won't be able to generate more certificates signed by this CA but if the file would fall into wrong hands it might be used against us for nefarious things like spoofing.&lt;/p&gt;

&lt;p&gt;This allows us to have our dev server running on &lt;code&gt;https&lt;/code&gt; (which means it closely resembles production environment) without decreasing the overall safety of our development machine.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tls</category>
      <category>security</category>
      <category>ssl</category>
    </item>
    <item>
      <title>Publishing a port on an existing docker container</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Thu, 12 Aug 2021 15:36:43 +0000</pubDate>
      <link>https://forem.com/marzelin/publishing-a-port-on-an-existing-docker-container-298d</link>
      <guid>https://forem.com/marzelin/publishing-a-port-on-an-existing-docker-container-298d</guid>
      <description>&lt;p&gt;When you need to publish some port on a container that was already run, you can do this by changing internal docker files.&lt;/p&gt;

&lt;p&gt;First, you need to find the container &lt;code&gt;id&lt;/code&gt; with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker inspect &amp;lt;container_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stop the container if it's running and go to the container directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd /var/lib/docker/containers/&amp;lt;container_id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;small&gt;For &lt;code&gt;wsl&lt;/code&gt; users, the container directory can be found here:&lt;br&gt;&lt;code&gt;\\wsl$\docker-desktop-data\version-pack-data\community\docker\containers&lt;/code&gt;&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;add &lt;code&gt;"ExposedPorts"&lt;/code&gt; to &lt;code&gt;config.v2.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Config": {
    ....
    "ExposedPorts": {
        "&amp;lt;port_number&amp;gt;/tcp": {},
    },
    ....
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;code&gt;"PortBindings"&lt;/code&gt; to &lt;code&gt;hostconfig.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"PortBindings": {
     "&amp;lt;port_number&amp;gt;/tcp": [
         {
             "HostIp": "",
             "HostPort": "&amp;lt;port_number&amp;gt;"
         }
     ]
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For best results restart docker service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;systemctl restart docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can start the container and it should have ports published.&lt;/p&gt;

</description>
      <category>docker</category>
    </item>
    <item>
      <title>How to reduce size of docker data volume in Docker Desktop for Windows v2</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Sun, 08 Nov 2020 09:53:42 +0000</pubDate>
      <link>https://forem.com/marzelin/how-to-reduce-size-of-docker-data-volume-in-docker-desktop-for-windows-v2-5d38</link>
      <guid>https://forem.com/marzelin/how-to-reduce-size-of-docker-data-volume-in-docker-desktop-for-windows-v2-5d38</guid>
      <description>&lt;p&gt;Docker Desktop for Windows v2, which uses WSL2, stores all image and container files in a separate virtual volume (vhdx). This virtual hard disk file can automatically grow when it needs more space (to a certain limit). Unfortunately, if you reclaim some space, i.e. by removing unused images, vhdx doesn't shrink automatically. Luckily, you can reduce its size manually by calling this command in PowerShell (as Administrator):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Optimize-VHD&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Path&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c:\path\to\data.vhdx&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-Mode&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Full&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The docker-desktop-data vhdx is normally in this location:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;LOCALAPPDATA&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nx"&gt;\Docker\wsl\data\ext4.vhdx&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>docker</category>
      <category>wsl2</category>
    </item>
    <item>
      <title>What is a container</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Sun, 17 Nov 2019 19:59:54 +0000</pubDate>
      <link>https://forem.com/marzelin/what-is-a-container-2flh</link>
      <guid>https://forem.com/marzelin/what-is-a-container-2flh</guid>
      <description>&lt;p&gt;You might know that a container is a standardized unit of software, that is, an application with everything that it requires to work: runtime, libraries, config, etc.&lt;/p&gt;

&lt;p&gt;If this is not enough for you and you'd like to actually see with your own eyes what a container is, read on.&lt;/p&gt;

&lt;p&gt;A container can be in two states: running or stopped.&lt;/p&gt;

&lt;p&gt;In stopped state a container looks absolutely simple: it just one file called &lt;code&gt;config.json&lt;/code&gt; that contains configuration and a directory called &lt;code&gt;rootfs&lt;/code&gt; that is used as a container root (&lt;code&gt;/&lt;/code&gt;) directory.&lt;/p&gt;

&lt;p&gt;Can it be that simple?&lt;br&gt;
You might know that a container image is basically just an archived directory that becomes the root directory when a container runs. We can unpack it with &lt;code&gt;tar&lt;/code&gt; to a directory called &lt;code&gt;rootfs&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;rootfs &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; docker &lt;span class="nb"&gt;export&lt;/span&gt; &lt;span class="si"&gt;$(&lt;/span&gt;docker create alpine&lt;span class="si"&gt;)&lt;/span&gt; | &lt;span class="nb"&gt;tar&lt;/span&gt; &lt;span class="nt"&gt;-xf&lt;/span&gt; - &lt;span class="nt"&gt;-C&lt;/span&gt; rootfs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can run a container with &lt;code&gt;runc&lt;/code&gt; command after creating a default config file (&lt;code&gt;config.json&lt;/code&gt;) with &lt;code&gt;runc spec&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;runc spec &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;sudo &lt;/span&gt;runc run mycontainerid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To see that you're indeed in a container, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; /etc/&lt;span class="k"&gt;*&lt;/span&gt;release&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and you should see &lt;code&gt;NAME="Alpine Linux"&lt;/code&gt;&lt;br&gt;
Moreover, if you start another terminal and create a file in &lt;code&gt;rootfs&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch&lt;/span&gt; /path/to/rootfs/hello-from-host
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you'll see this file in the container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; /
&lt;span class="c"&gt;# bin  hello-from-host  media&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what is &lt;code&gt;runc&lt;/code&gt; and what does it do?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;runc&lt;/code&gt; runs a container (that's basically what docker uses under the hood) in an isolated environment. Kind of.&lt;/p&gt;

&lt;p&gt;What you might think is that &lt;code&gt;runc&lt;/code&gt; puts an app process in a software equivalent of a solid metal box.&lt;/p&gt;

&lt;p&gt;But it's far from truth. A container process is not enclosed in some kind of jail from which it cannot escape. The process has no idea that it's restricted. It has no idea how &lt;em&gt;real world&lt;/em&gt; looks like. &lt;code&gt;runc&lt;/code&gt; virtually lies to him that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;rootfs&lt;/code&gt; directory is the root of the filesystem&lt;/li&gt;
&lt;li&gt;there are no other processes in the system and it's init process with &lt;code&gt;PID 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;about available network, computation and memory resources&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's more like putting a vr headset into a process without telling it that it's in virtual reality. It can't escape &lt;code&gt;rootfs&lt;/code&gt; directory because in his world view &lt;code&gt;rootfs&lt;/code&gt; is &lt;code&gt;/&lt;/code&gt; and there's no way to go higher than &lt;code&gt;/&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is possible mostly thanks to  two kernel features: &lt;code&gt;namespaces&lt;/code&gt; and &lt;code&gt;cgroups&lt;/code&gt;. &lt;code&gt;namespaces&lt;/code&gt; allow to virtualize system resources and &lt;code&gt;cgroups&lt;/code&gt; provides a way to limit resources like CPU and memory.&lt;/p&gt;

&lt;p&gt;As you can see containers do not contain. The isolation is only one-way. A contained process can't see the world outside of a container but the host has all the information about the process, can access its filesystem and interact with it as if it was a normal process (it practically is a normal process). Isolation is based on providing fake information about the state of the system to a contained process.&lt;/p&gt;

&lt;p&gt;That makes containers really lightweight but a little less secure than VMs. Moving a normal app to a container is a breeze because from the app's perspective there's no difference between normal environment and virtual environment presented to it when it's run in a container so there's no need for special modifications.&lt;/p&gt;

&lt;p&gt;Summing up, a container is an app code and all its dependencies kept in a single directory tree plus a config file. When run, a container is restricted to that directory by making it think that the directory is a filesystem root and providing it with manufactured information about system resources. To make the container easy to transport it's packed into an image format that's basically an archive of a container directory.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>kubernetes</category>
      <category>linux</category>
    </item>
    <item>
      <title>Declarative vs Imperative - a Thoughtful Comparison</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Wed, 26 Jun 2019 14:14:55 +0000</pubDate>
      <link>https://forem.com/marzelin/declarative-vs-imperative-a-thoughtful-comparison-4hm6</link>
      <guid>https://forem.com/marzelin/declarative-vs-imperative-a-thoughtful-comparison-4hm6</guid>
      <description>&lt;p&gt;You might see a lot of people talking that declarative is better because it is simpler and give an example like this:&lt;/p&gt;

&lt;p&gt;declarative code (what would you like):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a tea
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;vs imperative code (what would you like me to do):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- boil water
- pour water into a cup
- add a tea bag to the water
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One short line of declarative code vs three long lines of imperative code. Evidently declarative must be better. But is this a fair example? What if we modify it a bit:&lt;/p&gt;

&lt;p&gt;declarative code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a tea

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;vs imperative code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;make a tea
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not much a difference, is it?&lt;br&gt;
So, is declarative superiority just a hoax?&lt;br&gt;
Spoiler alert: no, but if you want to know the real reasons, read on.&lt;/p&gt;

&lt;p&gt;The basics&lt;/p&gt;

&lt;p&gt;With declarative code, we specify what we'd like - the end result or state.&lt;br&gt;
With imperative code, we specify a set of steps - an instruction that we'd like to be performed.&lt;/p&gt;

&lt;p&gt;What are pros and cons of both? From a developer perspective, when looking at the code we are mostly concerned about what the code does (the end result), not so much how the code makes it happen (the steps). That's why we tend to put imperative code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- boil water
- pour water into a cup
- add a tea bag to the water
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into a module with a meaningful name like &lt;code&gt;makeTea&lt;/code&gt;. This saves a bit of time and effort needed to figure out what that three lines are all about. With declarative code we declare the final goal so we have this feature inherently. That's a slight advantage of declarative code - we immediately know the end goal, with imperative code the end goal is implicit (it is there but we need to analyze the lines to get it or otherwise comment the code, or put it into a function or a module with a meaningful name). &lt;/p&gt;

&lt;p&gt;The real advantage of declarative code is that it doesn't specify the starting point. With imperative code an initial state is implicit and mostly we start from scratch (which is the easiest way) like here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- boil water
- pour water into a cup
- add a tea bag to the water
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It implies that we are starting with cold water. What if we already had a hot water? It would get thrown away or would be boiled twice. A wasted effort.&lt;br&gt;
With declarative code the system can be smart and reuse the resources it already has. The way of figuring out how to go from current state to the desired state is not our business anymore because we can't really specify the initial state in a declarative language - the state change is managed internally by the engine.&lt;/p&gt;

&lt;p&gt;That's the real deal here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With declarative, you specify the end goal and let the system work it out by itself.&lt;/li&gt;
&lt;li&gt;With imperative you implicitly specify the starting state (or states if you want to be more resource efficient) by explicitly specifying a set of steps that lead to some final state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What are some real-world implications of this?&lt;/p&gt;

&lt;p&gt;Let's say we want some function that takes a string of HTML and renders it.&lt;br&gt;
The easiest implementation would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const render = (html) =&amp;gt; document.body.innerHTML = html;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem with this is that DOM is stateful, for example, the user might type something to an &lt;code&gt;&amp;lt;insert&amp;gt;&lt;/code&gt; element and we want to preserve that. The above code doesn't care about what is currently in the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt;, it throws away everything and puts new content there.&lt;/p&gt;

&lt;p&gt;You might've heard that React is declarative. In fact React &lt;code&gt;render()&lt;/code&gt; function is declarative. It takes a description of how view should look like and figures out a way to make it happen. It is declarative because it takes a description as an input. It might work similarly to the code above - discard current content and replace it with the new one - but is much more intelligent and reuses the elements when it can.&lt;/p&gt;

&lt;p&gt;That's the power of declarative. You only need to specify the desired state and let the system figure out how to get there. This is really important in places where resource reuse is important, where you can't just throw away everything and start from scratch like in DOM and network clusters because it takes this really hard problem of figuring out how to go from any state of the system to the desired state off your back.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>increasing test timeout in jest while debugging</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Sun, 12 Aug 2018 14:12:11 +0000</pubDate>
      <link>https://forem.com/marzelin/increasing-test-timeout-in-jest-while-debugging-2p9l</link>
      <guid>https://forem.com/marzelin/increasing-test-timeout-in-jest-while-debugging-2p9l</guid>
      <description>

&lt;p&gt;When debugging code, the script execution takes substantially more time which sometimes leads to very strange errors when an asyc test is failed due to exceeding the time specified in &lt;code&gt;jest.setTimeout&lt;/code&gt; which causes the next test to execute while the previous test is still running in the background.&lt;/p&gt;

&lt;p&gt;Fortunately, we can change the default time using &lt;code&gt;--setupTestFrameworkScriptFile&lt;/code&gt; option that should point to a file in which we change the timeout:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;jasmine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DEFAULT_TIMEOUT_INTERVAL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can put this file in the home directory and run jest with changed timeouts from anywhere with a little bit of command line magic:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;node &lt;span class="nt"&gt;--inspect-brk&lt;/span&gt; node_modules/.bin/jest &lt;span class="nt"&gt;--runInBand&lt;/span&gt; &lt;span class="nt"&gt;--setupTestFrameworkScriptFile&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;HOME&lt;/span&gt;&lt;span class="k"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;/test-setup.js"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Another cool thing is that if we also have a setup file in our project, both files are used (but values from file given through command line option take precedence).&lt;/p&gt;


</description>
      <category>jesttesting</category>
    </item>
    <item>
      <title>vscode debug config for wsl and jest</title>
      <dc:creator>Marc Ziel</dc:creator>
      <pubDate>Mon, 06 Aug 2018 21:48:35 +0000</pubDate>
      <link>https://forem.com/marzelin/vscode-debug-config-for-wsl-and-jest-o04</link>
      <guid>https://forem.com/marzelin/vscode-debug-config-for-wsl-and-jest-o04</guid>
      <description>&lt;p&gt;When using the config from jest &lt;a href="https://github.com/Microsoft/vscode-recipes/tree/master/debugging-jest-tests"&gt;documentation&lt;/a&gt; the execution doesn't stop at specified breakpoints but the following config works with wsl:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"0.2.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"configurations"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"request"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"launch"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Jest All"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"program"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"${workspaceFolder}/node_modules/jest/bin/jest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"args"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"--runInBand"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="nl"&gt;"useWSL"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>wsl</category>
      <category>vscode</category>
      <category>jest</category>
      <category>debugging</category>
    </item>
  </channel>
</rss>
