Skip to content

Commit e6d09d2

Browse files
Enable env var for auto creation of MSSQL_DB if var exists and db does not (#17)
1 parent 9c31b83 commit e6d09d2

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

sqlinstance/Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ RUN /bin/bash /tmp/start-sql.sh
4646
FROM dbatools/mssqlbase
4747
COPY --from=builder /var/opt/mssql /var/opt/mssql
4848
COPY --from=builder /opt/mssql-tools/bin /opt/mssql-tools/bin
49+
COPY --from=builder /tmp/post-entrypoint.sh /opt/mssql/bin/post-entrypoint.sh
4950

5051
# make a shared dir with the proper permissions
5152
USER root
5253
RUN mkdir /shared; chown mssql /shared
5354

5455
# run a rootless container
5556
USER mssql
56-
ENTRYPOINT /opt/mssql/bin/sqlservr
57-
# CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
57+
# create a new database if env var is set
58+
ENTRYPOINT /opt/mssql/bin/sqlservr & /opt/mssql/bin/post-entrypoint.sh
5859

5960
# label the container
6061
LABEL org.opencontainers.image.vendor="dbatools"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# if an environment variable for newdb exists, create it
2+
if [ -n "${MSSQL_DB+1}" ]; then
3+
# load up environment variables
4+
export SQLCMDSERVER=localhost
5+
export SQLCMDUSER=sqladmin
6+
export SQLCMDPASSWORD=dbatools.IO
7+
export PATH=$PATH:/opt/mssql-tools/bin
8+
9+
# wait for sql to be ready
10+
for i in {1..30};
11+
do
12+
sqlcmd -S localhost -d master -Q "SELECT @@VERSION"
13+
if [ $? -ne 0 ];then
14+
sleep 2
15+
fi
16+
done
17+
18+
# create the db if it doesn't exist already
19+
sqlcmd -S localhost -d master -Q "IF DB_ID (N'${MSSQL_DB}') IS NULL CREATE DATABASE ${MSSQL_DB};"
20+
fi
21+
22+
# keep it going
23+
sleep infinity

0 commit comments

Comments
 (0)