<?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: Gareth Bradley</title>
    <description>The latest articles on DEV Community by Gareth Bradley (@garfbradaz).</description>
    <link>https://dev.to/garfbradaz</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%2F115969%2Fb9e041b0-ad19-452d-8aea-231df1bb7dae.jpeg</url>
      <title>DEV Community: Gareth Bradley</title>
      <link>https://dev.to/garfbradaz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/garfbradaz"/>
    <language>en</language>
    <item>
      <title>Episode 5 - JSON API using ASP.NET Core, Docker &amp;  MongoDB - Modelling, Controller and Unit Tests Part I - BookStore</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Sun, 17 Feb 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/episode-5---json-api-using-aspnet-core-docker---mongodb---modelling-controller-and-unit-tests-part-i---bookstore-5alk</link>
      <guid>https://dev.to/garfbradaz/episode-5---json-api-using-aspnet-core-docker---mongodb---modelling-controller-and-unit-tests-part-i---bookstore-5alk</guid>
      <description>&lt;h2&gt;
  
  
  Previously on Dcoding
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://garfbradaz.github.io/blog/2018/12/31/Episode-4-JSON-API-ASP.NET-Core-Docker-Compose.html"&gt;Episode 4&lt;/a&gt; I set up our &lt;code&gt;docker-compose&lt;/code&gt; files to allow us to knit together our application and the services it will be use. Today’s episode is focusing on &lt;strong&gt;Modelling, Controller and Unit Tests for the BookStore Object&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Here is a reminder on the sample &lt;em&gt;User Stories / Epics&lt;/em&gt; for phase 1.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As a &lt;em&gt;book store_I can _add&lt;/em&gt; our &lt;em&gt;store&lt;/em&gt; to the database_So_ we can be accessible&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;book store_I can _add&lt;/em&gt; our &lt;em&gt;inventory&lt;/em&gt; to our database_So_ we can expose our inventory&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;book store_We can _update&lt;/em&gt; a books stock level_For_ an accurate stock level&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;API consumer_I can look up a _stores address&lt;/em&gt;&lt;em&gt;So&lt;/em&gt; we know where to buy a &lt;em&gt;book&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;API consumer_I can look up a _book&lt;/em&gt;&lt;em&gt;So&lt;/em&gt; we can get a &lt;em&gt;list&lt;/em&gt; of &lt;em&gt;stores&lt;/em&gt; who sell a_book_&lt;/p&gt;

&lt;p&gt;As &lt;em&gt;book store IT Security_We can add _API Keys&lt;/em&gt; to the API_For_ API Consumers to use when querying the API&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Dependency on MongoDB
&lt;/h2&gt;

&lt;p&gt;Our application will ultimately be using &lt;em&gt;MongoDB&lt;/em&gt; as its back end data store. We need to take a dependency on the MongoDB driver. This will allow us to communicate with and use MongoDB.&lt;/p&gt;

&lt;p&gt;Using your shell of choice, change directory to &lt;code&gt;../src/api&lt;/code&gt; directory. To recap, our directory structure is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
    ├── src
    | ├── api
    | |
    | ├── BookStoreApp.WebApi.csproj
    | ├── Dockerfile
    ├── tests
    | ├── integration
    | ├── unit
    | ├── BookStore.Tests.csproj
    ├── docker
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Something has changed, can you guess what? Yep that’s right, I have renamed the &lt;code&gt;.csproj&lt;/code&gt; to &lt;code&gt;BookStoreApp.WebApi.csproj&lt;/code&gt; instead of &lt;code&gt;BookStore.WebApi.csproj&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now run the following command, which will add the latest Nuget package to your api project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add package MongoDB.Driver
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We will be returning to MongoDB once we have the unit test infrastructure set up in the next episode.&lt;/p&gt;

&lt;h2&gt;
  
  
  BookStore
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Modelling
&lt;/h3&gt;

&lt;p&gt;The first of our models (the &lt;em&gt;M&lt;/em&gt; in &lt;strong&gt;M&lt;/strong&gt; VC), will represent our &lt;strong&gt;BookStore&lt;/strong&gt;. A simple &lt;a href="https://en.wikipedia.org/wiki/Plain_old_CLR_object"&gt;Plain old C# (POCO)&lt;/a&gt; to represent this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// &amp;lt;summary&amp;gt;
    /// BookStore POCO.
    /// &amp;lt;/summary&amp;gt;
    public class BookStore
    {
        [BsonId]
        public ObjectId Id {get; set;}
        public string Name {get;set;}
        public string AddressLine1 {get;set;}
        public string AddressLine2 {get;set;}
        public string AddressLine3 {get;set;}
        public string City {get;set;}
        public string PostCode {get;set;}
        public string TelephoneNumber {get;set;}

        /// &amp;lt;summary&amp;gt;
        /// Default constructor
        /// &amp;lt;/summary&amp;gt;
        public BookStore()
        {
            this.Id = ObjectId.GenerateNewId();
        }
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I added a &lt;code&gt;Model&lt;/code&gt; folder to my &lt;code&gt;../api&lt;/code&gt; folder where this was added. MongoDB uses a serialization format standard called &lt;a href="https://docs.mongodb.com/manual/reference/glossary/#term-bson"&gt;BSON&lt;/a&gt;. BSON has the idea of &lt;em&gt;types&lt;/em&gt; and one of those types is &lt;a href="https://docs.mongodb.com/manual/reference/bson-types/#objectid"&gt;&lt;code&gt;ObjectId&lt;/code&gt;&lt;/a&gt; which represents a unique id for the record. You decorate the &lt;code&gt;Id&lt;/code&gt; property with &lt;code&gt;[BsonId]&lt;/code&gt; attribute, to inform the MongoDB driver what field is your &lt;code&gt;BsonId&lt;/code&gt; field.&lt;/p&gt;

&lt;p&gt;I have also created a default constructor that sets that Id up. Now we have created the &lt;code&gt;Models&lt;/code&gt; directory and structure is now the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
    ├── src
    | ├── api
    | ├── Models
    | ├── BookStore.cs
    | ├── BookStoreApp.WebApi.csproj
    | ├── Dockerfile
    ├── tests
    | ├── integration
    | ├── unit
    | ├── BookStore.Tests.csproj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Controller
&lt;/h3&gt;

&lt;p&gt;Now we need to create a &lt;strong&gt;BookStore&lt;/strong&gt; controller. A controller is the &lt;em&gt;C&lt;/em&gt; in MV &lt;strong&gt;C&lt;/strong&gt; pattern. The controller will handle the HTTP requests that come in using &lt;em&gt;Actions&lt;/em&gt;. The HTTP requests are mapped to Actions via the routing pipeline of MVC. For WebAPIs, they map to specific HTTP methods like &lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, &lt;code&gt;PUT&lt;/code&gt; etc.&lt;/p&gt;

&lt;p&gt;ASP.NET expects you to follow conventions when creating your own Controllers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Controller class name always ends with &lt;code&gt;Controller&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Controller class should reside in a folder called &lt;code&gt;Controllers&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The Controller class should inherit from &lt;code&gt;Microsoft.AspNetCore.Mvc.ControllerBase&lt;/code&gt; (WebAPI) / &lt;code&gt;Microsoft.AspNetCore.Mvc.Controller&lt;/code&gt; (ASP.NET MVC Apps with Views).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you do a &lt;code&gt;dotnet new webapi&lt;/code&gt; the templates include a standard &lt;code&gt;ValuesController.cs&lt;/code&gt; that lives in a &lt;code&gt;Controllers&lt;/code&gt; folder. Create a new &lt;code&gt;BookStoreController.cs&lt;/code&gt; file in there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
    ├── src
    | ├── api
    | ├── Controllers
    | ├── BookStoreController.cs
    | ├── Models
    | ├── BookStore.cs
    | ├── BookStoreApp.WebApi.csproj
    | ├── Dockerfile
    ├── tests
    | ├── integration
    | ├── unit
    | ├── BookStore.Tests.csproj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To this file I have added the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Route("api/[controller]")]
    [ApiController]
    public class BookStoreController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public async Task&amp;lt;ActionResult&amp;gt; Get()
        {
            var bookStore = new List&amp;lt;BookStore&amp;gt;{
                new BookStore {
                    Name = "Waterstones",
                    AddressLine1 = "The Dolphin &amp;amp; Anchor",
                    AddressLine2 = "West Street",
                    City = "Chichester",
                    PostCode = "PO19 1QD",
                    TelephoneNumber = "01234 773030"
                }
            };
            return await Task.Run(() =&amp;gt; new JsonResult(bookStore));
        }
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Its a simple &lt;code&gt;GET&lt;/code&gt; method that just returns a hard coded &lt;code&gt;BookStore&lt;/code&gt; object. For today’s episode we are just concentrating on getting our unit test infrastructure up and running so we can ignore &lt;code&gt;PUT&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; until my next episode.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unit Tests
&lt;/h3&gt;

&lt;p&gt;Before we move forward we need to use the &lt;code&gt;dotnet&lt;/code&gt; tooling to add a project reference to our tests project. Change directory to &lt;code&gt;tests\unit&lt;/code&gt; and do the following to add a reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet add reference ..\..\src\api\BookStoreApp.WebApi.csproj
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I have also added two basic test methods to cover our new &lt;code&gt;GET&lt;/code&gt; method in oir controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ControllerTests
    {
        [Fact]
        public async Task BookStoreController_Get_Should_Return_ActionResult()
        {
            //Arrange
            var controller = new BookStoreController();

            //Act
            var result = await controller.Get();

            //Assert
            Assert.IsType&amp;lt;JsonResult&amp;gt;(result);
        }

        [Fact]
        public async Task BookStoreController_Get_Should_Return_Correct_BookStore_Data()
        {
            //Arrange
            var controller = new BookStoreController();

            //Act
            var result = await controller.Get();
            var json = result.ToJson&amp;lt;BookStore&amp;gt;();

            //Assert
            Assert.True(json[0].Name == "Waterstones",$"Assert failed, received {json[0].Name} ");
            Assert.True(json[0].PostCode == "PO19 1QD",$"Assert failed, received {json[0].PostCode} ");
            Assert.True(json[0].TelephoneNumber == "01234773030",$"Assert failed, received {json[0].TelephoneNumber} ");
        }
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before we start integrating with Docker, we can test using (you guessed it), the &lt;code&gt;dotnet&lt;/code&gt; tooling. Make sure you are in the &lt;code&gt;tests\unit&lt;/code&gt; directory and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet restore
dotnet test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;test&lt;/code&gt; will set off a &lt;code&gt;dotnet build&lt;/code&gt; first then ran our XUnit tests. One test should fail and this will be outputted similar to this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Total tests: 2. Passed: 1. Failed: 1. Skipped: 0.&lt;/p&gt;

&lt;p&gt;Test Run Failed.&lt;/p&gt;

&lt;p&gt;Test execution time: 2.6340 Seconds&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The error was intentional, there should of been a space in between &lt;code&gt;"01234773030"&lt;/code&gt;, I fixed this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Assert.True(json[0].TelephoneNumber == "01234 773030",$"Assert failed, received {json[0].TelephoneNumber} ");
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Re-run the tests and everything should now be green:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Total tests: 2. Passed: 2. Failed: 0. Skipped: 0.&lt;/p&gt;

&lt;p&gt;Test Run Successful.&lt;/p&gt;

&lt;p&gt;Test execution time: 3.1173 Seconds&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we have a project with some basic logic within a controller, and a very basic model. We have also started creating some tests to cover this code. Now we need to &lt;em&gt;build and run&lt;/em&gt; the application. We could at this stage use the normal route for that, but as these episodes include &lt;strong&gt;Docker&lt;/strong&gt; , lets integrate what we know into a docker pipeline.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker
&lt;/h2&gt;

&lt;p&gt;Firstly in the root of the project create a new Docker file. Your project structure should look like this now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
    ├── src
    ├── tests
    ├── Dockerfile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we will create a multi-stage &lt;code&gt;Dockerfile&lt;/code&gt; that will restore, build and run our tests. Here is the first stage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app

COPY src/api/BookStoreApp.WebApi.csproj ./src/api/
RUN dotnet restore ./src/api/BookStoreApp.WebApi.csproj
COPY tests/unit/BookStore.Tests.csproj ./tests/unit/
RUN dotnet restore ./tests/unit/BookStore.Tests.csproj

COPY . .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You should be familiar with this now from previous episodes. This time take note that we are copying the unit test project files in to the build context. Also note we need to keep the same directory structure as before, because we added a &lt;code&gt;dotnet add reference&lt;/code&gt; previously into the test project. If the directories didn’t match we would get build errors.&lt;/p&gt;

&lt;p&gt;Run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t test-ep5 .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now do a &lt;code&gt;docker image&lt;/code&gt; and you will see a massive image there:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_8fGWiYx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://garfbradaz.github.io/assets/img/posts/test-ep5-size.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_8fGWiYx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://garfbradaz.github.io/assets/img/posts/test-ep5-size.png" alt="test-5 image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Docker has a way of managing this, meet &lt;code&gt;.dockerignore&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  .dockerignore
&lt;/h3&gt;

&lt;p&gt;This file behaves similarly to a &lt;code&gt;.gitignore&lt;/code&gt;. It tells docker which files to not copying in during a build. So how do you know which files to ignore, well I learnt a good trick from &lt;a href="https://twitter.com/g0t4"&gt;Wes Higbee&lt;/a&gt; by passing in a &lt;code&gt;ls alR&lt;/code&gt; to list out your directories. Run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --rm test-ep5 ls -alR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will list out your containers file system, and you can easily see what is copied in. So things like &lt;code&gt;.vscode&lt;/code&gt;, &lt;code&gt;.git&lt;/code&gt; folders and &lt;code&gt;bin&lt;/code&gt; directories. None of this stuff is needed during the build stage of this multi stage &lt;code&gt;Dockerfile&lt;/code&gt;, so lets exclude it, using similar glob patterns you can use in &lt;code&gt;.gitignore&lt;/code&gt; files. Add a &lt;code&gt;.dockerignore&lt;/code&gt; file to your root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
    ├── src
    ├── tests
    ├── Dockerfile
    ├── .dockerignore
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then add the following, excluding files and artefacts that are not needed for a restore and build. We exclude things like our &lt;code&gt;docker&lt;/code&gt; folder and &lt;code&gt;.ps1&lt;/code&gt; scripts we have been using. Plus the &lt;code&gt;README.md&lt;/code&gt; and dockerfiles.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;**/.vscode/
**/.git/
docker/
**/bin/
**/obj/
**/.dockerignore/
**/Dockerfile*
**/docker-compose*.yml/
run.ps1
clean.ps1
README.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Re-run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t test-ep5 .
 docker run --rm test-ep5 ls -alR
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You will see a cleaner build plus about 100MB less space than previously:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X7f5gqXM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://garfbradaz.github.io/assets/img/posts/test-ep5-size-2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X7f5gqXM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://garfbradaz.github.io/assets/img/posts/test-ep5-size-2.png" alt="test-5 image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Unit Tests in Docker
&lt;/h2&gt;

&lt;p&gt;So now we have pruned our Image, we can add our tests to our &lt;code&gt;Dockerfile&lt;/code&gt;. Add the following into your &lt;code&gt;Dockerfile&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUN dotnet test tests/unit/BookStore.Tests.csproj
RUN dotnet publish src/api/BookStoreApp.WebApi.csproj -o /publish
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We run what we ran on the commandline earlier (see no magic). This will run our unit tests, then if they pass, we will publish a new release ready for our 2nd stage to use, by running &lt;code&gt;dotnet publish&lt;/code&gt; and outputting to a new &lt;code&gt;/publish&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Run again using the following and see your test success!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t test-ep5 .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If these unit tests fail, we will see the same as we did previously, so you can see how this all ties in. And just ecause we run our tests in docker, &lt;code&gt;dotnet&lt;/code&gt; is still behaving the same. No docker magic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running your application
&lt;/h3&gt;

&lt;p&gt;In previous episodes we had a multi stage &lt;code&gt;Dockerfile&lt;/code&gt; with a 2nd stage that runs the application itself. No different here, our 2nd stage allows us to run our application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; Once we have added this, we cannot use our &lt;strong&gt;“ls aLR”&lt;/strong&gt; trick Wes taught us, as our &lt;code&gt;ENTRYPOINT&lt;/code&gt; will be set with &lt;code&gt;dotnet&lt;/code&gt;. Also the 1st build stage is thrown away once used so we cannot access the directory fully anyway.&lt;/p&gt;

&lt;p&gt;Add the following to your &lt;code&gt;Dockerfile&lt;/code&gt; which is our runtime stage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime-env
WORKDIR /publish
COPY --from=build-env /publish .
ENTRYPOINT ["dotnet","BookStoreApp.WebApi.dll"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Rebuild the Image first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t test-ep5 .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then we can run the Image as a container process, overriding some of the ASP.NET Core environment variables using &lt;code&gt;-e&lt;/code&gt;. We would override these usually when using &lt;code&gt;docker-compose&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -e "ASPNETCORE_ENVIRONMENT=Development" -e "ASPNETCORE_URLS=http://+:5003" -p 5003:5003 --rm -it test-ep5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can then use your application of choice (I’m using Postman, more to come on that) to hit &lt;a href="http://localhost:5003/api/bookstore"&gt;http://localhost:5003/api/bookstore&lt;/a&gt;. You should receive the following JSON payload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    {
        "id": "5c6947093497a200016c0dee",
        "name": "Waterstones",
        "addressLine1": "The Dolphin &amp;amp; Anchor",
        "addressLine2": "West Street",
        "addressLine3": null,
        "city": "Chichester",
        "postCode": "PO19 1QD",
        "telephoneNumber": "01234 773030"
    }
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And thats it, we are slowly starting to move our development and unit test pipeline into docker itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next time
&lt;/h2&gt;

&lt;p&gt;That’s it for today. Remember all code is on &lt;a href="https://github.com/garfbradaz/webapi-episodes/tree/master/episode-5"&gt;Github&lt;/a&gt; if you want it.&lt;/p&gt;

&lt;p&gt;On our next episode we will start integrating MongoDB into the application.&lt;/p&gt;

</description>
      <category>unittests</category>
      <category>docker</category>
      <category>dotnetcore</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Raspberrypi Cluster Kubernetes</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Tue, 12 Feb 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/raspberrypi-cluster-kubernetes-n75</link>
      <guid>https://dev.to/garfbradaz/raspberrypi-cluster-kubernetes-n75</guid>
      <description>&lt;h2&gt;
  
  
  Creating Raspberry Pi Cluster to Learn Kubernetes
&lt;/h2&gt;

&lt;p&gt;In August 2018 I bought some Pi’s tp create a cluster, mainly after reading and watching &lt;a href="https://blog.alexellis.io/build-your-own-bare-metal-arm-cluster/" rel="noopener noreferrer"&gt;Alex Ellis&lt;/a&gt; and &lt;a href="https://www.hanselman.com/blog/HowToBuildAKubernetesClusterWithARMRaspberryPiThenRunNETCoreOnOpenFaas.aspx" rel="noopener noreferrer"&gt;Scott Hanselman’s&lt;/a&gt; blogs and videos on the subject, which inspired me to make the purchase. I was running Code Clubs at the time, so I thought this would make a good show and tell as well.&amp;lt;!--more--&amp;gt;&lt;/p&gt;

&lt;p&gt;Both Alex and Scott posts were both inspiring and I really wanted to give it ago. Anyway I got the equipment, but work and real-life got in the way and progress was slow. Then in December 2018 I landed a new job, starting in April this year, and they use Kubernetes extensively so I also had another good reason to get it up and running - to learn Kubernetes!&lt;/p&gt;

&lt;p&gt;So this month I finally got it working, including a proper master and node setup:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkuj97prieo9sfbmqw75y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkuj97prieo9sfbmqw75y.jpg" width="800" height="1066"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There were some pain points, like make sure you set up your static IP’s for your Pi’s (and set them up correctly on your router), and more importantly, set up a static IP &lt;em&gt;for&lt;/em&gt; the router itself. The yellow box is the picture is the &lt;a href="https://www.amazon.co.uk/gp/product/B073TSK26W/ref=oh_aui_search_asin_title?ie=UTF8&amp;amp;psc=1" rel="noopener noreferrer"&gt;rGL-MT300N-V2 router I went for&lt;/a&gt; in the end, and the only bit of Hanselmans shopping list from his post, I diverted from.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Sons love it
&lt;/h2&gt;

&lt;p&gt;My two sons love it, as we are building Raspberry Pi robots as well in parallel to this. They are interested in what this is doing and &lt;em&gt;why&lt;/em&gt;. Sparking curiosity in little minds is important, and me just building websites doesn’t get their juices flowing!&lt;/p&gt;

&lt;h2&gt;
  
  
  Should I do this?
&lt;/h2&gt;

&lt;p&gt;Yes. :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Whats next?
&lt;/h2&gt;

&lt;p&gt;Well, learning Kubernetes itself and running some .NET Core container sites. I would like to see how quick the .NET SDK is for building on the Pi’s now, as I would like to experiment with an local build server.&lt;/p&gt;

&lt;p&gt;Also, Alex is the creator of &lt;a href="https://www.openfaas.com/" rel="noopener noreferrer"&gt;&lt;em&gt;OpenFaaS&lt;/em&gt;&lt;/a&gt;, a cloud agnostic Serverless solution. Having worked with Azure Functions recently at my current job, I can definitely see why this is a good idea. A Serverless infrastructure &lt;em&gt;without&lt;/em&gt; being tied down to a specific cloud. I want to explore converting a small Web API app to OpenFaaS and experimenting there (I’m actually super excited).&lt;/p&gt;

&lt;p&gt;Lastly, when I restart my Code Clubs up, I would like to do a show and tell with something interesting that includes the cluster. I was thinking about running some OpenFaaS functions that “talk” to my Pi robot I’m building and possibly allow you to control it for example.&lt;/p&gt;

&lt;p&gt;Lots to do before April arrives!&lt;/p&gt;

</description>
      <category>blog</category>
      <category>raspberrypi</category>
      <category>kubernetes</category>
      <category>openfaas</category>
    </item>
    <item>
      <title>Episode 4 - JSON API using ASP.NET Core, Docker &amp; MongoDB - Docker Part II Docker Compose</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Mon, 31 Dec 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/episode-4---json-api-using-aspnet-core-docker-amp-mongodb---docker-part-ii-docker-compose-53j1</link>
      <guid>https://dev.to/garfbradaz/episode-4---json-api-using-aspnet-core-docker-amp-mongodb---docker-part-ii-docker-compose-53j1</guid>
      <description>&lt;h2&gt;
  
  
  Previously on Dcoding
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://dev.to/garfbradaz/episode-2---json-api-using-aspnet-core-docker--mongodb-docker-part-i-dockerfiles-2o8i"&gt;Episode 3&lt;/a&gt; I set up our Dockerfiles for creating our &lt;em&gt;Docker Images&lt;/em&gt; for our BookStore app. This will allow us to rapidly test our application as we move forward in later Episodes. Today’s episode is &lt;strong&gt;Docker Part 2: Docking Compose&lt;/strong&gt;. &amp;lt;!--more--&amp;gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Docker Compose?
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://docs.docker.com/compose/" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; a good explanation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Compose is a tool for defining and running multi-container Docker applications.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Docker compose allows you to define your &lt;em&gt;services&lt;/em&gt; you need to build and run a full scale application. Think about it, it is not just your application you need. Depending on the type of application you may want:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Database of some sort.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Load Balancer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Web Server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Other applications / APIs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You may even want to run your unit/integration testing during your Continuous Integration pipeline (CI). You could do that with Docker and Compose.&lt;/p&gt;

&lt;h2&gt;
  
  
  BookStore.WebApi Set up
&lt;/h2&gt;

&lt;p&gt;If we take a look at our application for the &lt;em&gt;BookStore.WebApi&lt;/em&gt;, we can see the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdocker-arch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdocker-arch.png" alt="architecture diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from my quickly drawn up diagram (used &lt;a href="https://www.microsoft.com/en-gb/p/microsoft-whiteboard/9mspc6mp8fm4?activetab=pivot:overviewtab" rel="noopener noreferrer"&gt;Microsoft Whiteboard&lt;/a&gt;), we have two &lt;em&gt;services&lt;/em&gt; running in containers, 1 for the application and the other for the database (&lt;em&gt;MongoDB&lt;/em&gt;), so we can compose these together using &lt;code&gt;docker-compose&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  docker-compose.dev.yml
&lt;/h2&gt;

&lt;p&gt;So in &lt;a href="https://dev.to/garfbradaz/episode-2-json-api-dotnet-core-docker-project-structure-3i0c"&gt;Episode 2&lt;/a&gt; we set up the Project Structure, so change directory to the &lt;code&gt;./docker&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
    ├── src
    | ├── api
    | |
    | ├── BookStore.WebApi.csproj
    | ├── Dockerfile
    ├── tests
    | ├── integration
    | ├── unit
    | ├── BookStore.Tests.csproj
    ├── docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within that directory, you should have two empty YAML files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;docker-compose.dev.yml&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;docker-compose.yml&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are concentrating on a &lt;em&gt;Development&lt;/em&gt; environment first, so add the following to &lt;code&gt;docker-compose.dev.yml&lt;/code&gt;. These files help us define the shape of our services and the dependancies they have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3"
services:
  webapi:
    image: garfbradaz/bookstoreapi:develop
    container_name: webapi_tutorial_debug
    build:
      args:
        buildconfig: Debug
      context: ../src/api
      dockerfile: Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:5003
    ports:
      - "5003:5003"
    depends_on:
      - mongodb
  mongodb:
    image: mongo:latest
    container_name: mongodb
    ports:
      - "27017:27017"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And also, just add the following line to &lt;code&gt;docker-compose.yml&lt;/code&gt; file so that when we run &lt;code&gt;docker-compose up&lt;/code&gt; it doesn’t fail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As in &lt;strong&gt;Episode 3&lt;/strong&gt; lets break this &lt;em&gt;YAML&lt;/em&gt; file down now and what we are declaring. These files are &lt;em&gt;YAML&lt;/em&gt; and following the normal &lt;em&gt;YAML&lt;/em&gt; syntax rules around indenting etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;version:&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is really important. The currently released version (as of December 2018) is &lt;strong&gt;3(.7)&lt;/strong&gt;. You only have to include the whole number within the &lt;code&gt;version&lt;/code&gt; number field within the YAML file. Each &lt;em&gt;major&lt;/em&gt; upgrade (from 1.x, to 2.x, to 3.x) brings about possible breaking changes, including syntax changes to the YAML structure itself.&lt;/p&gt;

&lt;p&gt;Also the version of Compose relates to the version of the released &lt;em&gt;Docker Engine&lt;/em&gt; so have a good read of the &lt;a href="https://docs.docker.com/compose/compose-file/compose-versioning/#compatibility-matrix" rel="noopener noreferrer"&gt;Compatibility Matrix&lt;/a&gt;, but usually you pick the latest version.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;services:&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.docker.com/get-started/part3/" rel="noopener noreferrer"&gt;Docker services&lt;/a&gt; is where you define each application. So we have two services defined:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;webapi:&lt;/code&gt; which is our ASP.NET Core &lt;em&gt;BookStore.WebApi&lt;/em&gt; application.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mongodb:&lt;/code&gt; which is the back-end data store, &lt;em&gt;MongoDB&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;image:&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Each service has an &lt;code&gt;image&lt;/code&gt; defined. Mongo’s &lt;a href="https://hub.docker.com/_/mongo" rel="noopener noreferrer"&gt;&lt;code&gt;mongo:latest&lt;/code&gt;&lt;/a&gt; will be pulled directly from &lt;em&gt;hub.docker.com&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Our own will be built locally for now (until we publish it later) and its simply called &lt;code&gt;garfbradaz/bookstoreapi:develop&lt;/code&gt;. Note the &lt;em&gt;tag&lt;/em&gt; of &lt;strong&gt;develop&lt;/strong&gt;. We now have denoted our debug image, and we can add things like symbols etc for debugging purposes.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;container_name:&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is just a nice friendly name for our container. You can see the name when you run &lt;code&gt;docker ps&lt;/code&gt; after you have run a container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run your applications
&lt;/h2&gt;

&lt;p&gt;Docker compose has a command &lt;code&gt;docker compose up&lt;/code&gt;, which allows you to (re)build, (re)create and attach containers for the service. You run the command using the following (make sure you are in the &lt;code&gt;./docker&lt;/code&gt; directory):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose -f docker-compose.dev.yml up -d --build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command overrides the file (&lt;code&gt;-f&lt;/code&gt;) to &lt;code&gt;docker-compose.dev.yml&lt;/code&gt; and runs &lt;code&gt;up&lt;/code&gt;. The containers will run in detached mode (&lt;code&gt;-d&lt;/code&gt;) and run in the background. We will also (re)build the images (&lt;code&gt;--build&lt;/code&gt;). Because we have a &lt;code&gt;build&lt;/code&gt; section in our &lt;code&gt;docker-compose.dev.yml&lt;/code&gt; file for our code those values will be used. We set the context (&lt;code&gt;../src/api&lt;/code&gt;) which is the relative directory to the source code we are building (Relative to the &lt;code&gt;./docker&lt;/code&gt; directory), plus tell &lt;code&gt;docker-compose&lt;/code&gt; the Dockerfile name.&lt;/p&gt;

&lt;p&gt;We also send in some &lt;code&gt;args&lt;/code&gt; &lt;em&gt;into&lt;/em&gt; the Dockerfile. Currently we ignore these, but we will be coming back to them later in this post.&lt;/p&gt;

&lt;p&gt;Lastly we set some &lt;code&gt;environment&lt;/code&gt; variables for our application/ASP.NET Core to use. Specifically around setting up a &lt;code&gt;Development&lt;/code&gt; environment and HTTP URLs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If we dont set these, our application will try and use HTTPS because that is the default now (which is a good thing). Because we haven’t set any self-signed developer certificates up yet this will become a bit of a pain. We will do it, but to get up and running, we are switching off HTTPS for now.&lt;/p&gt;

&lt;p&gt;We also pull down a &lt;strong&gt;MongoDB&lt;/strong&gt; image and start a new database, listening on port 27017. This is the standard port mapping for MongoDB.&lt;/p&gt;

&lt;p&gt;This command will also create a default network for your applications to live in. Normally named after the directory &lt;code&gt;docker-compose&lt;/code&gt; is run with a postfix of &lt;strong&gt;default&lt;/strong&gt;. So mine is &lt;code&gt;docker_default&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check the containers are running
&lt;/h2&gt;

&lt;p&gt;You can now run a &lt;code&gt;docker ps&lt;/code&gt; on your commandline of choice. You should see your &lt;code&gt;webapi_tutorial_debug&lt;/code&gt; and &lt;code&gt;mongodb&lt;/code&gt; ( &lt;strong&gt;Hint:&lt;/strong&gt; &lt;code&gt;container_name&lt;/code&gt; you set in the &lt;code&gt;docker-compose.dev.yml&lt;/code&gt; file).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdocker-ps.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdocker-ps.png" alt="docker ps"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop containers
&lt;/h2&gt;

&lt;p&gt;When you have finished you can clear up your containers by running the following, which will stop and remove the containers networks created for this service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose -f docker-compose.dev.yml down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Powershell Scripts (Optional)
&lt;/h2&gt;

&lt;p&gt;I have created two powershell scripts that automate this. You just need to run them in the root of the project:&lt;/p&gt;

&lt;h3&gt;
  
  
  Run containers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.\run.ps1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Close and clean containers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.\clean.ps1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Powershell Core is now cross platform as well so you can install Powershell Core and use these scripts on Mac and Linux boxes if you wish.&lt;/p&gt;

&lt;h2&gt;
  
  
  Debug Arguments
&lt;/h2&gt;

&lt;p&gt;Previously I mentioned we set a &lt;code&gt;arg&lt;/code&gt; called &lt;code&gt;buildconfig&lt;/code&gt; to &lt;code&gt;Debug&lt;/code&gt;. We haven’t used this so far. But we will now. I use this to build a debug version of our ASP.NET Core &lt;em&gt;BookStore.WebApi&lt;/em&gt; app, so we can debug into the container using &lt;em&gt;vsdbg&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Have a read of my article &lt;a href="https://dev.to/garfbradaz/debug-dotnet-core-in-docker-5c66"&gt;Debug .NET Core in Docker&lt;/a&gt; about what this is. For this article, change directory to &lt;code&gt;./src/api&lt;/code&gt; and make sure your &lt;code&gt;Dockerfile&lt;/code&gt; looks 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;FROM microsoft/dotnet:2.2-sdk AS build-env
ARG buildconfig
WORKDIR /app
COPY BookStore.WebApi.csproj .
COPY . .
RUN env
RUN if ["${buildconfig}" = "Debug"]; then \
        dotnet publish -o /publish -c Debug; \
    else \
        dotnet publish -o /publish -c Release; \
    fi 

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime-env
ARG buildconfig
ENV DEBIAN_FRONTEND noninteractive
WORKDIR /publish
COPY --from=build-env /publish .
RUN if ["${buildconfig}" = "Debug"]; then \
        apt-get update &amp;amp;&amp;amp; \
        apt-get install -y --no-install-recommends apt-utils &amp;amp;&amp;amp; \
        apt-get install curl unzip procps mongodb -y &amp;amp;&amp;amp; \
        curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /publish/vsdbg; \
     else \ 
        echo "*Whistling*"; \
    fi 
ENV DEBIAN_FRONTEND teletype
ENTRYPOINT ["dotnet","BookStore.WebApi.dll"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Next time
&lt;/h2&gt;

&lt;p&gt;That’s it for today. Remember all code is on &lt;a href="https://github.com/garfbradaz/webapi-episodes/tree/master/episode-4" rel="noopener noreferrer"&gt;Github&lt;/a&gt; if you want it.&lt;/p&gt;

&lt;p&gt;Now we have our architecture spun up and ready, we can start building some ASP.NET Core code (using C#) to start creating our Models for our &lt;em&gt;BookStore.WebApi&lt;/em&gt;. We will also start creating some unit tests and creating the shape of our API.&lt;/p&gt;

</description>
      <category>blog</category>
      <category>howto</category>
      <category>aspnetcore</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Episode 3 - JSON API using ASP.NET Core, Docker &amp; MongoDB: Docker Part I, Dockerfiles</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Sat, 29 Dec 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/episode-2---json-api-using-aspnet-core-docker--mongodb-docker-part-i-dockerfiles-2o8i</link>
      <guid>https://dev.to/garfbradaz/episode-2---json-api-using-aspnet-core-docker--mongodb-docker-part-i-dockerfiles-2o8i</guid>
      <description>&lt;h2&gt;
  
  
  Previously on Dcoding
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://dev.to/garfbradaz/episode-2-json-api-dotnet-core-docker-project-structure-3i0c"&gt;Episode 2&lt;/a&gt; I set up the project directory structure. Today’s episode is &lt;strong&gt;Docker Part 1: DockerFiles&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Lets talk about Docker
&lt;/h2&gt;

&lt;p&gt;Firstly before we get into how to set up Docker, I want to get into bigging the big D up! Apart from &lt;em&gt;Visual Studio Code&lt;/em&gt; and &lt;em&gt;WSL&lt;/em&gt;, no other technology has transformed how I develop code in 2018. Yes there are pain points, but once you get through those, then your development experience starts (i dare say it) become a little more blissful.&lt;/p&gt;

&lt;p&gt;I can now pull down pre-developed images of applications, databases, load balancers, webs servers, SDKs build tools, pixie dust (nic: testing you are still reading!), and use them like lego bricks, to build a system, &lt;em&gt;without&lt;/em&gt; installing the binaries on my machine.&lt;/p&gt;

&lt;p&gt;Take this tutorial for example, apart from the .NET SDK installed for the IDE, &lt;em&gt;MongoDB&lt;/em&gt;, &lt;em&gt;nginx&lt;/em&gt; and the &lt;em&gt;dotnet runtime&lt;/em&gt; are all installed via images, for which I can build and throw away when finished. No installation needed on my local development (host) machine. Even SQL Server runs on Linux now, and has a Image to pull down.&lt;/p&gt;

&lt;p&gt;This Lego Brick approach to building software now means I dont need to worry about installing all the tools on my machine, configure those tools and un-install when finished. I can package it all up in a &lt;em&gt;Dockerfile&lt;/em&gt; to use now, later or even on another machine, without polluting my host machine and slowing it down.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Docker?
&lt;/h3&gt;

&lt;p&gt;Docker is product that utilises a technology &lt;em&gt;containers&lt;/em&gt;, which have been around in Linux for a while. Containers are isolated/sandboxed processes, which only use the bare minimum binaries needed to run the application &lt;em&gt;within&lt;/em&gt; the container, including the file system. Unlike &lt;em&gt;Virtual Machine&lt;/em&gt; which run a whole operating system, and the bloat around it.&lt;/p&gt;

&lt;p&gt;Now as I was writing this post, &lt;a href="https://twitter.com/dswersky" rel="noopener noreferrer"&gt;Dave Swersky&lt;/a&gt;, wrote an awesome post on &lt;a href="https://dev.to/raygun/what-is-docker-and-why-is-it-so-popular-45c7"&gt;What is Docker, and why is it so popular?&lt;/a&gt;. Check that out.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dockerfile
&lt;/h2&gt;

&lt;p&gt;If this is the first time you have used Docker, or you do not have it installed, check out dockers &lt;a href="https://www.docker.com/get-started" rel="noopener noreferrer"&gt;getting started&lt;/a&gt; to start your journey. You will need to sign up to Docker Hub(&lt;a href="https://hub.docker.com/" rel="noopener noreferrer"&gt;https://hub.docker.com/&lt;/a&gt;) as well. Think as hub as the repository for Container Images you can access. Not just Dockers, but the communities as a whole. You may here the term &lt;em&gt;Container Registry&lt;/em&gt; for the Hub as well (You can set up your own private Registries on Azure for example).&lt;/p&gt;

&lt;p&gt;So a couple of commands and terminology we need to clear up, which are useful now before moving forward.&lt;/p&gt;

&lt;h3&gt;
  
  
  Help
&lt;/h3&gt;

&lt;p&gt;So the &lt;code&gt;docker&lt;/code&gt; command has a nice help facility, so if you want to see a list of docker commands:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Images
&lt;/h3&gt;

&lt;p&gt;A image is the package that includes everything needed to run your application, from the environment, code and configuration. You normally use other companies/communities/developers pre-built images from the hub or &lt;em&gt;base images&lt;/em&gt;, but you can &lt;a href="https://docs.docker.com/develop/develop-images/baseimages/" rel="noopener noreferrer"&gt;create your own&lt;/a&gt; just as easily.&lt;/p&gt;

&lt;p&gt;If you want to see which images you have on your host machine (your development PC/Mac), then run the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;If this is all new, then nothing will be listed currently, but when we start building our &lt;em&gt;Dockerfile&lt;/em&gt;, we will come back to the output on what this means.&lt;/p&gt;

&lt;h3&gt;
  
  
  Containers
&lt;/h3&gt;

&lt;p&gt;The container is your image &lt;em&gt;running&lt;/em&gt; in a discrete process. You can have multiple containers running on your host machine. Again there is a command to see what containers are running:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You can also use the short cut (One I personally use a lot):&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Develop first Dockerfile
&lt;/h2&gt;

&lt;p&gt;What is a Dockerfile then? Here is a good description from Docker &lt;a href="https://docs.docker.com/engine/reference/builder/#usage" rel="noopener noreferrer"&gt;themselves&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build users can create an automated build that executes several command-line instructions in succession.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To begin with, we will build our first Dockerfile to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compile our .NET Core Web API for the BookStore.&lt;/li&gt;
&lt;li&gt;Run our .NET Core Web API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We call &lt;em&gt;Dockerfiles&lt;/em&gt; that do more than one thing, &lt;em&gt;multi-stage&lt;/em&gt; builds. They allow us to &lt;em&gt;build&lt;/em&gt; and &lt;em&gt;run&lt;/em&gt; without maintaining separate Dockerfiles (which was the case once upon a time). Because I don’t learn this stuff on my own, let me link to &lt;a href="https://blog.alexellis.io/mutli-stage-docker-builds/" rel="noopener noreferrer"&gt;Alex Ellis&lt;/a&gt; talking more about this.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Future Post:&lt;/strong&gt; While I’m chatting about Alex, in the future I will be refactoring this &lt;strong&gt;BookStore.App&lt;/strong&gt; to use &lt;a href="https://www.openfaas.com/" rel="noopener noreferrer"&gt;OpenFaaS&lt;/a&gt; an open source serverless architecture that &lt;em&gt;isn’t&lt;/em&gt; coupled to one particular cloud. I’m excited about this, as I have wanted to use this tech for a while now, I even have the Raspberry Pis to cluster…..&lt;/p&gt;

&lt;p&gt;So, back to the matter at hand. Firstly we need to &lt;em&gt;change directory&lt;/em&gt; to &lt;code&gt;src/api&lt;/code&gt; directory we set up in &lt;a href="///blog/2018/12/19/Episode-2-JSON-API-Dotnet-Core-Docker-Project-Structure.html"&gt;Episode 2&lt;/a&gt;. Just to recap we set up the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
    ├── src
    | ├── api
    | |
    | ├── BookStore.WebApi.csproj
    ├── tests
    | ├── integration
    | ├── unit
    | ├── BookStore.Tests.csproj
    ├── docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create a file called &lt;code&gt;Dockerfile&lt;/code&gt; in the root of &lt;code&gt;api&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;.
    ├── src
    | ├── api
    | |
    | ├── BookStore.WebApi.csproj
    | ├── Dockerfile
    ├── tests
    | ├── integration
    | ├── unit
    | ├── BookStore.Tests.csproj
    ├── docker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following to the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app
COPY BookStore.WebApi.csproj .
COPY . .
RUN dotnet publish -c Release -o /publish

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS runtime-env
WORKDIR /publish
EXPOSE 5000
COPY --from=build-env /publish .
ENTRYPOINT ["dotnet","BookStore.WebApi.dll"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this is new to you, then let me explain the format on this file and what it means.&lt;/p&gt;

&lt;h3&gt;
  
  
  FROM
&lt;/h3&gt;

&lt;p&gt;This statement sets the &lt;em&gt;base image&lt;/em&gt; created by Microsoft (or any organisation who has created an base image) and creates a new build stage (each &lt;code&gt;FROM&lt;/code&gt; creates a new stage). These images will live on a public repository like &lt;em&gt;hub.docker.com&lt;/em&gt;. So make sure you have logged in via &lt;em&gt;Docker Desktop&lt;/em&gt; otherwise the first build step will fail.&lt;/p&gt;

&lt;p&gt;Having multiple &lt;code&gt;FROM&lt;/code&gt; denotes a multi-stage Dockerfile.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://hub.docker.com/r/microsoft/dotnet/" rel="noopener noreferrer"&gt;microsoft/dotnet:2.2-sdk&lt;/a&gt; - SDK image. This base image is designed to allow you to build/publish using the &lt;em&gt;dotnet CLI&lt;/em&gt;. As you can see I’m using the 2.2 .NET Core Version (denoted the common tag name of &lt;strong&gt;:2.2-sdk&lt;/strong&gt; ).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://hub.docker.com/r/microsoft/dotnet/" rel="noopener noreferrer"&gt;microsoft/dotnet:2.2-aspnetcore-runtime&lt;/a&gt; - Runtime image. This base image is designed to run a ASP.NET Core application using the &lt;em&gt;dotnet runtime&lt;/em&gt;. Again it follows the same format with denoting the dotnet version by the common tag of &lt;strong&gt;:2.2-aspnetcore-runtime&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  WORKDIR
&lt;/h3&gt;

&lt;p&gt;This statement will create a directory for the following statements will work in. If the directory exists, then the directory is just set to the value in &lt;code&gt;WORKDIR&lt;/code&gt;. In our case &lt;code&gt;/app&lt;/code&gt; is created by the command and &lt;code&gt;/publish&lt;/code&gt; is set to (as publish is created in the &lt;code&gt;dotnet publish&lt;/code&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  COPY
&lt;/h3&gt;

&lt;p&gt;This statement will copy the files specified &lt;em&gt;into&lt;/em&gt; the containers file system (normally into the file system specified in &lt;code&gt;WORKDIR&lt;/code&gt;) from the host machine of the directory where the Dockerfile is placed. So in our instance, the &lt;code&gt;../src/api&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We have two &lt;code&gt;COPY&lt;/code&gt; commands in the build stage of our Dockerfile context. We do this for the &lt;strong&gt;BookStore.WebApi.csproj&lt;/strong&gt; so that we have a build cache entry for this file, so if it hasn’t changed, we dont copy it on every build (reduces build times). Also &lt;code&gt;COPY . .&lt;/code&gt; will copy everything else.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY BookStore.WebApi.csproj .
COPY . .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our 2nd stage actually copies the output from &lt;code&gt;build-env&lt;/code&gt; &lt;strong&gt;/publish&lt;/strong&gt; directory into the next stage.&lt;/p&gt;

&lt;h3&gt;
  
  
  RUN
&lt;/h3&gt;

&lt;p&gt;This statement will run a set of commands that will create another layer in your image and commit the results, which will form part of the container. It allows you to set up your container before running it.&lt;/p&gt;

&lt;p&gt;Our example is the &lt;code&gt;dotnet publish&lt;/code&gt; will be &lt;code&gt;RUN&lt;/code&gt; as part of the &lt;code&gt;docker build&lt;/code&gt; instruction set, creating the binaries to use when we &lt;code&gt;dotnet&lt;/code&gt; using the published &lt;code&gt;.dll&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ENTRYPOINT
&lt;/h3&gt;

&lt;p&gt;This statement defines what is run when you &lt;code&gt;docker run&lt;/code&gt; and start the container. Our &lt;code&gt;ENTRYPOINT&lt;/code&gt; runs the &lt;code&gt;dotnet&lt;/code&gt; command using our published &lt;code&gt;.dll&lt;/code&gt;. You can pass more parameters into the &lt;code&gt;ENTRYPOINT&lt;/code&gt; command via &lt;code&gt;docker run&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our multi-stage Dockerfile builds our source code in the first stage and copies the published output so that it is run using the &lt;em&gt;.NET Core Runetime&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build the Image
&lt;/h2&gt;

&lt;p&gt;Now we have a Dockerfile, we can build this file into an &lt;em&gt;image&lt;/em&gt; which will be stored locally on our host. Make sure you are on the commandline of choice, and change directory to &lt;code&gt;../src/api&lt;/code&gt; and run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t garfbradaz/ep3-api .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will run a &lt;code&gt;docker build&lt;/code&gt;. The “.” signifies you want to use the &lt;em&gt;Dockerfile&lt;/em&gt; in the current directory and &lt;code&gt;-t garfbradaz/ep3-api&lt;/code&gt; is a parameter that will build an Image with a &lt;strong&gt;[-]t&lt;/strong&gt;ag name of &lt;em&gt;garfbradaz/ep3-api&lt;/em&gt;. You can replace this string with anything you want.&lt;/p&gt;

&lt;p&gt;The first time you run this will take a while, because there will be no &lt;em&gt;cache entries&lt;/em&gt; from previous builds for your layers. You can also see all the layers that are built from the Microsoft &lt;em&gt;base images&lt;/em&gt; which are made up of layers also:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdocker-net-layers.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdocker-net-layers.PNG" alt="docker-net-layers"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you run a &lt;code&gt;docker images&lt;/code&gt; again you will now see your image has been built (plus the .NET Core Images):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdotnet-net-images.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdotnet-net-images.PNG" alt="dotnet-net-images"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Run the Image as a Container
&lt;/h3&gt;

&lt;p&gt;So, we have created a &lt;em&gt;Dockerfile&lt;/em&gt; that includes the steps to build an image, which includes the resources needed to run the application within a &lt;em&gt;container&lt;/em&gt;. This is the magic, as we can run this image on any OS that supports Docker itself (If we are building &lt;em&gt;Linux&lt;/em&gt; containers).&lt;/p&gt;

&lt;p&gt;So the following will run your container as isolated/sandboxed process:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run --env ASPNETCORE_ENVIRONMENT=Development --env ASPNETCORE_URLS=http://+:5000 -p 5000:5000 -t --rm -it garfbradaz/ep3-api
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will run your Container interactively (&lt;code&gt;-it&lt;/code&gt;), and override some environment variables (&lt;code&gt;--env&lt;/code&gt;); overriding port mapping to 5000 (&lt;code&gt;-p&lt;/code&gt;) and clean up the container with finished (&lt;code&gt;--rm&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;I use Powershell to validate the container is running fine, but you can use &lt;em&gt;postman&lt;/em&gt; (we explore setting this up in another chapter).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invoke-RestMethod -Uri http://localhost:5000/api/values -Method 'Get'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should fail with a 401 status, because we set up the &lt;code&gt;[Authorize]&lt;/code&gt; attribute against the &lt;code&gt;ValuesController.cs&lt;/code&gt;. But at least this proves we are hitting the Web API running using Kestrel within the Container process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next time
&lt;/h3&gt;

&lt;p&gt;We will be exploring &lt;code&gt;docker compose&lt;/code&gt; to allow us to run and knit together multiple containers together so we can interact with MongoDB for example.&lt;/p&gt;

</description>
      <category>blog</category>
      <category>howto</category>
      <category>dotnetcore</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Release Notes: Update 1</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Sat, 29 Dec 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/release-notes-update-1-5co5</link>
      <guid>https://dev.to/garfbradaz/release-notes-update-1-5co5</guid>
      <description>&lt;h2&gt;
  
  
  Release Notes - Update 1
&lt;/h2&gt;

&lt;p&gt;Whenever I do an update to this &lt;a href="https://garfbradaz.github.io" rel="noopener noreferrer"&gt;blog&lt;/a&gt; itself, I will be providing some Release Notes on changes, including where possible, what I have done, as this may be helpful to someone else.&lt;/p&gt;

&lt;h3&gt;
  
  
  Version Notes
&lt;/h3&gt;

&lt;p&gt;I was going to use a proper Semantic Version (SemVer) for the site, but decided just to do incremental number releases (like Chrome). Just, well because!. No other reason than ease.&lt;/p&gt;

&lt;h3&gt;
  
  
  Blog Post Comments
&lt;/h3&gt;

&lt;p&gt;I have now switched on comments using the service &lt;a href="https://disqus.com/" rel="noopener noreferrer"&gt;Disqus&lt;/a&gt; for which you can sign up for free. Because Jekyll doesn’t have a back end data store, this made the most sense to me (&amp;amp; Disqus has good support).&lt;/p&gt;

&lt;p&gt;How did I do it? Well this is my site, so you may need to add it different places depending on your site structure, &lt;strong&gt;but&lt;/strong&gt; I followed the standard &lt;em&gt;Jekyll&lt;/em&gt; site creation for Github Pages, the structure is what I would call &lt;strong&gt;vanilla&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Sign up to Disqus.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Take note of the the &lt;em&gt;shortname&lt;/em&gt; you create for your site.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edit your &lt;code&gt;_config.yml&lt;/code&gt; and add the following:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;disqus:
    shortname: &amp;lt;replace-with-your-short-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a &lt;code&gt;comments.html&lt;/code&gt; and and save it to your &lt;code&gt;_includes&lt;/code&gt; directory with the following snippet. This is the embedded code provided by Disqus:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="comments"&amp;gt;
        &amp;lt;div id="disqus_thread"&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;script type="text/javascript"&amp;gt;
            var disqus_shortname = 'garfbradaz';
            (function() {
            var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
            dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
            (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();    
        &amp;lt;/script&amp;gt;
    &amp;lt;noscript&amp;gt;Please enable JavaScript to view the &amp;lt;a href="http://disqus.com/?ref_noscript"&amp;gt;comments powered by Disqus.&amp;lt;/a&amp;gt;&amp;lt;/noscript&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add a reference to &lt;code&gt;comments.html&lt;/code&gt; to your &lt;code&gt;posts.html&lt;/code&gt;. Add the following to the bottom of the &lt;code&gt;posts.html&lt;/code&gt; (or whatever your HTMl file is called):
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% include comments.html %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You need to turn a comment &lt;strong&gt;on&lt;/strong&gt; using the following Front Matter value in each post file:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;comments: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Rebuild your posts:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle exec jekyll build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Voila, you will have a nice comment at the bottom of your page:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdisqus.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdisqus.png" alt="disqus"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blog</category>
      <category>releasenotes</category>
      <category>disqus</category>
      <category>meta</category>
    </item>
    <item>
      <title>Episode 2 - JSON API using ASP.NET Core, Docker &amp; MongoDB: Project Structure</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Wed, 19 Dec 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/episode-2-json-api-dotnet-core-docker-project-structure-3i0c</link>
      <guid>https://dev.to/garfbradaz/episode-2-json-api-dotnet-core-docker-project-structure-3i0c</guid>
      <description>&lt;h2&gt;
  
  
  Previously on Dcoding
&lt;/h2&gt;

&lt;p&gt;In &lt;a href="https://dev.to/garfbradaz/episode-1-json-api-net-core-using-docker--mongo-26md"&gt;Episode 1&lt;/a&gt; I set the scene for what this series will contain. Today’s episode is simply on setting your &lt;em&gt;directory&lt;/em&gt; structure and creating the relevant projects. &lt;/p&gt;

&lt;h2&gt;
  
  
  Directory Structure
&lt;/h2&gt;

&lt;p&gt;Firstly, create a directory structure for your &lt;em&gt;Source (src)&lt;/em&gt; where our &lt;strong&gt;dotnet projects&lt;/strong&gt; will be saved, &lt;em&gt;tests&lt;/em&gt; will hold our &lt;strong&gt;Unit&lt;/strong&gt; and &lt;strong&gt;Integration&lt;/strong&gt; tests and lastly, &lt;em&gt;docker&lt;/em&gt; which will hold our &lt;strong&gt;docker-compose&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .
    ├── src
    ├── tests
    ├── docker

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Src
&lt;/h3&gt;

&lt;p&gt;Change directory to &lt;em&gt;src&lt;/em&gt; and we will run the following &lt;em&gt;dotnet&lt;/em&gt; command to new up a Project. Before we move on any further and do that though, lets talk about &lt;em&gt;dotnet new&lt;/em&gt; for people who have not used it.&lt;/p&gt;

&lt;p&gt;This command will create a dotnet project based off a default project template. You get a bunch of defaults with the SDK (&lt;a href="https://docs.microsoft.com/en-us/dotnet/core/tutorials/create-custom-template" rel="noopener noreferrer"&gt;and you can create your own&lt;/a&gt;. Run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new --help

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

&lt;/div&gt;



&lt;p&gt;This will give you a list of available templates you can use:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fep2-dotnet-new.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fep2-dotnet-new.png" alt="templates"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take note of the &lt;em&gt;short name&lt;/em&gt; as this is what we will be using next. So now we know what &lt;code&gt;dotnet new&lt;/code&gt; does, lets create the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet new webapi --auth Individual -o api -n BookStore.WebApi

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

&lt;/div&gt;



&lt;p&gt;This will create a folder with the name &lt;em&gt;api&lt;/em&gt; and in it will create a new &lt;em&gt;webapi&lt;/em&gt; project using authentication (we will explore this more in a later episode). The project will be called &lt;em&gt;BookStore.WebApi&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; You can do a &lt;code&gt;dotnet new webapi --help&lt;/code&gt; and see all the available switches for that short name.&lt;/p&gt;

&lt;p&gt;Your directory structure should look like this now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .
    ├── src
    | ├── api
    | |
    | ├── BookStore.WebApi
    ├── tests
    ├── docker

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Tests
&lt;/h3&gt;

&lt;p&gt;Change directory to the &lt;em&gt;tests&lt;/em&gt; folder. Firstly mainly create a &lt;em&gt;integration&lt;/em&gt; test folder. These will hold our &lt;strong&gt;Postman&lt;/strong&gt; tests (again for another episode). Secondly, we need to create our Unit Test project. I will be using &lt;em&gt;Xunit&lt;/em&gt;, but as you can see, there were a few different projects for testing when you did a &lt;code&gt;dotnet new --help&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;dotnet new xunit -o unit -n BookStore.Tests

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

&lt;/div&gt;



&lt;p&gt;Again this will create a &lt;em&gt;xunit&lt;/em&gt; project type within a folder called &lt;em&gt;unit&lt;/em&gt;. The project name will be &lt;em&gt;BookStore.Tests&lt;/em&gt;. Within the &lt;em&gt;integration&lt;/em&gt; folder just add an empty &lt;strong&gt;postman.json&lt;/strong&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .
    ├── src
    | ├── api
    | |
    | ├── BookStore.WebApi
    ├── tests
    | ├── integration
    | ├── unit
    | ├── BookStore.Tests
    ├── docker

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Docker
&lt;/h3&gt;

&lt;p&gt;Change directory to &lt;em&gt;docker&lt;/em&gt; folder and for now just create two empty files:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;docker-compose.yml&lt;/p&gt;

&lt;p&gt;docker-compose.dev.yml&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes you guessed it, this files will become apparent (and fatter) in a future episode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next time
&lt;/h2&gt;

&lt;p&gt;So now we have set up our Project directory structure, we can move on to setting up our &lt;strong&gt;Docker&lt;/strong&gt; environment, using &lt;em&gt;Dockerfiles&lt;/em&gt; and &lt;em&gt;docker-compose&lt;/em&gt;. Join me next time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note:&lt;/strong&gt; Episode 1 can be found on my &lt;a href="https://github.com/garfbradaz/webapi-episodes" rel="noopener noreferrer"&gt;Github Repository&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blog</category>
      <category>howto</category>
      <category>dotnetcore</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Episode 1 - JSON API using ASP.NET Core, Docker &amp; MongoDB</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Tue, 18 Dec 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/episode-1-json-api-net-core-using-docker--mongo-26md</link>
      <guid>https://dev.to/garfbradaz/episode-1-json-api-net-core-using-docker--mongo-26md</guid>
      <description>

&lt;h2&gt;
  
  
  Inspiration
&lt;/h2&gt;

&lt;p&gt;There is a series on &lt;a href="https://dev.o"&gt;dev.to&lt;/a&gt; that I have followed by &lt;a href="https://dev.to/nathanabland"&gt;Nathan Bland&lt;/a&gt; who has recorded as series of videos around creating a JSON API, using &lt;em&gt;Node.js&lt;/em&gt;, &lt;em&gt;docker&lt;/em&gt; and &lt;em&gt;Express&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I liked the idea around this, so I’m going to do the same, but in a blog style, but replacing &lt;em&gt;Node.js&lt;/em&gt; and &lt;em&gt;Express&lt;/em&gt; with &lt;em&gt;ASP.NET Core WebAPI&lt;/em&gt;. I also want to use a MongoDB as a the backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set the Scene
&lt;/h2&gt;

&lt;p&gt;The API itself will be for a &lt;em&gt;Book Shop Chain&lt;/em&gt; who want to expose their stores and titles via a public RESTful service for other companies to use. This can be used for stock lookups and to see which stores have stock available to sell.&lt;/p&gt;

&lt;p&gt;Here are some example Epics to show the shape of the API needed:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As a &lt;em&gt;book store_I can _add&lt;/em&gt; our &lt;em&gt;store&lt;/em&gt; to the database_So_ we can be accessible&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;book store_I can _add&lt;/em&gt; our &lt;em&gt;inventory&lt;/em&gt; to our database_So_ we can expose our inventory&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;book store_We can _update&lt;/em&gt; a books stock level_For_ an accurate stock level&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;API consumer_I can look up a _stores address&lt;/em&gt;&lt;em&gt;So&lt;/em&gt; we know where to buy a &lt;em&gt;book&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As a &lt;em&gt;API consumer_I can look up a _book&lt;/em&gt;&lt;em&gt;So&lt;/em&gt; we can get a &lt;em&gt;list&lt;/em&gt; of &lt;em&gt;stores&lt;/em&gt; who sell a_book_&lt;/p&gt;

&lt;p&gt;As &lt;em&gt;book store IT Security_We can add _API Keys&lt;/em&gt; to the API_For_ API Consumers to use when querying the API&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What to expect
&lt;/h2&gt;

&lt;p&gt;My plan is to release weekly blog posts covering in a similar fashion like Nathan’s:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setting up your .NET Core Project Structure.&lt;/li&gt;
&lt;li&gt;Setting up docker environment using docker-compose.&lt;/li&gt;
&lt;li&gt;Modelling API JSON back end using Mongo.&lt;/li&gt;
&lt;li&gt;Modelling API using Swagger.&lt;/li&gt;
&lt;li&gt;Unit Tests with Xunit.&lt;/li&gt;
&lt;li&gt;Integration Tests with Postman.&lt;/li&gt;
&lt;li&gt;Skeleton Book Store API (Routes etc).&lt;/li&gt;
&lt;li&gt;Skeleton Inventory API (Routes etc).&lt;/li&gt;
&lt;li&gt;Securing your API.&lt;/li&gt;
&lt;li&gt;Data validation.&lt;/li&gt;
&lt;li&gt;Pagination for large data queries.&lt;/li&gt;
&lt;li&gt;Filter and Search.&lt;/li&gt;
&lt;li&gt;Hooking up to Azure Dev Ops for Build and Release Pipeline.&lt;/li&gt;
&lt;li&gt;Publisher Swagger Documentation on Github.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will post any code on Github as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start at Zero
&lt;/h2&gt;

&lt;p&gt;So before starting cracking on with this, make sure your environment is set up. Because this is &lt;em&gt;.NET Core&lt;/em&gt;, this is cross platform, so if you own a Mac or Linux box, feel free to join in. I’m going to be building this on a &lt;em&gt;Surface Book&lt;/em&gt;, but I will try to be a good Xpat-Citizen and test across environments.&lt;/p&gt;

&lt;p&gt;Please make sure the following is installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dotnet.microsoft.com/download"&gt;Dotnet Core&lt;/a&gt; - I’m currently using 2.2.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.docker.com/get-started"&gt;Docker&lt;/a&gt; - You will need to also create a login for &lt;a href="https://hub.docker.com/signup"&gt;Docker Hub&lt;/a&gt; if you do not have one.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.microsoft.com/en-gb/powershell/scripting/overview?view=powershell-6"&gt;Powershell Core&lt;/a&gt; - PowerShell Core is cross-platform and runs on Windows, Linux, and MacOS while the old one runs on Windows only(CLI). There are options to install on the OS of choice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We wont need to install Mongo as we will be running it using &lt;em&gt;Docker&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  IDE
&lt;/h2&gt;

&lt;p&gt;I will be using &lt;em&gt;Visual Studio Code&lt;/em&gt; for cross platform .NET Core support. I use a bunch of extension which I have put here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/garfbradaz/be010080df076d19892bf4ec71127b53"&gt;https://gist.github.com/garfbradaz/be010080df076d19892bf4ec71127b53&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can import these using the following &lt;em&gt;Settings Sync&lt;/em&gt; extension on the marketplace:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=Shan.code-settings-sync"&gt;https://marketplace.visualstudio.com/items?itemName=Shan.code-settings-sync&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you already use &lt;em&gt;VSCode&lt;/em&gt;, you may just want to find the &lt;em&gt;C#&lt;/em&gt;, &lt;em&gt;Docker&lt;/em&gt; &lt;em&gt;GitLens&lt;/em&gt; and &lt;em&gt;.NET Core Test Explorer&lt;/em&gt; &lt;a href="https://code.visualstudio.com/docs/editor/extension-gallery"&gt;extensions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; My list contains extensions like &lt;em&gt;Jekyll&lt;/em&gt; support which you wont need for this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Next time
&lt;/h2&gt;

&lt;p&gt;We will create the &lt;em&gt;ASP.NET Core WebAPI&lt;/em&gt; template, set up the project structure. Join me next time!.&lt;/p&gt;


</description>
      <category>blog</category>
      <category>howto</category>
      <category>dotnetcore</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Debug .NET Core In Docker</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Thu, 13 Dec 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/debug-dotnet-core-in-docker-5c66</link>
      <guid>https://dev.to/garfbradaz/debug-dotnet-core-in-docker-5c66</guid>
      <description>&lt;h2&gt;
  
  
  Debugging ASP.NET Core in a Linux Docker with Visual Studio Code
&lt;/h2&gt;

&lt;p&gt;So, you have started using Docker for your .NET Core applications - awesome news. You are using Linux Containers, and Visual Studio Code, and you then go to Debug your container…..oh. (I’m using Windows).&lt;/p&gt;

&lt;p&gt;Its not the most straight forward process I have found, if you are looking at getting this up and running. Firstly I didn’t figure this out on my own, the reason I’m blogging about it is because I meshed together the knowledge I had, plus these very informative posts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="http://blog.jonathanchannon.com/2017/06/07/debugging-netcore-docker/" rel="noopener noreferrer"&gt;Jonathan Channon&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.richard-banks.org/2018/07/debugging-core-in-docker.html" rel="noopener noreferrer"&gt;Richard Banks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It was the above two posts that glued it together for me. Have a good read of those as well. Before we go any further, just to confirm these instructions are for &lt;em&gt;Visual Studio Code&lt;/em&gt; and &lt;em&gt;Linux Containers&lt;/em&gt; using &lt;em&gt;Docker Compose&lt;/em&gt;, I’m not sure how this is set-up on Visual Studio. I’m presuming you have &lt;em&gt;VSCode&lt;/em&gt;, &lt;em&gt;Docker&lt;/em&gt; and &lt;em&gt;.NET Core SDK&lt;/em&gt; installed. And you are using Linux Containers.&lt;/p&gt;

&lt;h2&gt;
  
  
  DockerFile
&lt;/h2&gt;

&lt;p&gt;Firstly lets look at my multi-staged (Multiple &lt;code&gt;FROM&lt;/code&gt;) Dockerfile for building my project.&lt;/p&gt;

&lt;h3&gt;
  
  
  1st Stage Build
&lt;/h3&gt;

&lt;p&gt;The first part of the Dockerfile &lt;code&gt;build-env&lt;/code&gt; stage, takes an &lt;code&gt;ARG&lt;/code&gt; of &lt;code&gt;buildconfig&lt;/code&gt;, which controls how the &lt;code&gt;dotnet publish&lt;/code&gt; is configured. And yes I know, I have a todo item to refactor the if/else to just pass the &lt;code&gt;buildconfig&lt;/code&gt; as a &lt;code&gt;-c&lt;/code&gt; value.&lt;/p&gt;

&lt;h3&gt;
  
  
  2nd Stage Run
&lt;/h3&gt;

&lt;p&gt;So the 1st stage handled the build, the 2nd handles the running of the Web API project. As you can see, there is a &lt;code&gt;Debug&lt;/code&gt; switch again, to install some dependencies when running in &lt;code&gt;Debug&lt;/code&gt;. Ignore the &lt;strong&gt;mongo&lt;/strong&gt; installs, it is the &lt;em&gt;vsdbg&lt;/em&gt; installation We are interested in.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The debugger platform has been expanded between Visual Studio 2017 15.3 and previous versions of Visual Studio. Visual Studio can now communicate using the debug adapter protocol which is used by Visual Studio, Visual Studio Code and Visual Studio for Mac. This protocol is now used for debugging cross-platform .NET Core applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/Microsoft/MIEngine/wiki/Offroad-Debugging-of-.NET-Core-on-Linux---OSX-from-Visual-Studio" rel="noopener noreferrer"&gt;Source&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So vsdbg is installed &lt;strong&gt;into&lt;/strong&gt; the container to allow remote debugging from your host machine using Visual Studio Code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM microsoft/dotnet:2.2-sdk AS build-env
ARG buildconfig
WORKDIR /app
COPY ServiceApp.WebApi.csproj .
RUN dotnet restore
COPY . .
RUN if ["${buildconfig}" = "Debug"]; then \
        dotnet publish -o /publish -c Debug; \
    else \
        dotnet publish -o /publish -c Release; \
    fi

FROM microsoft/dotnet:2.2-aspnetcore-runtime
ARG buildconfig
ENV DEBIAN_FRONTEND noninteractive
WORKDIR /publish
COPY --from=build-env /publish .
RUN if ["${buildconfig}" = "Debug"]; then \
        apt-get update &amp;amp;&amp;amp; \
        apt-get install -y --no-install-recommends apt-utils &amp;amp;&amp;amp; \
        apt-get install curl unzip procps mongodb -y &amp;amp;&amp;amp; \
        curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l /publish/vsdbg; \
     else \
        echo "*Whistling*"; \
    fi
ENV DEBIAN_FRONTEND teletype
ENTRYPOINT ["dotnet","ServiceApp.WebApi.dll"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Launch Task Configuration for Visual Studio Code
&lt;/h2&gt;

&lt;p&gt;So this was the bit I struggled with, to the point I &lt;a href="https://github.com/OmniSharp/omnisharp-vscode/issues/2724" rel="noopener noreferrer"&gt;logged a Github Issue&lt;/a&gt; and subsequently had a light bulb moment afterwards!&lt;/p&gt;

&lt;p&gt;Both Jonathan and Richards work and investigations for their posts, again helped tenfold.&lt;/p&gt;

&lt;p&gt;Important bits are the &lt;code&gt;sourceFileMap&lt;/code&gt; which map your projects code to the binaries in the Container. The &lt;em&gt;/app&lt;/em&gt; in reference in mine, relates to the &lt;em&gt;/app&lt;/em&gt; directory declared in the Dockerfile.&lt;/p&gt;

&lt;p&gt;The other important snippet is the &lt;code&gt;debuggerPath&lt;/code&gt; option. This is the directory where vsdbg has been installed within your Container. You can set different directories within your &lt;code&gt;curl&lt;/code&gt; command in the Dockerfile if you so wish.&lt;/p&gt;

&lt;p&gt;Lastly, note the Container name of &lt;code&gt;webapi_debug&lt;/code&gt;, I set this up in the relevant compose file.&lt;/p&gt;

&lt;p&gt;The below will live in a &lt;code&gt;launch.json&lt;/code&gt; in your &lt;code&gt;.vscode&lt;/code&gt; directory. If configured correctly you should get a nice launch icon:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff53j5ifvoq4pq4h0dp61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff53j5ifvoq4pq4h0dp61.png" alt="icon"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "name": "Attach to webapi (Docker)",
    "type": "coreclr",
    "request": "attach",
    "sourceFileMap": {
        "/app": "${workspaceFolder}/src/api"
    },
    "processId" : "${command:pickRemoteProcess}",
    "pipeTransport": {
        "debuggerPath": "/publish/vsdbg/vsdbg",
        "pipeProgram": "docker",
        "pipeCwd": "${workspaceFolder}/src/api",
        "quoteArgs": false,
        "pipeArgs": [
            "exec","-i", "webapi_debug"
        ]
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Composing
&lt;/h2&gt;

&lt;p&gt;Because I’m using separate Docker containers for &lt;em&gt;MongoDB&lt;/em&gt; and &lt;em&gt;Identity Server&lt;/em&gt; I use &lt;code&gt;docker-compose&lt;/code&gt; to knit together my containers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; I’m planning on doing a Identity Server post next on my experiences in getting that set-up.&lt;/p&gt;

&lt;p&gt;The below is from my &lt;code&gt;docker-compose.dev.yml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;So the container with the interesting configurations is &lt;code&gt;webapi&lt;/code&gt; which is my ASP.NET Core Web API project. As you can see I set the container name to &lt;code&gt;webapi_debug&lt;/code&gt; is set. As I mentioned before, the launch task needs that to remote into that particular container.&lt;/p&gt;

&lt;p&gt;You can also see I mount my Source Code directory from my &lt;strong&gt;host&lt;/strong&gt; Windows machine using &lt;code&gt;volumes&lt;/code&gt; using a relative path to my source code directory (&lt;em&gt;../api&lt;/em&gt;) to &lt;em&gt;:/app&lt;/em&gt;, allowing us, when a breakpoint is hit, for the code to be shown. If you configure this bit wrong, you will get the following error when the breakpoint is thrown:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6cn9zn9168p34nunvhp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo6cn9zn9168p34nunvhp.png" alt="file-not-found"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The other interesting config points, are the &lt;strong&gt;ASPNETCORE_&lt;/strong&gt; * environment variables for &lt;code&gt;Development&lt;/code&gt; and the &lt;code&gt;buildconfig&lt;/code&gt; argument of &lt;code&gt;Debug&lt;/code&gt;, which as we learnt previously, is passed into the &lt;code&gt;Dockerfile&lt;/code&gt; and used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: "3"
services:
  identity:
    image: garfbradaz/serviceident:develop
    container_name: identity
    build:
      args:
        buildconfig: Debug
      context: ../quickidentity
      dockerfile: Dockerfile
    volumes:
      - ../quickidentity:/app2
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://identity:5002  
      - AuthorityUrl=http://identity:5002
    ports:
      - "5002:5002"
  webapi:
    image: garfbradaz/serviceapp:develop
    container_name: webapi_debug
    build:
      args:
        buildconfig: Debug
      context: ../api
      dockerfile: Dockerfile
    volumes:
      - ../api:/app
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://+:5001
      - AuthorityUrl=http://identity:5002
    ports:
      - "5001:5001"
    depends_on:
      - mongodb
      - identity
  mongodb:
    image: mongo:latest
    container_name: mongodb
    ports:
      - "27017:27017"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running
&lt;/h2&gt;

&lt;p&gt;So put this all together you can run your containers using the Compose:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker-compose -f docker-compose.dev.yml up -d --build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Set a breakpoint in your code (Example I set in my Controller), and run your debug task in &lt;em&gt;Visual Studio Code&lt;/em&gt;. This will ask you to pick your process for your &lt;code&gt;dotnet&lt;/code&gt; code. Then when the breakpoint is hit, you can inspect it as normal.&lt;/p&gt;

&lt;p&gt;I find that it can hang and take half a minute sometimes for the debug session to initiate, so bear with it.&lt;/p&gt;

&lt;p&gt;Anyway I hope this helps someone, and a massive thank you to &lt;a href="https://twitter.com/jchannon" rel="noopener noreferrer"&gt;Jonathan&lt;/a&gt; and &lt;a href="https://twitter.com/rbanks54" rel="noopener noreferrer"&gt;Richard&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>blog</category>
      <category>howto</category>
      <category>dotnetcore</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Setting Up Github Pages Jekyll And Using Windows Subsystem For Linux</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Wed, 12 Dec 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/setting-up-github-pages-jekyll-and-using-windows-subsystem-for-linux-470h</link>
      <guid>https://dev.to/garfbradaz/setting-up-github-pages-jekyll-and-using-windows-subsystem-for-linux-470h</guid>
      <description>&lt;h2&gt;
  
  
  Setting up Github Pages, Jekyll and using Windows Subsystem for Linux
&lt;/h2&gt;

&lt;p&gt;The 1st &lt;em&gt;how to&lt;/em&gt; for setting up the blog and tools needed to edit it. I’m a Windows fan (I love my Surface devices), but I’m also starting to appreciate Linux, so this is a perfect opportunity to set up my Surface Book to use &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/faq" rel="noopener noreferrer"&gt;Windows Subsystem for Linux&lt;/a&gt;, which allows you command line access to Linux. I’m assured this is not a virtual machine, but a &lt;a href="https://blogs.msdn.microsoft.com/wsl/2016/04/22/windows-subsystem-for-linux-overview/" rel="noopener noreferrer"&gt;syscall mapping between Windows and Linux itself&lt;/a&gt;, to actual Linux binaries themselves.&lt;/p&gt;

&lt;p&gt;I’m going to provide the instructions on how I set up &lt;em&gt;my&lt;/em&gt; Surface Book. You may want to tweak these to your needs.&lt;/p&gt;

&lt;p&gt;NB: I originally posted this blog post &lt;strong&gt;December 2018&lt;/strong&gt; so if anything has changed or doesn’t work, let me know.&lt;/p&gt;

&lt;h2&gt;
  
  
  WSL Set up
&lt;/h2&gt;

&lt;p&gt;Windows Subsystem for linux needs setting up. If you have done this previously, then skip to code instructions&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Use Powershell Windows Subsystem for Linux (WSL), make sure you Run as Administrator:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The prompt will ask you to restart your machine, so make sure you save anything before pressing &lt;strong&gt;Y&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Frelease-notes_powershell.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Frelease-notes_powershell.png" alt="Powershell screen shot"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use the Windows Store to install your favourite Linux Distro. I chose &lt;em&gt;Debian&lt;/em&gt;. More information &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10#windows-10-fall-creators-update-and-later-install-from-the-microsoft-store" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Visual Studio Code
&lt;/h2&gt;

&lt;p&gt;Again if you have installed this previously, then jump to Jekyll.&lt;/p&gt;

&lt;p&gt;I’m a great VSCode advocate, mainly from using .NET Core and C#. But I’m using TypeScript a little and using it more and more. I actually get a little sad face when I realise I need to use Visual Studio Enterprise for anything now (Usually because Razor support isn’t the best in Code, but improving).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I’m going to use Visual Studio Code to allow me to compose and edit markdown. Its the perfect editor because:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;The IDE is cross platform, so you can look after your blog within the IDE     across any platform.&lt;/li&gt;
&lt;li&gt;It has git out of the box.&lt;/li&gt;
&lt;li&gt;Various Markdown extensions for extending the experience.&lt;/li&gt;
&lt;li&gt;Markdown Preview, so you can view how your post will look.
-I will do a separate post on my Visual Code extension set up.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Download and install Code &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Install Jekyll on WSL
&lt;/h2&gt;

&lt;p&gt;As I mentioned previously, I installed Debian, so the instructions I’m giving are for Debian. More more information see &lt;a href="https://jekyllrb.com/docs/installation/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Firstly open a bash Window. I fired up Debian app I just installed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install dependencies for Ruby:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get install ruby-full build-essential&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now, I got the following error when I ran this on Debian:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reading state information… Done &lt;br&gt;
E: Unable to locate package ruby-full &lt;br&gt;
E:     Unable to locate package build-essential&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Essentially the &lt;em&gt;sources.lst&lt;/em&gt; is missing the &lt;em&gt;deb-src&lt;/em&gt; links to the repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdeb-src.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdeb-src.png" alt="deb-src"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;sudo nano /etc/apt/sources.list&lt;/code&gt; to edit the file and add the missing entries. Here is my file&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdeb-src-added.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fdeb-src-added.png" alt="deb-src"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;sudo apt update&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Re-run step 2. If this works, move on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the environment variables for gems for when bash is run interactively:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;echo '# Install Ruby Gems to ~/gems' &amp;gt;&amp;gt; ~/.bashrc&lt;/code&gt;&lt;br&gt;
&lt;code&gt;echo 'export GEM_HOME=$HOME/gems' &amp;gt;&amp;gt; ~/.bashrc&lt;/code&gt;&lt;br&gt;
&lt;code&gt;echo 'export PATH=$HOME/gems/bin:$PATH' &amp;gt;&amp;gt; ~/.bashrc&lt;/code&gt;&lt;br&gt;
&lt;code&gt;source ~/.bashrc&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Install Jekyll and bundler using &lt;code&gt;gem install jekyll bundler&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Confirm you have &lt;em&gt;GCC&lt;/em&gt; and &lt;em&gt;Make&lt;/em&gt; available:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;    gcc &lt;span class="nt"&gt;-v&lt;/span&gt;
    g++ &lt;span class="nt"&gt;-v&lt;/span&gt;
    make &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Install Git on WSL
&lt;/h2&gt;

&lt;p&gt;You may already have &lt;em&gt;Git for Windows&lt;/em&gt; set up on your Windows instance, but you will need to make sure Git is on WSL as well. Follow these instructions.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install git:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt install git&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To validate it has worked, run &lt;code&gt;git version&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Set up standard email and username details:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     git config --global user.email "your@email.com"
     git config --global user.name "your username"
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;As described &lt;a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_code_core_autocrlf_code" rel="noopener noreferrer"&gt;here&lt;/a&gt;, need to set your line endings correctly.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git config --global core.autocrlf input&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Create Github Repository
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;There are different steps that need taking when creating your repository. Firstly the repository names needs to be a specific format:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;your-github-username.github.io&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can find your username my navigating (top right) click Your &lt;em&gt;Avatar-&amp;gt;Your profile&lt;/em&gt;. Example mine is garfbradaz:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;![me](https://garfbradaz.github.io/assets/img/posts/me-github.png)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So mine is &lt;em&gt;garfbradaz.github.io&lt;/em&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;You can select a Jekyll theme as well also. This is saved to the &lt;code&gt;_config.yml&lt;/code&gt; file in your repository. This can be changed at a later date.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Ftheme-%2520%2520%2520%2520github.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Ftheme-%2520%2520%2520%2520github.png" alt="github options"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Once the site is published, your repository name is your URL for your new site:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://garfbradaz.github.io" rel="noopener noreferrer"&gt;my site&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Update Github with generated SSH using WSL
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Run the following in WSL bash terminal to generate a key, replacing the email with yours that you use for Github:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh-keygen -t rsa -b 4096 -C &amp;lt;your@githubemail.com&amp;gt;&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Press &lt;strong&gt;Enter&lt;/strong&gt; to accept the default directory, and add a pass phrase.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Add the SSH private key to &lt;em&gt;ssh-agent&lt;/em&gt; so that you dont need to write the pass phrase each and every git commit:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh-add ~/.ssh/id_rsa&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is using &lt;strong&gt;~&lt;/strong&gt; which is fine as we are within a Linux bash terminal.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Run the following to Print tne RSA key to the screen. Then using your mouse, copy to clipboard:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cat ~/.ssh/id_rsa.pub&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to github in your browser of choice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Your profile&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;SSH and GPG keys&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on button &lt;strong&gt;New SSH Key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Give the key a meaningful title.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Paste RSA key.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Add RSA key&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Clone the Site to WSL
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Firstly run &lt;em&gt;Visual Studio Code&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open a Terminal within Visual Studio code. One option for doing that is &lt;strong&gt;Menu-&amp;gt;Terminal-&amp;gt;New Terminal&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then run bash.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Something important to note. After you run bash, you will be greeted with a directory prefixed with &lt;code&gt;/mnt/c&lt;/code&gt;. This means the drive is mounted and mapped to &lt;code&gt;C:\&lt;/code&gt;. You can prove this by doing a &lt;code&gt;ls&lt;/code&gt;. Look at all those familiar Windows files:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fwindows-files-bash.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fwindows-files-bash.png" alt="WSL Bash Directory Listing"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;code&gt;cd ~ &amp;amp;&amp;amp; pwd&lt;/code&gt; isn’t your traditional home directory, so dont place any files in here you want Windows to access. This &lt;strong&gt;DOES NOT&lt;/strong&gt; map to your Windows Home directory. If you edit this Linux Only Windows files, you will corrupt your Linux install.&lt;/p&gt;

&lt;p&gt;Basically it is safe to use the &lt;code&gt;/mnt/c&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;With that in mind, navigate to the folder you wish to clone your Jekyll repository, here is an example for mine (You do this in the same bash terminal you created in Visual Studio Code):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd /mnt/c/code/blog&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is the following Windows directory:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; C:\code\blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Git clone into this directory:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git clone &amp;lt;your-repository-url&amp;gt; .&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And because you are within Visual Studio Code, the files will appear straight away:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;![files appear in VSCode](https://garfbradaz.github.io/assets/img/posts/git-in-vscode.png)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Now create a file called &lt;em&gt;Gemfile&lt;/em&gt; and add the following (root of the directory):&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;source ‘&lt;a href="https://rubygems.org%E2%80%99" rel="noopener noreferrer"&gt;https://rubygems.org’&lt;/a&gt; gem ‘github-pages’, group: :jekyll_plugins&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fgem-in-vscode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgarfbradaz.github.io%2Fassets%2Fimg%2Fposts%2Fgem-in-vscode.png" alt="files appear in VSCode"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you can see VSCode recognises up its a Gemfile and uses a nice Ruby icon.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Now create the dependencies. Within the bash terminal within Visual Studio code run:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bundle install&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; At this stage for Debian, I found there was an error installing &lt;strong&gt;nokogirl-1.8.5&lt;/strong&gt; dependencies.&lt;/p&gt;

&lt;p&gt;I resolved this by running the following, then re-running &lt;code&gt;bundle install&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`sudo apt-get install libpng-dev`
`sudo apt-get install --reinstall zlibc zlib1g zlib1g-dev`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  (Optional) Modify Layout of Theme
&lt;/h2&gt;

&lt;p&gt;If you want to amend the layout; add your own styles or navigation, then you need to check your themes repository for instructions on how to update (each may have different instructions).&lt;/p&gt;

&lt;p&gt;The URL scheme for the repositories is:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; https://github.com/pages-themes/&amp;lt;your-selected-theme&amp;gt;&amp;lt;/your-selected-theme&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So mine is:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;a href="https://github.com/pages-themes/minimal" rel="noopener noreferrer"&gt;https://github.com/pages-themes/minimal&lt;/a&gt;&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Update _config.yml&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Update your title and description for your blog.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; title: Gareth Bradleys Blog&lt;br&gt;
&amp;gt; description: garfbradaz new blog, hosted on github&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Release, Release, Release...&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;After you have proof read it and checked obviously. The beauty of Jekyll is you to &lt;strong&gt;publish&lt;/strong&gt; your blog, you just use the git skills you (may) already have, so run the following in bash while in your repository where your &lt;code&gt;.git&lt;/code&gt; folder is:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&lt;code&gt;git add .&lt;/code&gt;&lt;br&gt;
&lt;code&gt;git commit -m "Start of life for my awesome blog - Arise!"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;git push origin&lt;/code&gt;&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Next time…&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;I will talk about &lt;em&gt;How to Add Google analytics&lt;/em&gt; and &lt;em&gt;How to publish your first post&lt;/em&gt; all in Visual Studio Code.&lt;/p&gt;

&lt;p&gt;I haven’t written this blog post without help. I would like to shout out to the following blogs and documentation for assisting me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://daverupert.com/2018/04/developing-on-windows-with-wsl-and-visual-studio-code/" rel="noopener noreferrer"&gt;Dave Rupert&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://pages.github.com/" rel="noopener noreferrer"&gt;Github Pages&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/" rel="noopener noreferrer"&gt;Github Docs - SSH&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blogs.msdn.microsoft.com/commandline/2016/11/17/do-not-change-linux-files-using-windows-apps-and-tools/" rel="noopener noreferrer"&gt;Microsoft WSL Blog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jekyllrb.com/docs/installation/ubuntu/" rel="noopener noreferrer"&gt;Jekyll Docs - Ubuntu&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://jekyllrb.com/docs/" rel="noopener noreferrer"&gt;Jekyll Docs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blog</category>
      <category>jekyll</category>
      <category>howto</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Arise New Blog</title>
      <dc:creator>Gareth Bradley</dc:creator>
      <pubDate>Sat, 07 Jul 2018 00:00:00 +0000</pubDate>
      <link>https://dev.to/garfbradaz/arise-new-blog-362f</link>
      <guid>https://dev.to/garfbradaz/arise-new-blog-362f</guid>
      <description>&lt;h2&gt;
  
  
  It’s alive!
&lt;/h2&gt;

&lt;p&gt;I have wanted to move &lt;a href="https://mywebanecdotes.com/"&gt;my current&lt;/a&gt; off Wordpress for sometime, but time has always been a factor. As a developer I find myself living within &lt;em&gt;Visual Studio Code&lt;/em&gt;, so I decided to set up a blog which can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use my Visual Studio Code&lt;/li&gt;
&lt;li&gt;Use my git skills&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I knew github has &lt;a href="https://pages.github.com/"&gt;Github Pages&lt;/a&gt;, and it used &lt;a href="https://jekyllrb.com/"&gt;Jekyll&lt;/a&gt;. So I have started a Github page. So this is day one. I chose a minimal theme for the site, and each change to the site I will document as a blog post. Think of them as Release Notes!&lt;/p&gt;

&lt;p&gt;As I move on I change the style and layout etc, but for now (and posterity) here is the Home Page from day 1:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hF26NnUd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://garfbradaz.github.io/assets/img/posts/day_1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hF26NnUd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://garfbradaz.github.io/assets/img/posts/day_1.PNG" alt="Day 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://garfbradaz.github.io/blog/2018/07/07/Arise-New-Blog.html"&gt;Source Post&lt;/a&gt;&lt;/p&gt;

</description>
      <category>blog</category>
      <category>misc</category>
      <category>jekyll</category>
    </item>
  </channel>
</rss>
