<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ferarias on Medium]]></title>
        <description><![CDATA[Stories by ferarias on Medium]]></description>
        <link>https://medium.com/@ferarias?source=rss-7e615a7b3b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*f6p1JE561P3AG8MF.jpeg</url>
            <title>Stories by ferarias on Medium</title>
            <link>https://medium.com/@ferarias?source=rss-7e615a7b3b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 09:59:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ferarias/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Secure Synology NAS with a custom domain, dynamic DNS and a free certificate]]></title>
            <link>https://medium.com/@ferarias/secure-synology-nas-with-a-custom-domain-dynamic-dns-and-a-free-certificate-e61fd8e607a8?source=rss-7e615a7b3b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e61fd8e607a8</guid>
            <category><![CDATA[synology]]></category>
            <category><![CDATA[dsm]]></category>
            <category><![CDATA[dyn-dns]]></category>
            <category><![CDATA[ssl-certificate]]></category>
            <category><![CDATA[freedns]]></category>
            <dc:creator><![CDATA[ferarias]]></dc:creator>
            <pubDate>Thu, 13 Jan 2022 19:37:25 GMT</pubDate>
            <atom:updated>2022-01-13T19:37:25.847Z</atom:updated>
            <content:encoded><![CDATA[<h3>Secure Synology NAS with a custom domain, dynamic DNS and a free SSL certificate</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gjS1sutaDolNtNc_CeSpGw.jpeg" /><figcaption>Photo by form <a href="https://pxhere.com/en/photo/1029761"><strong>PxHere</strong></a></figcaption></figure><h3>Overview</h3><p>In this article I will try to explain how I set up my Synology NAS so that I can connect to it from the Internet via <em>https, </em>protected by a valid certificate and using my own domain name.</p><p>I must warn you this is a very personal setup that uses specific tools. Maybe it fits your needs and maybe it doesn’t. Anyway it is a guide you can follow, and eventually replace some parts with alternatives best suited to your needs.</p><p>I did this post using Windows 11, but it doesn’t matter which OS you use. Just replace PuTTY with whatever tool that allows you to connect to your NAS via SSH and you’ll be good to go.</p><p>A note of warning: I am not a network expert, so maybe there’s some security risks here that I didn’t take into account. If so, please let me know in the comments and proceed with caution.</p><h4>Pre-requisites</h4><p>This is what you will need:</p><ul><li><strong>A </strong><a href="https://freedns.afraid.org/"><strong>FreeDNS</strong></a><strong> account</strong> to have a dynamic DNS. Create an account if you don’t have one.</li><li><strong>A domain</strong> of your own with full control over it. If you don’t have one, you can also <a href="https://freedns.afraid.org/domain/register/">register one</a> from FreeDNS.</li><li><strong>A </strong><a href="https://mariushosting.com/can-i-run-docker-on-my-synology-nas/"><strong>Docker-capable</strong></a><strong> Synology NAS</strong></li><li><a href="https://www.putty.org/"><strong>PuTTY</strong></a><strong> </strong>or similar to connect to your NAS via SSH</li></ul><h4>Dynamic DNS with FreeDNS</h4><p>Your ISP can change your public IP without warning, and usually does it each time your router is rebooted, so you need a way to update the DNS name servers whenever that happens. <strong>FreeDNS </strong>is a service that allow you to freely update DNS records so that your domain name points to your public IP all the time.</p><h4>Certificate with ZeroSSL</h4><p>Browsers these days are more an more concerned about security, and while you can connect to your NAS <em>ignoring</em> the warnings, I prefer not having that feeling of <em>something’s wrong</em> by issuing my own certificate and publishing it in my Synology. <strong>ZeroSSL </strong>is a service that allows you to issue a certificate for your domain at no cost.</p><p>Enough talking. Let’s get our hands dirty.</p><h3>Dynamic DNS</h3><h4>Forward your custom domain to FreeDNS servers</h4><p>For historic reasons I bought my domain using GoDaddy and I still use that. Depending on your domain provider, your experience will be different, but what you want to accomplish in this step is to administer your domain so that it uses custom nameservers. For example, here are the <a href="https://es.godaddy.com/help/cambiar-servidores-de-nombres-para-mis-dominios-664">instructions to do it in GoDaddy domains</a>. To find instructions for your provider, google something like custom nameservers for domain godaddy, changing godaddywith the name of your provider.</p><p>Once you know how to do it for your domain provider, change the nameservers of your domain to the following:</p><pre>NS1.AFRAID.ORG<br>NS2.AFRAID.ORG<br>NS3.AFRAID.ORG<br>NS4.AFRAID.ORG</pre><p>Now we need to <a href="https://freedns.afraid.org/domain/add.php">add a domain into FreeDNS</a>. Type your domain (eg: whatever.com) into <em>Domain </em>field and set the <em>Shared State</em> to Private.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*MlHay1fqKvxiHtmu6QW4KQ.png" /></figure><p>Now browse to the <a href="https://freedns.afraid.org/subdomain/">Subdomains</a> tab and add your subdomains of type A.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*KEr47MBPVF1mWFb6M0kDtA.png" /></figure><p>The most important ones right now are your generic domain (eg, whatever.com) and the <em>www </em>host ( www.whatever.com). The IP is not that important at this point, since we will be updating it dynamically.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/682/1*e1rIe3vnQz6Qyy-NzCOXBw.png" /></figure><h4>Updating the IP automatically</h4><p>Synology included an out-of-the-box solution for the IPs to be updated dynamically using different dynamic DNS providers. Luckily for us, FreeDNS is one of them.</p><p>Open a browser and point to you Synology NAS DSM, for example <a href="https://192.169.1.7:5001.">https://192.169.1.7:5001.</a></p><ul><li>Open Control Panel.</li><li>Browse to Connectivity. External Access.</li><li>Click the DDNS tab and then click the Add button.</li><li>In <em>Service Provider</em>, choose Freedns.org</li><li>In <em>Hostname</em>, type your domain name, eg whatever.com</li><li>Fill in Username and Password with your FreeDNS credentials</li><li>Click the <em>Test Connection</em> button.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*ySPcmrsJvlU_62LAzQmgQg.png" /></figure><p>If everything is OK, you should see your public IP automatically set in the <em>External Address</em> field.</p><ul><li>Click OK to save your changes.</li></ul><p>Now click the button Update Now and take a look at the Status column. After a short while, it should read Normal and the External Address should be updated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*K5ayF0LkQM5SuUSBHL2MiA.png" /></figure><p>Return to the FreeDNS portal and check that your IPs have been updated accordingly.</p><p>You have finished the Dynamic DNS part and now your custom domain should be pointing to your home network.</p><h3>Prepare DSM to use Docker</h3><h4>Install Docker</h4><p>If you have already installed and enabled the Docker package in your Synology, skip this section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*XpJNB8AxlYXGEISn626pAA.png" /></figure><ul><li>Open Package Center</li><li>Search for Docker and then click on the package</li><li>Press <em>Install</em>, then <em>Run</em>.</li></ul><h4>Create a group for Docker</h4><p>We are going to create a docker group to allow using docker with no restrictions.</p><ul><li>Open Control Panel</li><li>Browse to User &amp; Group</li><li>If you don’t have one yet, create now a personal restricted user for yourself in DSM. There’s too many advantages to mention here, but the main reason is security. I’ll assume you have one and I will use fernando for all the examples here.</li><li>Click on Group and create a new group called docker.</li><li>Click on Members</li><li>Add your user (fernandoin this example) to the group.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*G_o2EeQhk1Rbts-mvTrgbg.png" /></figure><p>We will use this later to grant access to the Docker sock to this group.</p><h4>Enable SSH in DSM</h4><ul><li>Open Control Panel</li><li>Browse to Terminal &amp; SNMP</li><li>Click <em>Enable SSH Service</em> and choose a port. I prefer to enable a different port than the default 22, usually something easy to remember like 2211 or 2200.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*XPnHOTHEe4ngZawGPLZe9Q.png" /></figure><h4>Connect to your NAS via SSH</h4><p>You are prepared now to connect to your NAS via SSH to type in some command lines.</p><ul><li>Open PuTTY</li><li>Create an SSH connection to your NAS IP.</li><li>Remember to establish the port you set previously (if you did not set it, the default is 22). Use your own user credentials to connect.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*lNNRo3_BXEyCApOUc4ewYA.png" /></figure><ul><li>Click Open</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*Qhbq4MGa2sWZMOiF_1LhZw.png" /></figure><h4>Grant docker group permissions to run Docker</h4><p><em>Note: this step is not strictly necessary, but I find it most convenient.</em></p><p>Once connected, issue this command to be able to use docker without having to sudoeverytime:</p><pre>sudo chown root:docker /var/run/docker.sock</pre><p>Try to invoke the following afterwards:</p><pre>docker run -it --rm hello-world</pre><p>If you are not required for credentials, everything is fine. Else, check that your user belongs to the group docker, and check that the /var/run/docker.sockis chown’ed to this group.</p><h3>The SSL certificate</h3><h4>Obtain the acme.sh script</h4><p><a href="https://github.com/acmesh-official/acme.sh">The acme.sh script</a> is the easiest way to manage certificates from different Certification Authorities (CA). It’s a UNIX shell script that manages most of the common options you need to issue and publish certificates in many different ways.</p><p>Now comes a fun part. Since git is not installed in your NAS, we will use docker.</p><p><em>Note: if </em><em>gitis installed in your NAS, good for you, you can just clone the repo as you usually do</em></p><pre>docker run -ti --rm -v ${HOME}:/root -v $(pwd):/git alpine/git clone <a href="https://github.com/acmesh-official/acme.sh.git">https://github.com/acmesh-official/acme.sh.git</a> acmesh</pre><pre>cd acmesh<br>chmod +x ./acme.sh</pre><p>We will be using the acme.sh script with an specific <a href="https://github.com/acmesh-official/acme.sh/wiki/dnsapi#15-use-freedns">FreeDNS script</a> that is called from it. For the FreeDNS script to work, you need to export these two variables so it can connect to your FreeDNS account:</p><pre>export FREEDNS_User=&quot;&lt;your FreeDNS username&gt;&quot;<br>export FREEDNS_Password=&quot;&lt;your FreeDNS password&quot;</pre><p>Now, let’s test if everything is working (mind the--testflag):</p><pre>./acme.sh --issue --dns dns_freedns -d whatever.com -d www.whatever.com --test</pre><h4>Register in ZeroSSL</h4><p>First, you need to <em>register</em> in ZeroSSL, the provider that will actually issue the certificate. It’s as simple as the following command, replacing the email with one of yours, of course:</p><pre>./acme.sh --register-account -m me@whatever.com --server zerossl</pre><h4>Issue the certificate</h4><p>To actually issue the certificate, do:</p><pre>/acme.sh --issue --dns dns_freedns -d whatever.com -d www.whatever.com --force</pre><p>The --force flag is required only if you did the --test before.</p><p>After a while, you should get the certificate</p><pre>Your cert is in: /acme.sh/whatever.com/whatever.com.cer<br>Your cert key is in: /acme.sh/whatever.com/whatever.com.key<br>The intermediate CA cert is in: /acme.sh/whatever.com/ca.cer<br>And the full chain certs is there: /acme.sh/whatever.com/fullchain.cer</pre><h4>Publish the certificate</h4><p>Ok, time to deploy the certificate in your NAS. acme.sh has something called <em>deploy hooks</em>, that take care of the required steps to publish certificates in different environments. We will be using the <a href="https://github.com/acmesh-official/acme.sh/wiki/deployhooks#20-deploy-the-cert-into-synology-dsm">synology_dsm hook</a>.</p><pre>export SYNO_Username=&quot;&lt;DSM Admin Username&gt;&quot;<br>export SYNO_Password=&quot;&lt;DSM Admin Password&gt;&quot;<br>export SYNO_Certificate=&quot;acme.sh whatever.com ZS certificate&quot; <br>export SYNO_Create=1 # defaults to off, this setting is not saved.  By setting to 1 we create the certificate if it&#39;s not in DSM<br>acme.sh --deploy -d whatever.com --deploy-hook synology_dsm</pre><p>SYNO_Create must be set to 1 for the certificate to be created if not exists. Replace whatever.com with your actual domain.</p><p>Read the documentation at the <a href="https://github.com/acmesh-official/acme.sh/wiki/deployhooks#20-deploy-the-cert-into-synology-dsm">synology_dsm hook</a> page. You can use the default admin user credentials, or — better still — , create an specific administrative user with no rights to access any of your precious data folders.</p><h4>Program the renewal of the certificate</h4><p>If you followed all the previous steps, the renewal of the certificate is as simple as calling the script with the cron flag.</p><pre>./acme.sh --cron</pre><p>If you do it now, you will see that it has no effect since the next renewal is not until a couple of months.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/885/1*OoXXrtEHXftN2gKrRc38KA.png" /></figure><p>What we are going to do is to schedule a task in your Synology to invoke the command on a monthly basis.</p><ul><li>Open Control Panel</li><li>Browse to Task Scheduler</li><li>Create → Scheduled Task → User-defined script</li><li>In the General tab, in <em>Task</em> give it a name such as “Renew certificate” and in <em>User </em>use your user (or any user that <strong>belongs to the docker group</strong>)</li><li>In Schedule tab, mark <em>Run on the following date</em> and set to the 1st of next month, then choose <em>Repeat monthly</em>.</li><li>In Task Settings, paste the following User-defined script:</li></ul><pre>/var/services/homes/fernando/acmesh/acme.sh --cron</pre><p>Of course, the path is where you downloaded your acme.sh script. You can determine it in your ssh session with the pwd command.</p><h4>Change the certificates in DSM</h4><ul><li>Open Control Panel</li><li>Browse to Connectivity. Security.</li><li>Click on the Certificate tab</li><li>The new certificate should appear there.</li><li>Select the new certificate, choose Action →Edit and mark <em>Set as default certificate</em>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*yRwMD7gTGy3OXn6_p96D3g.png" /></figure><ul><li>Click on Settings</li><li>In the Configure tab, set the new certificate for all services (except QuickConnect, that uses its own)</li><li>Click OK</li></ul><h4>Alternate usage of the acme.sh script using Docker</h4><p>During my research I found out there’s a somewhat easier way to invoke the acme.sh script without having to even download it, <a href="https://github.com/acmesh-official/acme.sh/wiki/Run-acme.sh-in-docker">using Docker</a> again.</p><p>You just need to create a folder to store the configurations and use docker to mount it as a volume in the container. You don’t have to export variables but pass them to the container.</p><p>Here are the previous commands using this technique.</p><pre>mkdir acme</pre><pre># register in ZeroSSL CA<br>sudo docker run --rm -it -v &quot;$(pwd)/acme&quot;:/acme.sh --net=host --env FREEDNS_User=&#39;&lt;your FreeDNS username&gt;&#39; --env FREEDNS_Password=&#39;&lt;your FreeDNS password&gt;&#39; neilpang/acme.sh  --register-account -m me@whatever.com --server zerossl</pre><pre># Test<br>sudo docker run --rm -it -v &quot;$(pwd)/acme&quot;:/acme.sh --net=host --env FREEDNS_User=&#39;&lt;your FreeDNS username&gt;&#39; --env FREEDNS_Password=&#39;&lt;your FreeDNS password&gt;&#39; neilpang/acme.sh  --issue --dns dns_freedns -d whatever.com -d www.whatever.com --test</pre><pre># Issue the certificate<br>sudo docker run --rm -it -v &quot;$(pwd)/acme&quot;:/acme.sh --net=host --env FREEDNS_User=&#39;&lt;your FreeDNS username&gt;&#39; --env FREEDNS_Password=&#39;&lt;your FreeDNS password&gt;&#39; neilpang/acme.sh  --issue --dns dns_freedns -d whatever.com -d www.whatever.com --force</pre><pre># Deploy the certificate<br>sudo docker run --rm -it -v &quot;$(pwd)/acme&quot;:/acme.sh --net=host --env SYNO_Certificate=&#39;whatever_com_zs&#39; --env SYNO_Username=&#39;&lt;DSM Admin Username&gt;&#39; --env SYNO_Password=&#39;&lt;DSM Admin Password&gt;&#39; --env SYNO_Create=1 neilpang/acme.sh  --deploy -d whatever.com --deploy-hook synology_dsm</pre><pre># Renew the certificate<br>sudo docker run --rm -it -v &quot;$(pwd)/acme&quot;:/acme.sh --net=host neilpang/acme.sh --cron</pre><p>In the /acme folder you will find stored all the configuration files.</p><p>In the Task Scheduler user-defined script you need to provide the full path to the configurations folder (replace with your own):</p><pre>docker run --rm -v &quot;/var/services/homes/fernando/acme&quot;:/acme.sh --net=host neilpang/acme.sh --cron</pre><p>I hope you find this post useful. I did it because I tend to forget things. It’s to remember what did I do when building all this in case I have to do it again.</p><p>If you find errors please let me know in the comments. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e61fd8e607a8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Docker in Windows 11 using WSL2]]></title>
            <link>https://medium.com/@ferarias/docker-in-windows-11-using-wsl2-8e30faddc32c?source=rss-7e615a7b3b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e30faddc32c</guid>
            <category><![CDATA[wsl-2]]></category>
            <category><![CDATA[wsl]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[ubuntu]]></category>
            <dc:creator><![CDATA[ferarias]]></dc:creator>
            <pubDate>Wed, 05 Jan 2022 17:28:57 GMT</pubDate>
            <atom:updated>2024-05-02T14:39:15.417Z</atom:updated>
            <content:encoded><![CDATA[<p>I wanted to share my experience setting up an environment for development with containerized applications on Windows. The license for Docker Desktop is not expensive, but it’s not free anymore, so I wanted to check if there’s an easy and <em>cheap</em> alternative to Docker Desktop for Windows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aXpXt_CA10SpyqbwGvID2g.jpeg" /></figure><p>My research is based upon these two resources:</p><ul><li><a href="https://dev.to/bowmanjd/install-docker-on-windows-wsl-without-docker-desktop-34m9"><strong>Install Docker on Windows (WSL) without Docker Desktop</strong></a>, by <a href="https://dev.to/bowmanjd">Jonathan Bowman</a>.</li><li><a href="https://itnext.io/replacing-docker-desktop-with-multipass-to-avoid-docker-desktop-fees-8cbe57b9037f"><strong>Replacing Docker Desktop with Multipass, to avoid Docker Desktop fees</strong></a>, by <a href="https://medium.com/@7genblogger">David Herron</a>.</li></ul><p>but my goal here is to go straight to set up a quick environment for working without too much hassle. No ceremony, not too much prose, just the real cake. In fact, if you already have a WSL Debian-based distribution you can skip to the “Windows side…” section and start reading from there.</p><p>I assume you have some basic knowledge of Docker and Linux. <em>PowerShell </em>and <em>bash </em>basic skills will be useful, too.</p><h3>The game plan</h3><p>The main goal here is to set up a containers runtime using WSL2, Ubuntu Linux and free packages. The secondary goal is being able to use it from both Linux and Windows.</p><p>To do so, we need to setup WSL2, Ubuntu, dockerd and containerd, then build docker-cli for Windows and finally wire them so they can talk to each other.</p><h3>Pre-requisites</h3><p>First, you need to set up your Windows so that you can use virtualized containers, as well as Windows Subsystem for Linux. Open a terminal and type:</p><pre>Enable-WindowsOptionalFeature -Online -FeatureName &#39;Containers&#39; -All</pre><pre>Enable-WindowsOptionalFeature -Online -FeatureName &#39;Microsoft-Hyper-V&#39; -All</pre><pre>Enable-WindowsOptionalFeature -Online -FeatureName &#39;VirtualMachinePlatform&#39; -All</pre><pre>Enable-WindowsOptionalFeature -Online -FeatureName &#39;Microsoft-Windows-Subsystem-Linux&#39; -All</pre><h4>Winget</h4><p>To install software, I will be using <a href="https://docs.microsoft.com/en-us/windows/package-manager/winget/#production-recommended">winget</a>, the package manager for Windows, since it’s the easiest way to install stuff on Windows these days (given there’s a package installer for it).</p><h4>Windows Terminal</h4><p>We will use console a lot, so I recommend using a decent terminal. Windows offers the excellent Windows Terminal, which can be obtained from <a href="https://aka.ms/terminal">Microsoft Store</a>.</p><p><em>Note: Windows Terminal requires Windows 10 1903 (build 18362) or later</em></p><h4>Powershell</h4><p>Now we have the basics up and running, you should also install Microsoft PowerShell (not Windows PowerShell) to make your (console) life easier. Open a terminal session and type:</p><pre>winget install --id &#39;Microsoft.Powershell&#39; --scope machine</pre><h4>Ubuntu linux</h4><p>You could also use any Debian-based installation, but I feel more comfortable in Ubuntu, so I’ll stick to this distribution.</p><pre>PS&gt; winget install Ubuntu</pre><p>Once it is installed, just type:</p><pre>PS&gt; ubuntu</pre><p>You will see a message like this:</p><pre>Installing, this may take a few minutes</pre><p>Once it finishes, you must create a default account. You can use whatever username you like; I usually use my first name, fernando, and an easy to remember password.</p><pre>Please create a default UNIX user account. The username does not need to match your Windows username.</pre><p>Ok, once you are done, you’ll see something like the following:</p><pre>Installation successful!<br>Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.10.60.1-microsoft-standard-WSL2 x86_64)</pre><pre>[...]</pre><p>Now you have a Ubuntu Linux under WSL2 ready to play with. For now, just close the terminal with</p><pre>exit</pre><h3>Meanwhile, on the Windows side…</h3><p>Open a Windows Powershell (using Windows Terminal), and type the following to check everything is fine from the WSL perspective:</p><pre>wsl --list</pre><p>You should see the following:</p><pre>Windows Subsystem for Linux Distributions:<br>Ubuntu (Default)</pre><p>We will need also a folder to hold the docker-cli executable for Windows. If you already have one and it’s in your PATH, that’s fine. You’ll need it later. In case you don’t have one, let’s create one, for example: c:\tools, and add it to the PATH (for example, using <a href="https://www.architectryan.com/2018/03/17/add-to-the-path-on-windows-10/">this</a>).</p><h3>Install docker in WSL</h3><p>Open a terminal window in WSL Ubuntu (again, you can use Windows Terminal)</p><p>Start by upgrading the packages:</p><pre>sudo apt update &amp;&amp; sudo apt upgrade</pre><p>[…takes a while…]</p><p>Install pre-requisites</p><pre>sudo apt install --no-install-recommends apt-transport-https ca-certificates curl gnupg2</pre><p>Let’s make sure the certificate for Docker repo is trusted:</p><pre>source /etc/os-release<br>curl -fsSL <a href="https://download.docker.com/linux/$%7BID%7D/gpg">https://download.docker.com/linux/${ID}/gpg</a> | sudo apt-key add -</pre><p>Add the Docker repository to the list of available repos for packages:</p><pre>echo &quot;deb [arch=amd64] <a href="https://download.docker.com/linux/$%7BID%7D">https://download.docker.com/linux/${ID}</a> ${VERSION_CODENAME} stable&quot; | sudo tee /etc/apt/sources.list.d/docker.list</pre><p>Now update the packages and install Docker and Docker CLI</p><pre>sudo apt update<br>sudo apt install docker-ce docker-ce-cli containerd.io</pre><p>Add your user to the Docker group, named docker:</p><pre>sudo usermod -aG docker $USER</pre><p>Let’s create a configuration for the dockerd daemon:</p><pre>sudo mkdir /etc/docker/<br>sudo vi /etc/docker/daemon.json</pre><p>And add the following contents:</p><pre>{<br>  &quot;hosts&quot;: [ &quot;tcp://0.0.0.0:&quot; ],<br>  &quot;tls&quot;: false<br>}</pre><p>You can launch dockerd directly:</p><pre>sudo dockerd</pre><p>and stop with CTRL-C</p><p>or launch dockerd in the background:</p><pre>sudo dockerd &amp;</pre><p>And stop it with:</p><pre>sudo pkill dockerd</pre><p>If you launch it in the foreground, just open a new Ubuntu terminal session to keep on firing commands.</p><p>Check the containerdprocess is listening:</p><pre>ss -peanut</pre><p>And expect something like:</p><pre>Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port Process<br>tcp LISTEN 0 4096 *:2375 *:* ino:41752 sk:2001 v6only:0 &lt;-&gt;</pre><p>Try everything’s fine in the Docker world with this command:</p><pre>docker -H 127.0.0.1 run --rm hello-world</pre><p>If you are successful, export DOCKER_HOST so you don’t have to type it every time:</p><pre>export DOCKER_HOST=&quot;tcp://127.0.0.1:2375&quot;</pre><h4>Extra. Avoid being asked for superuser password each time you run dockerd</h4><p>As <a href="https://dev.to/bowmanjd">Jonathan Bowman</a> suggests, you can avoid being asked for sudo password each time you launch dockerd.</p><pre>sudo visudo</pre><p>and add at the end of the file:</p><pre>%docker ALL=(ALL) NOPASSWD: /usr/bin/dockerd</pre><h3>Build docker-cli for Windows</h3><p>To build docker-cli for Windows we are going to use Docker, as suggested in <a href="https://github.com/docker/cli">their repo</a>.</p><p>First, we need to clone the repo from Github:</p><pre>git clone <a href="https://github.com/docker/cli.git">https://github.com/docker/cli.git</a><br>cd cli</pre><p>Now, build Docker CLI for the Windows platform:</p><pre>docker buildx bake<br>docker buildx bake --set binary.platform=windows/amd64</pre><p>If everything goes well, you will find the docker-cli executable in the build folder. You just have to move it from there to your Windows local tools location (we set that up previously, remember?). In our case, C:\tools\</p><pre>cp build/docker-windows-amd64.exe /mnt/c/tools</pre><h3>Back in the Windows zone</h3><p>Let’s get back to Windows PowerShell. Move to your tools folder and rename the docker-windows-amd64.exe to simply docker.exe, so that we just have to type docker (remember your tools folder must be in your PATH)</p><pre>C:\<br>cd c:\tools<br>ren docker-windows-amd64.exe docker.exe</pre><p>We need to know the IP of the WSL host. To do so, we can use this command (taken from <a href="https://superuser.com/questions/1586386/how-to-find-wsl2-machines-ip-address-from-windows/1603307">here</a>):</p><pre>wsl -- ip -o -4 -json addr list eth0 `<br>| ConvertFrom-Json `<br>| %{ $_.addr_info.local } `<br>| ?{ $_ }</pre><p>With the obtained IP, try the following (replacing with your IP, of course):</p><pre>docker -H &lt;YOUR_WSL_IP&gt; ps</pre><p>Did it work? Congratulations. You can try something more challenging:</p><pre>docker run --rm hello-world</pre><p>Recent versions of Docker CLI allow the creation of <em>contexts</em>, which allow you to create different contexts for different Docker environments. We can benefit from this to create a specific context for WSL:</p><pre>docker context create wsl --docker &quot;host=tcp://&lt;YOUR_WSL_IP&gt;:2375&quot;</pre><pre>docker context use wsl</pre><pre>docker run --rm hello-world</pre><p>The only problem I have now is that WSL changes instance IP on each reboot, so you have to change the context each time you restart. You can change context like this:</p><pre>docker context update wsl --docker &quot;host=tcp://&lt;NEW_WSL_IP&gt;:2375&quot;</pre><p>A simple PowerShell script to update the context automatically so that it is set to the correct host:</p><pre>$wslip = wsl -- ip -o -4 -json addr list eth0 | ConvertFrom-Json | ForEach-Object { $_.addr_info.local } | Where-Object { $_ }</pre><pre>$ctx = docker context ls --format json | ConvertFrom-Json | Where-Object { $_.Name -eq &quot;wsl&quot; }</pre><pre>if($null -eq $ctx) {</pre><pre>Write-Host &quot;Creating Docker context &#39;wsl&#39; to host=tcp://$($wslip):2375&quot;</pre><pre>docker context create wsl --docker &quot;host=tcp://$($wslip):2375&quot; | Out-Null</pre><pre>} else {</pre><pre>docker context update wsl --docker &quot;host=tcp://$($wslip):2375&quot; | Out-Null</pre><pre>}</pre><pre>docker context use wsl | Out-Null</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e30faddc32c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Code Coverage in VSTS with xUnit, Coverlet and ReportGenerator]]></title>
            <link>https://medium.com/bluekiri/code-coverage-in-vsts-with-xunit-coverlet-and-reportgenerator-be2a64cd9c2f?source=rss-7e615a7b3b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/be2a64cd9c2f</guid>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-testing]]></category>
            <category><![CDATA[code]]></category>
            <dc:creator><![CDATA[ferarias]]></dc:creator>
            <pubDate>Thu, 21 Jun 2018 06:53:17 GMT</pubDate>
            <atom:updated>2019-12-07T17:45:32.216Z</atom:updated>
            <cc:license>http://creativecommons.org/licenses/by/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Today I was reading <a href="https://www.hanselman.com/blog/AutomaticUnitTestingInNETCorePlusCodeCoverageInVisualStudioCode.aspx">a post by Scott Hanselman</a> about Code Coverage in .Net Core projects. Suddenly I started to go down a rabbit hole of posts about tools I never heard of, but that sounded quite interesting, such as <a href="https://github.com/tonerdo/coverlet">Coverlet </a>and <a href="https://github.com/danielpalme/ReportGenerator">ReportGenerator</a>. Then I thought that they could possibly fit into our CI flows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*076o2xIO5K6Fmo-5" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When I started .NET development I didn&#39;t do Unit Testing at all, I admit it. However, thanks to good folks such as <a href="http://twitter.com/lfraile">Luis Fraile</a> and <a href="https://medium.com/u/3d0d49fa3aea">Unai Zorrilla Castro</a> I soon realized that they were so important that I could not live without them. So, I adopted <a href="https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest">Microsoft’s MsTest</a> and started to write lots of [TestClass]es and [TestMethod]s. Then at some point I heard about <a href="http://xunit.github.io/">xUnit</a> and nothing was the same again. I forgot about [TestClass] attributes and started to write [Fact]s and [Theory]es.</p><p>I have been using them for a long time, but the problem I found when changing from MsTest to <a href="https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-dotnet-test">xUnit</a> was that the information shown in Visual Studio Team Services’ build results was very poor, to be polite. And there was no code coverage data at all. That was bad indeed, but I am lazy and I have postponed the task of improving this until today.</p><h3>Coverlet</h3><p>The first thing I did was to partially implement the changes suggested by Scott Hanselman to include code coverage in my project. Coverlet is a package that can be included into your projects and generates code coverage info during build. The project states that it is literally a “cross-platform code coverage library for .NET Core, with support for line, branch and method coverage”.</p><p>To include Coverlet build targets in your project you must add the following line to your test project’s csproj file:</p><pre>&lt;PackageReference <em>Include</em>=&quot;coverlet.msbuild&quot; <em>Version</em>=&quot;2.0.1&quot; /&gt;</pre><p>You can find out how the magic is done by reading <a href="https://github.com/tonerdo/coverlet">its Github home page</a>, but basically you can generate a code coverage file in several common formats by invoking dotnet test with the CollectCoverage parameter.</p><pre>dotnet test /p:CollectCoverage=true</pre><p>This, by default, generates a code coverage file in JSON format. If you want to experiment with Visual Studio Code extension Coverage Gutters, as suggested by Hanselman, you can use a task such as this in your .vscode/tasks.json file:</p><pre>{<br> &quot;label&quot;: &quot;test with coverage&quot;,<br> &quot;command&quot;: &quot;dotnet&quot;,<br> &quot;type&quot;: &quot;process&quot;,<br> &quot;args&quot;: [<br>  &quot;test&quot;,<br>  &quot;/p:CollectCoverage=true&quot;,<br>  &quot;/p:CoverletOutputFormat=lcov&quot;,<br>  &quot;/p:CoverletOutput=./lcov&quot;,<br>  &quot;${workspaceFolder}/test/WebUserManager.Test/WebUserManagerTests.csproj&quot;<br> ],<br> &quot;problemMatcher&quot;: &quot;$msCompile&quot;,<br> &quot;group&quot;: {<br>  &quot;kind&quot;: &quot;test&quot;,<br>  &quot;isDefault&quot;: true<br> }<br>}</pre><p>The <em>lcov.info</em> file can be parsed by <a href="https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters">Coverage Gutters</a> extension and show which lines are covered by your tests. This can be very convenient in local development, but for CI in VSTS we’ll need to use a different file format that can be understood by our report generation tool. The format is the one used by <a href="http://cobertura.github.io/cobertura/">Cobertura</a>, a code coverage utility for Java, and it can be generated by invoking <em>dotnet test </em>with these parameters:</p><pre>dotnet test <br>  /p:CollectCoverage=true <br>  /p:CoverletOutputFormat=cobertura</pre><p>This generates a file called <em>coverage.cobertura.xml</em>. This alone can be used to publish code coverage data to VSTS build results. However, we’ll go a step further and generate a full report that can be viewed and browsed with source code linking.</p><h3>ReportGenerator</h3><p>The tool we will need is called <a href="https://github.com/danielpalme/ReportGenerator">ReportGenerator</a>, and its main usage is to generate beautiful reports in several formats from code coverage files in other formats. In our case we are going to generate an HTML file from our <em>coverage.cobertura.xml</em> file.</p><p>ReportGenerator is an executable that can be downloaded and run easily. But since we don’t want to depend on tools installed into our agents, we will make the build download and install the tool upon request. To do so, we will add it as a <a href="https://docs.microsoft.com/en-us/dotnet/core/tools/extensibility#per-project-based-extensibility">dotnet CLI tool</a> into our project. Just add the following lines into your test project’s .csproj file:</p><pre>&lt;ItemGroup&gt;<br>  &lt;DotNetCliToolReference <em>Include</em>=&quot;dotnet-reportgenerator-cli&quot; <em>Version</em>=&quot;4.0.0-alpha12&quot; /&gt;<br>&lt;/ItemGroup&gt;</pre><p>NOTE: this is an alpha version of the tool, so be prepared to face potential issues and changes without notification. If you need something more stable, you can download and install the ReportGenerator.exe in a folder of your build agents and set your PATH accordingly.</p><p>Now your tester <em>.csproj</em> file should look something like this:</p><pre>&lt;Project <em>Sdk</em>=&quot;Microsoft.NET.Sdk.Web&quot;&gt;</pre><pre>  &lt;PropertyGroup&gt;<br>    &lt;TargetFramework&gt;netcoreapp2.1&lt;/TargetFramework&gt;<br>    &lt;IsPackable&gt;False&lt;/IsPackable&gt;<br>  &lt;/PropertyGroup&gt;</pre><pre>  &lt;ItemGroup&gt;<br>    &lt;ProjectReference <em>Include</em>=&quot;..\..\src\App\Acme.App.csproj&quot; /&gt;<br>    &lt;ProjectReference <em>Include</em>=&quot;..\..\src\Domain\Acme.Domain.csproj&quot; /&gt;<br>    &lt;ProjectReference <em>Include</em>=&quot;..\..\src\Infrastructure\Acme.Infrastructure.csproj&quot; /&gt;<br>  &lt;/ItemGroup&gt;</pre><pre>  &lt;ItemGroup&gt;<br>    &lt;PackageReference <em>Include</em>=&quot;Microsoft.NET.Test.Sdk&quot; <em>Version</em>=&quot;15.7.2&quot; /&gt;<br>    &lt;PackageReference <em>Include</em>=&quot;xunit&quot; <em>Version</em>=&quot;2.3.1&quot; /&gt;<br>    &lt;PackageReference <em>Include</em>=&quot;xunit.runner.visualstudio&quot; <em>Version</em>=&quot;2.3.1&quot; /&gt;<br>    &lt;PackageReference <em>Include</em>=&quot;Moq&quot; <em>Version</em>=&quot;4.8.2&quot; /&gt;<br>    &lt;PackageReference <em>Include</em>=&quot;coverlet.msbuild&quot; <em>Version</em>=&quot;2.0.1&quot; /&gt;<br>  &lt;/ItemGroup&gt;</pre><pre>  &lt;ItemGroup&gt;<br>    &lt;DotNetCliToolReference <em>Include</em>=&quot;dotnet-reportgenerator-cli&quot; <em>Version</em>=&quot;4.0.0-alpha12&quot; /&gt;<br>  &lt;/ItemGroup&gt;</pre><pre>  &lt;ItemGroup <em>Condition</em>=&quot; &#39;$(TargetFramework)&#39; == &#39;netcoreapp2.1&#39; &quot;&gt;<br>    &lt;PackageReference <em>Include</em>=&quot;Microsoft.CodeCoverage&quot; <em>Version</em>=&quot;1.0.3&quot; /&gt;<br>  &lt;/ItemGroup&gt;</pre><pre>&lt;/Project&gt;</pre><h3>Set up build definition</h3><p>Now that we have the project correctly configured, it’s time to set up our build definition. Keep in mind that the following steps won’t work if you haven’t modified your project with the NuGet packages defined before.</p><p>Below you can see an example build definition. We are interested in the three highlighted tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*u6lJwbQy52GmsMsv9zVktw.png" /></figure><p>The first task runs the tests using the xUnit test runner and generates the code coverage file. The second task takes the generated file and builds a detailed report linked to source code. The third task publishes the two previous results so that they can be shown in the build results panel. Let’s see each one in detail.</p><h4>Test and collect coverage</h4><p>This is a <em>.NET Core</em> task with these options:</p><ul><li>Command: test</li><li>Path to project(s): select just one test project. It should be possible to choose more than one with globs but I haven’t tried.</li><li>Arguments:</li></ul><pre>/p:CollectCoverage=true <br>/p:CoverletOutputFormat=<strong>cobertura </strong><br>/p:CoverletOutput=$(Build.SourcesDirectory)\TestResults\Coverage\</pre><p>Notice how we are setting the output format to <em>cobertura </em>and the folder where the output will be left to a subfolder named <em>TestResults\Coverage</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uN-nX8-sZyftvxDEWrpiHw.png" /><figcaption>dotnet test and collect coverage</figcaption></figure><h4>Report generator</h4><p>The second task is also a <em>.NET Core</em> task with these options:</p><ul><li>Command: <em>custom</em></li><li>Path to project(s): same as the first task</li><li>Custom command: <em>reportgenerator</em></li><li>Working Directory: set to the folder of the test project</li><li>Arguments:</li></ul><pre>&quot;-reports:$(Build.SourcesDirectory)\TestResults\Coverage\coverage.cobertura.xml&quot; &quot;-targetdir:$(Build.SourcesDirectory)\TestResults\Coverage\Reports&quot; -tag:$(Build.BuildNumber) -reportTypes:htmlInline</pre><p>Here we are invoking the .Net Core CLI tool <em>ReportGenerator.exe</em>, as explained before, setting the input file to <em>coverage.cobertura.xml</em>, the output format to <em>HTML inline</em> (no external js nor image files) and the output folder to <em>TestResults\Coverage\Reports</em>.</p><p>NOTE: mind the quotes around some of the parameters, especially the ones containing paths.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_7nTZI8Ul3UIN_SMwyDM0A.png" /><figcaption>dotnet reportgenerator</figcaption></figure><p>At this point we have generated the coverage file and the report at these locations:</p><ul><li><em>$(Build.SourcesDirectory)\TestResults\Coverage\coverage.cobertura.xml</em></li><li><em>$(Build.SourcesDirectory)\TestResults\Coverage\Report\*.htm</em></li></ul><h4>Publish coverage data</h4><p>The last task just publishes the generated data. Add a “<em>Publish Code Coverage Results</em>” task with these values:</p><ul><li>Code coverage tool: <em>Cobertura</em></li><li>Summary file: <em>$(Build.SourcesDirectory)\TestResults\Coverage\**\coverage.cobertura.xml</em></li><li><em>Report directory: $(Build.SourcesDirectory)\TestResults\Coverage\Reports</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nBk892x7TvCOgyBVJNa1vQ.png" /><figcaption>publish code coverage results</figcaption></figure><p>And that’s it. The final results will be like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*iSmtZUeheeB0A_Y5tPcXrQ.png" /><figcaption>Build results</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EYahESmY_kGkt_Hub56FLQ.png" /><figcaption>Code coverage results</figcaption></figure><p>More information:</p><ul><li><a href="https://www.hanselman.com/blog/AutomaticUnitTestingInNETCorePlusCodeCoverageInVisualStudioCode.aspx">https://www.hanselman.com/blog/AutomaticUnitTestingInNETCorePlusCodeCoverageInVisualStudioCode.aspx</a></li><li><a href="https://github.com/tonerdo/coverlet">https://github.com/tonerdo/coverlet</a></li><li><a href="https://danielpalme.github.io/ReportGenerator/">https://danielpalme.github.io/ReportGenerator/</a></li><li><a href="https://github.com/danielpalme/ReportGenerator/wiki/Integration">https://github.com/danielpalme/ReportGenerator/wiki/Integration</a></li><li><a href="https://www.nuget.org/packages/dotnet-reportgenerator-cli/">https://www.nuget.org/packages/dotnet-reportgenerator-cli/</a></li><li><a href="https://dotnetthoughts.net/code-coverage-in-netcore-with-coverlet/">https://dotnetthoughts.net/code-coverage-in-netcore-with-coverlet/</a></li><li><a href="https://docs.microsoft.com/en-us/dotnet/core/tools/extensibility#per-project-based-extensibility">https://docs.microsoft.com/en-us/dotnet/core/tools/extensibility#per-project-based-extensibility</a></li><li><a href="https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters">https://marketplace.visualstudio.com/items?itemName=ryanluker.vscode-coverage-gutters</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be2a64cd9c2f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/bluekiri/code-coverage-in-vsts-with-xunit-coverlet-and-reportgenerator-be2a64cd9c2f">Code Coverage in VSTS with xUnit, Coverlet and ReportGenerator</a> was originally published in <a href="https://medium.com/bluekiri">bluekiri</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>