Skip to content
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

Allow for running initialization scripts #60

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion linux/preview/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
# Base OS layer: Latest Ubuntu LTS.
FROM ubuntu:16.04

# For initialization scripts.
VOLUME /docker-entrypoint-initdb.d

# Default SQL Server TCP/Port.
EXPOSE 1433

# Copy all SQL Server runtime files from build drop into image.
COPY ./install /

# Add symlink for sqlcmd on PATH
RUN ln -s /opt/mssql-tools/bin/sqlcmd /usr/local/bin/sqlcmd

# Copy initialization scripts.
COPY docker-entrypoint.sh /usr/local/bin/
COPY docker-entrypoint-initdb.sh /usr/local/bin/

# Run SQL Server process.
CMD /opt/mssql/bin/sqlservr.sh
ENTRYPOINT ["docker-entrypoint.sh"]
8 changes: 7 additions & 1 deletion linux/preview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ Starting with the CTP 1.4 (March 17, 2017) release the mssql-tools package inclu
```
docker exec -it <container_id|container_name> /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P <your_password>
```
You can also use the tools in an entrypoint.sh script to do things like create databases or logins, attach databases, import data, or other setup tasks. See this example of [using an entrypoint.sh script to create a database and schema and bcp in some data](https://github.com/twright-msft/mssql-node-docker-demo-app).

~~You can also use the tools in an entrypoint.sh script to do things like create databases or logins, attach databases, import data, or other setup tasks. See this example of [using an entrypoint.sh script to create a database and schema and bcp in some data](https://github.com/twright-msft/mssql-node-docker-demo-app).~~

You can also mount scripts to into the container to do things like create databases or logins, attach databases, import data, or other setup tasks. When a container is started for the first time, it will execute any files with extensions .sh or .sql that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your SQL Server services by mounting scripts into that directory and provide custom images with contributed data.

For example:
> ``docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 -v $PWD/initdb:/docker-entrypoint-initdb.d -d microsoft/mssql-server-linux``

You can connect to the SQL Server instance in the container from outside the container by using various command line and GUI tools on the host or remote computers. See the [Connect and Query](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-connect-and-query-sqlcmd) topic in the SQL Server on Linux documentation.

Expand Down
22 changes: 22 additions & 0 deletions linux/preview/docker-entrypoint-initdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# wait for database to start...
for i in {30..0}; do
if sqlcmd -U SA -P $SA_PASSWORD -Q 'SELECT 1;' &> /dev/null; then
echo "$0: SQL Server started"
break
fi
echo "$0: SQL Server startup in progress..."
sleep 1
done

echo "$0: Initializing database"
for f in /docker-entrypoint-initdb.d/*; do
case "$f" in
*.sh) echo "$0: running $f"; . "$f" ;;
*.sql) echo "$0: running $f"; sqlcmd -U SA -P $SA_PASSWORD -X -i "$f"; echo ;;
*) echo "$0: ignoring $f" ;;
esac
echo
done
echo "$0: SQL Server Database ready"
3 changes: 3 additions & 0 deletions linux/preview/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
echo "$0: Starting SQL Server"
docker-entrypoint-initdb.sh & /opt/mssql/bin/sqlservr.sh