Skip to content

Can't create db and run scripts in Dockerfile #681

@Edgaras91

Description

@Edgaras91

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.
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions