Profile Picture

Sirwan Afifi

Stories from a web developer.

© 2017. Sirwan Afifi All rights reserved.

SQL Server on Linux in Docker on a Mac

Over the last few months, I have been working on a couple of ASP.NET Core projects, it’s been great experience so far becuase I can easily work on different platforms while working on these projects. I recently bought a MacBook Pro and have decided to do .NET projects on macOS since then.

Fortunately working with projects is not a pain in the neck anymore, with help of Docker you can easily setup up a complex development environment in minutes. It actually simplifies the process of building and shipping, running your apps on differente environments.

The first challenge I faced when using macOS was connecting to and using SQL Server. Thanks to Docker and SQL Server for Linux. It’s an instance of SQL Server as a docker container and it’s super-fast so that it runs in seconds.


Installing Docker for Mac

In order to get Docker to work you will need to install Docker for Mac, you can follow these steps to install it. Once Docker installation is done, it will automatically be started:

You can also run docker --version to verify that Docker is working as expected.


Pulling and running Docker image

Next, you can install and run SQL Server on Linux docker image using following command:

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=Passowrd’ -p 1433:1433 -d microsoft/mssql-server-linux

This command will install and run the SQL Server image. Now SQL Server is running locally in Docker container:

You can use its connection string in your ASP.NET Core application:

public class MvcMovieContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Data Source=localhost;Initial Catalog=MovieDB;User ID=sa;Password=StrongPassword");
    }

    public DbSet<Movie> Movies { get; set; }
}

Now if we run migration, the MovieDB database will be created in the running docker container:

dotnet ef database update

Connecting and quering the databse

There are several ways to connect and query a database running in Docker container, one way is using a command line tool called sqlcmd:

Personally I use mssql extension for Visual Studio Code. becuase it’s really easy to use so you don’t have to write your queries in command line mode. If you have not already installed this extension, all you need to do is press ⌘ + P and type ext install mssql then press enter to install it.

In order to connect and query your database first, you need to write your SQL queries into a SQL file in VS Code then press ⌘ + Shift + P then choose Create Connection Profile, follow the prompts to specify the connection properties for the new connection profile. At the end you should have following settings:

{
    "mssql.connections": [
        {
            "authenticationType": "SqlLogin",
            "server": "localhost",
            "database": "MovieDB",
            "user": "SA",
            "password": "",
            "savePassword": true,
            "profileName": "MovieDB"
        }
    ],
}

As you can see mssql.connections takes an array, it means that you can define as many connection profiles as you want. Once you defined your profile, you can use it to execute your queries then the result will be shown as a seperate window:

As you can see working with SQL Server on macOS is really straightforward. Something like this was not possible in .NET before but nowadays you can develope .NET applications on OS of your choice. that’s why I’m more excited about .NET more than ever.

Working remotely

It has been 8 months and so that I have been working remotely for a startup out there. It’s been my pleasure to work with a team of great and talented members. over the past few months, I have learned a lot both in terms of technical stuff and working as a team member.

For some, this might not seem a big deal. But for me, it’s a pretty big one. So I thought it would be great to share my experiences so far.

New technology, new tools

For this project we are using .NET core and ASP.NET MVC core on top of it. The main benefit of using cutting edge technologies is that you always keep yourself updated, but you might argue that working with new and shiny things is overwhelming because the product is not mature enough to work with. But I believe that’s also another great benefit because when you encounter a bug you easily open a GitHub issue and report it. So I think it’s best of both worlds.

Estimation

At first, I couldn’t deal with an accurate estimation, after a while I figured out that the problem was the task itself, not me, if a task is a big one then estimation most likely will be accurate, so the solution of having an accurate estimation is to break the task into smaller ones.

Documentation

A key point to have a good documentation is that, it needs to be updated after the requirement is changed. In our case, there are too many changes in requirements, this makes new team members confused. In my opinion, there must be a section called changelog in the documentation so that it will be really easy to know what was the previous changes at first place. you might say it’s better to use git log but I think sometimes you don’t need to go through several file changes in git history in order to understand a small change. Another good thing for new members is having mockups. I have been always a big fan of this type development, because as you know a picture worth a thousand words.

Working in different time zones

From my point of view working in different time zones makes remote working a little bit difficult, suppose you are given a task, now you have some questions and no one is online, so in this case, you have to leave your messages on Slack or Jira in order to get your questions answered. this is really big deal for small teams.

Collaboration

Another key point in agile development is collaboration. Working in a team is not just about delivering your product as soon as possible in some sprints. the best part of an agile team is having daily/weekly standup with other members. By other members I mean Mobile developers, Backend developers, Database developers, … I think the standup shouldn’t be held separately. As a web developer, I need to know what’s going on in other parts of the application, because I think it gives developers a really good big picture.

Conclusion

All in all, it has been a great journey for me so far. because working remotely not only improves your technical ability but gives you a cultural perspective.

My first experiences with Linux

Running Window 98 on my PC was my first experience with Windows. in fact, for the last 11 years or so, Windows has been the dominant operating system that I’ve been using. So, up until now, I have been an avid fan of Windows.

Disclaimer: I’m not going to say that I’m not interested in Windows anymore, I just want to explain my experience with Ubuntu

Windows just works fine for the most part. But, as a developer, I have always felt something is missing. For solving this problem, Microsoft has tried its best bringing in tools like PowerShell and Bash to Windows. But again, with all these great features and tools, as a developer, you think something is missing.

Two weeks ago, I decided to give Linux a try, So I chose Ubuntu because it’s user-friendly and its community is huge. Actually, my goal was to do some experiments with new version of .NET called .NET Core. The experience was great. First, I created a very simple console application on Windows and deployed it as a SCD (Self-Contained Deployment):

Then, I copied the publish folder on my USB stick and finally, I could run my .NET Core application without installing the .NET runtime on the target machine:

As a .NET developer I have been waiting voraciously for such opportunity. So, I think it’s the best time to migrate to Linux.

Actually, I was amazed at the first impression because Linux has everything a developer needs out of the box. Python is already installed on the OS. This is great because there are times you want to write simple scripts, scrap a page or something like that.

Other cool features of Linux are commands like sed and grep. These tools are great whether you are a developer or a network administrator. for example, suppose that you have following content within a file named test.json:

{
   "AppName": "Simple App",
   "Website": "sirwan.info",
   "UseSqlite": false,
}

Now you want to replace all occurrence of "UseSqlite": false, to "UseSqlite": true, you can do that simply by typing this command:

cat text.json | sed 's/"UseSqlite": false,/"UseSqlite": true,/'

You can definitely do wide varaity of things using these tools, I just wanted to tell you how much I am excited about these cool stuff.

Development on Ubuntu

The good news is that .NET Core runs on Windows, Mac and Linux. In order to install .NET Core on Ubuntu you just simply need to navigate to dot.net site and download the appropriate version. after installing .NET Core, it gives you a CLI called dotnet so you can simply create, build and publish your applications using this command, for example:

dotnet new
dotnet restore
dotnet run

You can also use the editor of your choice for developing .NET Core application, personally I prefer to use Visual Studio Code or VSCode for short. because it has all great features you need:

VSCode is great, But I think using an IDE like Visual Studio gives you the power you need when are developing applications, Unfortunately, there’s no official version of Visual Studio for Linux at this time. I hope Microsoft release it for Linux users The only reason that I can’t abandon Windows is the lack of Visual Studio So I have to keep using Windows on my main machine.

Conclusion

As I mentioned Visual Studio is the only reason to not completely abandon Windows, But I will use Ubuntu for sure. This was my first attempt to use Ubuntu and I am very happy about the experience, I would like to hear your ideas about your journey.

Happy Coding :))