-
Notifications
You must be signed in to change notification settings - Fork 766
Allow pointing to and executing a SQL script on run #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
That would be awesome, I can already see a great use-case for this image for spinning up dev databases. I hacked in that functionality with a modified start.ps1 script that I'd open a PR for but sounds like this repo isn't used to generate the image. |
Right, we don't build out of this repo. Feel free to share what you would do by creating a new Dockerfile and entrypoint.sh on your github and shoot us a link to that and the image on Docker Hub. Here is an implementations that I did last week for a demo that I did in a presentation at a conference. |
Here is an example of executing a SQL script at run time. That's really a lot more complicated than it needs to be though. |
FYI - We released CTP 1.4 today. This release of the mssql-server-linux image now includes the mssql-tools package (sqlcmd and bcp) in it. Executing sqlcmd as part of the entrypoint.sh script can be used for this kind of scenario for now. Since this is such a commonplace requirement we want to make it easier in the future, but sqlcmd will provide a reasonable option until then. |
I created a pull request based on the example in the demo mentioned above, implemented in a similar fashion as the mysql image. The genschsa/mssql-server-linux image on Docker Hub is an extension of the official image using this technique. Hope this is useful and considered for inclusion. |
As a workaround, maybe this can be helpfull:
|
Here's another workaround, this one will execute all the SQL files in a mounted folder:
|
@rmoff - This is a tidy solution. Thanks for sharing it. Hopefully, we'll get something like this built into the SQL Server engine and configurable via an env variable (MSSQL_STARTUP_SCRIPTS_DIR or something) in a future release, but for now, this is a good way to handle it. |
thanks @twright-msft. I enhanced it a bit further with trying to detect if the engine has started or not:
I'd be interested to know if this is a valid technique or not :) |
@rmoff - this is great, and I think we can take it one step further.
if 1, all databases are online |
Here's a healthcheck that I wrote that waits for dbs to come online, might be useful. /opt/mssql-tools/bin/sqlcmd -H localhost -U sa -P "$MSSQL_SA_PASSWORD" -l 1 -t 1 -Q "select name from sys.databases where state_desc != 'ONLINE'" | grep --quiet '0 rows affected' || exit 1 Save that as
|
@twright-msft @rmoff instead of the sleep infinity at the end, you can capture the pid and use wait.
|
Do we have any update on this issue. |
I get the following error when I attempt to do use the above example. Error: Microsoft ODBC Driver 17 for SQL Server : Driver's SQLAllocHandle on SQL_HANDLE_HENV failed Has anyone solved this massive bug in this image? This is issue is better described here - #431 |
Add my voice to this suggestion. The way MySQL does it is, any .sql scripts you mount into /docker-entrypoint-initdb.d/ get run in the order they are mounted. It's really nice way to seed test data in my dev docker compose. |
This is also how Postgresql handles it as well. I was sort of expecting this same convention, but shocked that I also had to control the execution of I had to update this to get this to work in my case. There were a couple
|
I struggle a bit to understand why entrypoint.d isn't just a part of the image - it's so standard in the docker community. Why force everyone to make odd solutions when it would be so easy to do a standard implementation for this and not having to handle issues for years about it... |
That would just make too much sense, apparently. This thread has been open since 2017. |
Also note that a lot of other databases already offer this option in their Docker images and tools like https://www.testcontainers.org/ then even provide a standardized API to use this feature. |
One issue with the suggestions here is that the container does not shut down cleanly. Since sql server is not running in PID 1, it does not get the SIGTERM signal, and when you try to shut down the container, it times out and kills the container. One solution is to trap SIGTERM and forward it on to the sql server process:
|
do you know any solution for .NET/C# environment? |
This would be super useful! There is a solution provided for Linux but does one exists for Windows? |
needs to be pid=$$! |
This would still be a beneficial addition 🙏🏼 |
Thank you @tracker1, I also have some advanced containers and can do it on my own but I am aiming for official containers from Microsoft to do this instead of pointing people to community containers. |
very clean and thorough. thank you for sharing |
@tracker1 Tip: rather than using and depending on an external tool like |
And for a solution to have a generic |
@rmoff and all others, I landed up in a situation where I have used the above logic to run the sql scripts (it creates a DB and tables in it), but now whenever I run the container it runs and then I get errors like |
@gkhedekar5758 you can make the scripts idempotent using the sql server equivalent of |
Any updates / recommendations for this? |
For scenarios where you might want to create logins, run some test/initial data population script, etc. it would be nice to be able to point to a sql script file and have that be executed by the entrypoint script after the sqlservr process is started.
The text was updated successfully, but these errors were encountered: