docker | sql-server

Create SQL Server Database From a Script in Docker-Compose

Run a SQL Server container with database initialized from a script using docker-compose.

Abhith Rajan
Abhith RajanMay 21, 2019 · 3 min read · Last Updated:

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'
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
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
  • setup.sql

Dockerfile Which basically get the latest image of (linux) and configures the SQL Server container.

4ENV SA_PASSWORD=Your_password123
6COPY ./data /
8ENTRYPOINT [ "/bin/bash", "" ]
9CMD [ "/opt/mssql/bin/sqlservr" ]

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

2set -e
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
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
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
22exec "$@"

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


Abhith Rajan

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

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

Related SnippetsView All

Related ArticlesView All

Related VideosView All

SQL Server 2017 Graph Database

Azure SQL Game Changers for Developers | BDL112

Related StoriesView All

Related Tools & ServicesView All

sequel fumpt

sqlfmt is an online SQL formatter. It is pronounced sequel fumpt. Its purpose is to beautifully format SQL statements.
SmarterASP.NET - Unlimited ASP.NET Web Hosting

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