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,

version: '3.4'

      - 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
      - db
    image: my/db/image/name
      context: .
      dockerfile: data/Dockerfile
    # ports:
    #   - "1444:1433"
     - mssqldata:/var/opt/mssql


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.


ENV SA_PASSWORD=Your_password123

COPY ./data /

ENTRYPOINT [ "/bin/bash", "" ]
CMD [ "/opt/mssql/bin/sqlservr" ]

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

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 &

exec "$@"

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



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

Abhith Rajan

Written byAbhith Rajan
Abhith Rajan is a software engineer by day and a full-stack developer by night. He's coding for almost a decade now. He codes 🧑‍💻, write ✍️, learn 📖 and advocate 👍.

Is this page helpful?

Related SnippetsView All

Related ArticlesView All

Related VideosView All

High Availability and SLA for Azure SQL Managed Instance

Using SQL Data Sync for Bidirectional Data Replication in SQL Server & Azure SQL DBs | Data Exposed

Build fast, scalable data system on Azure SQL Database Hyperscale | Clearent

Related Tools & ServicesView All


A process for automating Docker container base image updates.

sequel fumpt

sqlfmt is an online SQL formatter. It is pronounced sequel fumpt. Its purpose is to beautifully format SQL statements.