Create SQL Server Database From a Script in Docker-Compose

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

commit 51e8824 · authored · 2 min read

Create SQL Server Database From a Script in Docker-Compose

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,

version: '3.4'
services:
other.service:
...
environment:
- 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
depends_on:
- db
db:
image: my/db/image/name
build:
context: .
dockerfile: data/Dockerfile
# ports:
# - "1444:1433"
volumes:
- mssqldata:/var/opt/mssql
volumes:
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.

FROM mcr.microsoft.com/mssql/server
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=Your_password123
COPY ./data /
ENTRYPOINT [ "/bin/bash", "entrypoint.sh" ]
CMD [ "/opt/mssql/bin/sqlservr" ]

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

#!/bin/bash
set -e
if [ "$1" = '/opt/mssql/bin/sqlservr' ]; then
# If this is the container's first run, initialize the application database
if [ ! -f /tmp/app-initialized ]; then
# 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.
function initialize_app_database() {
# 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
sleep 15s
#run the setup script to create the DB and the schema in the DB
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Your_password123 -d master -i setup.sql
# Note that the container has been initialized so future starts won't wipe changes to the data
touch /tmp/app-initialized
}
initialize_app_database &
fi
fi
exec "$@"

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

CREATE DATABASE DB_NAME;
GO
USE DB_NAME;
...

local graph · open full graph

"Create SQL Server Database From a Script in Docker-Compose" is connected to the topics Docker, SQL Server and related to 8 other entries. Docker - SQL Error on ASP.NET Core Alpine Docker - SQL Error on ASP.N… Style Guide - SQL Server Auditing Column Names Style Guide - SQL Server Au… SQL Server - Delete Duplicate Rows SQL Server - Delete Duplica… Dapper - Execute Multiple Stored Procedures Dapper - Execute Multiple S… Microsoft SQL Server Guy Trying Oracle Database Microsoft SQL Server Guy Tr… Filtering, Paging and Sorting in SQL Server 2008 Filtering, Paging and Sorti… Create New Database Level User Create New Database Level U… UTC to UAE Time (Arabian Standard Time) UTC to UAE Time (Arabian St… Docker #docker SQL Server #sql-server Create SQL Server Database From a…

comments

loading discussion…