@ Getting started with .netcore web site development on Mac (or Linux) using MSSql2017 on docker.
By: Balinder Singh
Tags: Cross Platform, First Post, Docker, Net Core, Ms Sql Server

Background

Since this is my first article, I would like to share little bit of my background.

I have been doing programming for about 7 years now. During this journey, I have been very lucky as I got to work with such wonderful people along the way.

For first 5+ years I worked for a company called Visualantidote (a Toronto based web development/consulting shop and also developed few products), such a great company to work for and very nice people and great culture. There I worked in web development and later got a chance to work on a very cool product from the ground up. Then it got acquired by Formstack (a form building SAAS company) and now I work for this company which is remote -first company and with a great culture and very smart people. I can go on talking about how cool these companies are but I will save it for future.

So what is different this time around? Lately, I realized that it’s been long time I have been working in programming world and big part of my success (or fact that I am still doing programming 😄 ) apart from people I worked with, is the community it has, whether its open source world which is full of free content to learn or communities specific sites dedicated to programming, podcasts, tech blogs etc.

I wanted to write some blog posts for quite some time but I usually don’t post much even on social sites so I always just postponed.

But this time around, Firstly — I got additional vacation time from my company 😃 .

But primarily, in last few years, I got a chance to share knowledge at my Job a lot which encouraged me to share more and more and I loved it. Although, I always thought I don’t have much to share and whenever I thought about a topic worth sharing, I was like ‘Oh it is pretty much obvious that it is gonna work like this or that’ or ‘It is already there on such forum why bother’ or ‘I don’t know this topic well enough’ but now I think it might be helpful for someone who’s just starting, if not bigger audience and moreover, it is said that ‘the more you teach the more you learn’ (although this tutorial is the first time I am trying out .netcore), so I decided let’s give it shot even if it is not that clear or obvious at-least I might learn something new and I am hoping to write more in future.

To Teach Is To Learn Twice Over — Joseph Joubert

Too much talking, Let’s get into the business:

Goals

After following this code tutorial we should:

— be able to use .netcore template to get started with one of the templates out there. we are using webapp1 template with individual account membership.

— be familiar with how to add new classes, models and then scaffold views from those model/classes.

— be familiar with .netcore ef migrations i.e. a migration tool that automatically updates the database schema when your model changes without losing any existing data or other database objects.

— Be familiar with docker’s basic usage

Let’s get started

We are going to build a membership enabled MVC website using .Net core template on Mac (steps could be same for Linux/Unix)

Tools

I am using VSCode as IDE, we can use any text editor since we will be using cli (command line interface) to build the solution and run it.

We will be using MSSql 2017 image in a docker container on MAC.

MSSql on docker is just for development, for production you can use Azure DB hosted on Azure or simply deploy docker in Azure containers (I never tried that — possibly an article for future post).

Implementation

As a pre-requisite we will be setting up SQL server using Docker.

Install docker on your machine if it is not installed.

Once installed let’s test if docker works fine on your machine. To check, open your terminal and run following command

docker — version`

and you should see its version, like this

Docker version 18.09.2, build 6247962

After installing we need to start docker application before moving forward. You can do this by Docker GUI or by starting it from cli. Following command is tested in Mac only

open --background -a Docker

It will take a while to docker to start and be ready to take further commands. Now as we successfully start docker, Let’s get the latest microsoft mssql image into our docker: 2017 sql server

docker pull microsoft/mssql-server-linux:2017-latest

this will pull the latest 2017 image of sql server from docker cloud to your local docker environment.

Let’s create a container and execute the container.

docker run \

-e 'ACCEPT_EULA=Y' \

-e 'MSSQL_SA_PASSWORD=Strong!Passw0rd' \

-p 1401:1433 \

--name sql2017 \

-d microsoft/mssql-server-linux:2017-latest

What we are doing

* run: It creates a container and execute it.

* name: an alias for container. we will use it to start and stop the container

* e: it’s a KEY=VAL pair to set environment variables

* p: It publish container’s port to the host

* d: it runs container in detached mode i.e. in background

* run: It creates a container and execute it. * name: an alias for container. we will use it to start and stop the container * e: it’s a KEY=VAL pair to set environment variables * p: It publish container’s port to the host * d: it runs container in detached mode i.e. in background Screenshot-docker_run_sqlserver2017

Let’s set our docker container to interactive mode so that we can run some bash commands like sqlcmd which are available in our sql2017 container

sudo docker exec -it sql2017 "bash"

Screenshot-docker-exec-bash

then it will ask for your machine password. Type the password and hit enter and now you are in interactive mode. I guess you can ignore ‘sudo’ and then you don’t need to type in password, I just kept it like that because of the screenshot I attached here.

— Now we need to run sql commands. We will use sqlcmd cli. To do that we will create a sql session so that we can fire sql commands

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA

Screenshot-sql-cmd-prompt

Let’s create a new Database called MySampleDB. It won’t run following query immediately to run it you have to type ‘GO’ in next line. Let’s create database and then list all databases exist in current instance of SQL

Create Database MySampleDB

SELECT NAME FROM sys.sysdatabases

GO

It will create the new database named MySampleDB and show you show the list of all the database in DB server

NOTE: you can run any other sql command here.

To end your sqlcmd session, type:

QUIT

To detach the current Docker interactive mode without exiting the shell, use the escape sequence Ctrl-p + Ctrl-q

To stop the container when you are not using it run :

docker stop sql2017

To restart the image using previous container name ‘sql2017’. It will start the container with previous settings.