Skip to content

Instantly share code, notes, and snippets.

@sdcondon
Created December 20, 2020 23:35
Show Gist options
  • Save sdcondon/1bcc2e4d97903cfff62a0a6827695139 to your computer and use it in GitHub Desktop.
Save sdcondon/1bcc2e4d97903cfff62a0a6827695139 to your computer and use it in GitHub Desktop.
Dockerfile for building and deploying a DACPAC
# This dockerfile is a combination of two things:
# * A dotnet build of a csproj assumed to use the SqlProj SDK found here - https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/
# * Building on a SQL Server image to deploy a DACPAC, adapted from https://www.wintellect.com/devops-sql-server-dacpac-docker/
# First, the dotnet build of the DACPAC. The csproj referenced here should be
# one that uses this SDK: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["TestDb/TestDb.csproj", "TestDb/"]
RUN dotnet restore "TestDb/TestDb.csproj"
COPY . .
WORKDIR "/src/TestDb"
RUN dotnet build "TestDb.csproj" -c Release -o /app/build
# Then we create the final image using instructions adapted from https://www.wintellect.com/devops-sql-server-dacpac-docker/
FROM mcr.microsoft.com/mssql/server:2017-latest AS final
### Install Unzip
RUN apt-get update \
&& apt-get install unzip -y
### Install SQLPackage for Linux and make it executable
RUN wget -progress=bar:force -q -O sqlpackage.zip https://go.microsoft.com/fwlink/?linkid=873926 \
&& unzip -qq sqlpackage.zip -d /opt/sqlpackage \
&& chmod +x /opt/sqlpackage/sqlpackage
### Add the DACPAC to the image
COPY --from=build /app/build/TestDb.dacpac /tmp/db.dacpac
### Configure external build arguments to allow configurability.
ARG DBNAME
ARG SAPASSWORD
### Configure the required environmental variables
ENV ACCEPT_EULA=Y
ENV SA_PASSWORD=$SAPASSWORD
### Launch SQL Server, confirm startup is complete, deploy the DACPAC, then delete the DACPAC and terminate SQL Server.
### See https://stackoverflow.com/a/51589787/488695
RUN ( /opt/mssql/bin/sqlservr & ) | grep -q "Service Broker manager has started" \
&& /opt/sqlpackage/sqlpackage /a:Publish /tsn:. /tdn:${DBNAME} /tu:sa /tp:$SA_PASSWORD /sf:/tmp/db.dacpac \
&& rm /tmp/db.dacpac \
&& pkill sqlservr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment