This image is based on the official documentation from Microsoft: https://github.com/microsoft/mssql-docker/tree/master/linux/preview/examples/mssql-customize
It has been adapted to support Sql Server 2022 and provide an initialization framework for a first database and an associated user.
The goal of this image is to simplify the usage of Sql Server docker images in development environment.
Starting a Sql Server instance is simple:
$ docker run --name mssql -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -p 1433:1433 -d mssql-initialized:latest
... where mssql
is the name you want to assign to your container, <YourStrong!Passw0rd>
is the password to be set for the System Administrator (SA) user and latest
is the tag specifying the version you want. See the list above for relevant tags.
The additional parameters (MSSQL_DATABASE
, MSSQL_USER
and MSSQL_PASSWORD
) allow for the preparation of a database right after the start of the server:
$ docker run --name mssql -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -e "MSSQL_DATABASE=HelloWorld" -e "MSSQL_USER=hello" -e "MSSQL_PASSWORD=World!Passw0rd" -p 1433:1433 -d mssql-initialized:latest
Source and full list: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-configure-environment-variables
Required.
Set the ACCEPT_EULA variable to any value to confirm your acceptance of the End-User Licensing Agreement. Required setting for the SQL Server image.
Required.
Configure the SA user password.
Here are the environment variables that have been added to the Sql Server docker image to offer pre-initialization features.
Optional.
Define the name of the database to be created after the initialization of the server.
Required if MSSQL_PASSWORD
is defined. Ignored if MSSQL_DATABASE
is not defined.
Define the username of the account created during initialization. This account will have the db_owner
right on the initial database.
Required if MSSQL_USER
is defined. Ignored if MSSQL_DATABASE
is not defined.
Define the password of the account created during initialization.
The initialization process supports two custom scripts that can be provided using volumes.
They are both stored in the /usr/config/
folder.
This script will be executed if MSSQL_DATABASE
, MSSQL_USER
and MSSQL_PASSWORD
are defined. It will be executed on the created database with the dedicated user and can be used to configure the database itself.
$ docker run --name mssql -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -e "MSSQL_DATABASE=HelloWorld" -e "MSSQL_USER=hello" -e "MSSQL_PASSWORD=World!Passw0rd" -v local-init.sql:/usr/config/init.sql:ro -p 1433:1433 -d mssql-initialized:latest
This script will be executed if MSSQL_DATABASE
is defined. It will be executed on the master database with the SA account and can be used to configure the server itself.
$ docker run --name mssql -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=<YourStrong!Passw0rd>" -e "MSSQL_DATABASE=HelloWorld" -e "MSSQL_USER=hello" -e "MSSQL_PASSWORD=World!Passw0rd" -v local-init-sa.sql:/usr/config/init-sa.sql:ro -p 1433:1433 -d mssql-initialized:latest
flowchart TD
S[start] --> A
A{MSSQL_DATABASE is defined}
A -->|false| B[No scripts executed]
A -->|true| C[Create the database]
B --> End
C --> D{MSSQL_USER is defined}
D -->|true| E[Create the user]
D -->|false| G
E --> F[Execute init.sql]
F --> G[Execute init-sa.sql]
G --> End[end]