I am working on a MacBook Pro with M1 CPU so I can’t use the "normal" mssql docker image. I am using azure-sql-edge that doesn’t have sqlcmd to initialize the database (create schema, database, login).
I have created a sql script that I would like to run once the container starts but I can’t find any alternative to sqlcmd.
Is there any other way to do it?
3
Answers
Since I am starting a new project I looked into this issue again and found a good solution for me.
I found go-sqlcmd, a new implementation of sqlcmd using golang and it's compatible with M1 chips.
So I am running
azure-sql-edge
as before using docker compose:When the database container is up and in idle I run this bash script (in my case I am reading the environmnet variables from a .NET
appsettings.json
file):I had to split the database and schema creation in a script, then I create the user and assign it to the database.
The sql scripts,
init-db.sql
:init-user.sql
:I had same issue, I used mssql-tools docker image from Microsoft registry.
Sample docker-compose:
To use this docker-compose you need to have a shell script named
run-initialization.sh
with execute rights insidemssql_scripts
folder.The
run-initialization.sh
script waits for database to start up and then execute sql commands:/opt/mssql-tools/bin/sqlcmd -S mssql -U SA -P SA_Passw0rd -d master -Q "SELECT version()"
or if you want to execute from
test.sql
file:/opt/mssql-tools/bin/sqlcmd -S mssql -U SA -P SA_Passw0rd -d master -i /opt/mssql_scripts/test.sql
The solution above worked for me using Mac M1 chip, don’t need to create a shell script can run the commands direct.