Create and Setup MS SQL Server Database docker containers

Aleksander Parkhomenko
MS SQL Server containers
2 min readNov 10, 2020

--

Few months ago, I’ve designed and implemented mid size app with Angular10 as a Frontend, .NET Core 3.1 as a Backend and MS SQL Server 2017 under-hood. Rest API and Angular SPA are hosted independently on IIS 10.0.

One of the organizational tasks for development team was to setup the same development environment for all remote developers. Often development teams share one development database. Due the security policy of the company, we haven’t had access to test environment (only development), moreover, we should start development before such access had been granted.

Naturally, for us here was to setup and run MS SQL Server container locally on developers’ machines. From perspective of the team it is important, that such database (on-cite company database) is created with the initial snapshot: schemes, users, data. Having SQL script for it we’ve arranged it in this way: in docker-compose during creating database container, database and all needed initial data is created:

were docker image entrypoint looks like:

#start SQL Server, start the script to create/setup the DB 
/db-init.sh & /opt/mssql/bin/sqlservr

30 s sleep is added to allow MS SQL Server to start and execute SQL script:

In the SQL scripts we create database, users and default schema:

In this way during Docker container start we’ve created database, users and default schema. In such scripts, you can perform as much operations as you need. The next steps is database deployment on the corresponding environments. For this purpose, there are a number of approaches used: starting from writing pure SQL scripts to database migrations solutions, like Entity Framework migrations. Perhaps it is a matter of habit, but we’ve used FluentMigrator to create schema and data load for this purpose.

See here for more details.

The full code example can be found on Github.

Photo by Jonathan Kemper on Unsplash

--

--

Aleksander Parkhomenko
MS SQL Server containers

Software architect with a wide tech stack interests from database to gui, from monolith to cloud native.