I'm creating a custom Docker image from the existing mcr.microsoft.com/mssql/server:2017-latest-ubuntu
by adding some databases with Flyway and then creating backups of the databases within the container.
The idea is to start the Docker container and have it import all databases from the backup files it finds in a certain directory.
If my Docker entrypoint just starts sqlservr
and afterwards I exec
the shell scripts that restore the DBs from outside, everything works (except that sometimes SQL Server seems to be up, but really isn't).
According to https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-configure-docker?view=sql-server-2017#customcontainer, I should start my shell scripts before sqlservr
starts, since it's the foreground process which defines whether the container is up or not.
But it's a chicken/egg situation: How can I execute SQL commands within my shell scripts before SQL Server is running?
I'm aware that normally one would do this with a volume, but this is a test database, and I don't care what happens to the data once the test has finished.
Here's my Docker entrypoint script (I have some custom scripts in /opt/mssql-tools/bin/
):
#!/bin/sh
if ! whoami &> /dev/null; then
if [ -w /etc/passwd ]; then
echo "${USER_NAME:-sqlservr}:x:$(id -u):0:${USER_NAME:-sqlservr} user:${HOME}:/sbin/nologin" >> /etc/passwd
fi
fi
/opt/mssql-tools/bin/restore-databases.sh & sqlservr
Thanks for any hints and suggestions what "the Docker way" should be.