Recently, I am learning the SQL Server performance tunning and the book I am using is using the AdventureWorks database as the sample database. And I don’t want to install the SQL Server Engine in my Surface, not even the Express version. So I am thinking about using the Docker to host my database.
After some investigations and tries, I figured it out.
Below is the folder structure.
Now, let me go through some steps to build your own AdventureWorks database on your local computer.
Download the DB data file.
Microsoft provided AdventureWorks databases for a long time. The latest one is AdventureWorks2019. Here is the
official document
.
You can choose the bak file you prefer to use.
Prepare the DB restore scripts
1
2
3
4
5
6
7
8
9
10
11
USE[master]GO-- Use this command to restore the original MDF and LDF file.
RESTOREFILELISTONLYFROMDISK="/var/opt/mssql/backup/AdventureWorks2019.bak"-- Use files generated by the last step to create the database.
-- For the file path, you can see the output message from the first command.
RESTOREDATABASEAdventureWorks2019FROMDISK="/var/opt/mssql/backup/AdventureWorks2019.bak"WITHMOVE"AdventureWorks2017"TO"/var/opt/mssql/data/AdventureWorks2019.mdf",MOVE"AdventureWorks2017_log"TO"/var/opt/mssql/data/AdventureWorks2019.ndf"GO
Create docker file for the AdventureWorks DB
Here is the init.sh file which will be used as the COMMAND in the docker compose yaml file.
Since the container which host the SQL Server and the container which will be used to restore the DB will starts simultaneously, restoring the database will be failed until the SQL Server container finishes the initialization.
So I add the retry strategy to the shell file.
Here is the dockerfile content.
1
2
3
4
5
6
7
8
9
FROM mcr.microsoft.com/mssql-tools:latestUSER rootCOPY["Support/db/*", "/app/"]COPY["Source/*", "/var/opt/mssql/backup/"]RUN find /app -type f -exec sed -i "s/\r//"{}\;# Remember to add the permission.RUN chmod -R ugo+x /app/*.sh
Create the docker compose YAML
As you can see, there are two containers aforementioned in the last step. One for the SQL Server itself and another one is for the restoration.