Can't create db and run scripts in Dockerfile
- Dominant language
- C#
- Stars
- 297
- Forks
- 55
- Avg merge
- 18m
- Merged PRs (30d)
- 1
Description
**Describe the bug**
When trying to create a docker image of SQL server + schema using grate linux tool, it gives this error:
```
2025-11-10 16:46:05.77 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database 'xxx'. [CLIENT: 127.0.0.1]
2025-11-10 16:46:15.79 Logon Error: 18456, Severity: 14, State: 38.
2025-11-10 16:46:15.79 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database 'xxx'. [CLIENT: 127.0.0.1]
```
The key in the error is: Error: 18456, Severity: 14, **State: 38.**
[Couldn't find database requested by user.](https://learn.microsoft.com/en-us/sql/relational-databases/errors-events/mssqlserver-18456-database-engine-error?view=sql-server-ver17#additional-error-information)
The grate command in docker file:
```
RUN /opt/mssql/bin/sqlservr & \
sleep 10; \
for dbdir in $(ls -d /tmp/db/* | sort -r); do \
dbname=$(basename "$dbdir"); \
echo "Using connection string: Server=localhost;User ID=sa;Password=$SA_PASSWORD;Database=$dbname;TrustServerCertificate=True;Encrypt=false"; \
/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "${SA_PASSWORD}" -C -Q "SELECT 1"; \
echo "Connection test completed. Running Grate now."; \
/tmp/grate \
--adminconnectionstring "Server=localhost;User ID=sa;Password=$SA_PASSWORD;Database=master;TrustServerCertificate=True;Encrypt=false" \
--connectionstring "Server=localhost;User ID=sa;Password=$SA_PASSWORD;Database=$dbname;TrustServerCertificate=True;Encrypt=false" \
--files "$dbdir" \
--output "/tmp/rh_output_$dbname" \
--env "DEV" \
--noninteractive \
--schema=RoundhousE; \
done \
&& wait || true
```
Key takeaway:
- Using --adminconnectionstring to master, which successfully created the databases
- Using --connectionstring which **impatiently** tries to run the scripts, but fails with the above error
In same docker file, if I do "wait 30" at the end, and duplicate the code again, it will:
- Not try to re-create
- It will **successfully** run roundhouse scripts to update the database
**To Reproduce**
In Dockerfile, try to **create db** and run scripts
**Expected behavior**
Create DB and run the scripts
**Desktop (please complete the following information):**
- OS: Linux
- Version: 1.8.0
- Package: grate-linux-x64-self-contained-1.8.0.zip
- Base image: mcr.microsoft.com/mssql/server:2019-latest
Contributor guide
Assessment
This issue has not been assessed yet.