This repo contains a docker-based solution to containerize and run Microsoft SQL with automated creation of schemas and tables, and population of data from CSV files. This helps with creating a predefined copy of an application's database, which can help with local development, end-to-end testing, etc. This is not a tool per se, but rather a template or a solution using existing tools like docker.
- Install Docker Engine
- Fork or clone this repo
- Duplicate and rename
.env.example
file to.env
and set all values. - Create and populate
data
folder.- Create
data/__ddl__
folder and add all DDL statements as.sql
files - Read the section below to understand how to populate this folder.
- Create
- (Optional): Rename
name
inside docker-compose.yml to your project/app name. Default islocalize-mssql
. - Install all dependencies by running
npm install
- To create a fresh copy of all tables and data, run
npm start
. This will initialize the MSSQL server and run all SQL scripts and insert statements. - Once the tables have been populated with the test data, commit the changes to create a new image
docker commit <container-id> <app-name>:<tag>
.
The created image (which runs MSSQL server with preconfigured test data) can now be used across your dev and testing environments! And this solution works easily with CI too!
Command | Description |
---|---|
docker compose --profile init up -d | Initialize MS SQL server, create all tables and schemas, populate tables with data. (This will also keep the server running) |
docker compose stop | Stop the server |
docker compose start | Start the MS SQL server (if initiialization was already done) |
docker commit <container-id> <app-name>:<tag> | Commit the sql server to create an image with the populated data |
- Add all DDL statements inside
data/__ddl__/
. Prefix with a number for ordering.- Example:
01-fileA.sql
will be executed before02-fileB.sql
- Example:
- For populating data, create a subdirectory with the schema name, and a csv file with table name.
- Example: For a table
dbo.table1
, create a subdirectorydbo
(inside the data folder) and a file01-table1.csv
. - The numbering before table name ensures the order in which to execute the files. This helps with foreign key constraints.
- Example: For a table
MSSQL image from Microsoft does not support ARM64 architecture (Apple Silicon devices will be affected). Follow this blog for a workaround. The workaround is to use Rosetta (included in Docker engine) for emulating amd64 images.