docker | sql-server

Create SQL Server Database From a Script in Docker-Compose

by Abhith RajanMay 21, 2019 · 3 min read · Last Updated:
Share this

In one of our project which using Microsoft Orleans, We used ADO.NET clustering with SQL Server. When we tried to containerize the application suite, we need a SQL Server instance in a container (for development environment) with a database initialized with some tables and all.

Note: The below setup is not recommended for a PRODUCTION environment.

For that solution, our docker-compose.yml looks like below,

1version: '3.4'
2
3services:
4 other.service:
5 ...
6 environment:
7 - ConnectionString=Data Source=CONTAINER_NAME_OF_db;Initial Catalog=DB_NAME;User Id=sa;Password=Your_password123;Pooling=False;Max Pool Size=200;MultipleActiveResultSets=True
8 depends_on:
9 - db
10 db:
11 image: my/db/image/name
12 build:
13 context: .
14 dockerfile: data/Dockerfile
15 # ports:
16 # - "1444:1433"
17 volumes:
18 - mssqldata:/var/opt/mssql
19
20volumes:
21 mssqldata:

In the above, the other.service can be anything (WEB App/Console App etc) that requires the sql server db instance. We marked the dependency between these services properly using depends_on in the docker-compose.yml.

The volumes is specified in the docker-compose.yml in-order to persist the SQL Server data.

In the same directory where docker-compose.yml is, there is a data folder which contains following,

  • Dockerfile
  • entrypoint.sh
  • setup.sql

Dockerfile Which basically get the latest image of mcr.microsoft.com/mssql/server (linux) and configures the SQL Server container.

1FROM mcr.microsoft.com/mssql/server
2
3ENV ACCEPT_EULA=Y
4ENV SA_PASSWORD=Your_password123
5
6COPY ./data /
7
8ENTRYPOINT [ "/bin/bash", "entrypoint.sh" ]
9CMD [ "/opt/mssql/bin/sqlservr" ]

Entry point in the Dockerfile is pointed to a custom shell entrypoint.sh which is,

1#!/bin/bash
2set -e
3
4if [ "$1" = '/opt/mssql/bin/sqlservr' ]; then
5 # If this is the container's first run, initialize the application database
6 if [ ! -f /tmp/app-initialized ]; then
7 # Initialize the application database asynchronously in a background process. This allows a) the SQL Server process to be the main process in the container, which allows graceful shutdown and other goodies, and b) us to only start the SQL Server process once, as opposed to starting, stopping, then starting it again.
8 function initialize_app_database() {
9 # Wait a bit for SQL Server to start. SQL Server's process doesn't provide a clever way to check if it's up or not, and it needs to be up before we can import the application database
10 sleep 15s
11
12 #run the setup script to create the DB and the schema in the DB
13 /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Your_password123 -d master -i setup.sql
14
15 # Note that the container has been initialized so future starts won't wipe changes to the data
16 touch /tmp/app-initialized
17 }
18 initialize_app_database &
19 fi
20fi
21

And the setup.sql contains all the SQL statements to generate the DB and all. An example given below,

1CREATE DATABASE DB_NAME;
2GO
3USE DB_NAME;
4
5...

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Abhith Rajan

Written by Abhith Rajan Buy me a coffee
Abhith Rajan is an aspiring software engineer with more than 6 years of experience and proven successful track record of delivering technology-based products and services.

Related Posts

Related Services

SmarterASP.net - Unlimited ASP.NET Web Hosting

ASP.NET Hosting by SmarterASP.net. Unlimited ASP.NET Hosting Plans Starting at $2.95 a month.