I have a dotnet build process running in docker based on the microsoft/dotnet-framework:4.7.2-sdk
image.
Restore, build and publish works as expected, but my integration tests require SQL Server to be installed on the machine.
I would like to use a multistage build to run the dotnet test
(or more specifically in my case dotnet xunit
) command within a container based on microsoft/mssql-server-windows-developer:2017-latest
.
But doing that I no longer have access to the dotnet sdk. How can I run dotnet test
from the second stage of my build.
Some like this (not working, the last step fails, because the dotnet command is not recognized):
FROM microsoft/dotnet-framework:4.7.2-sdk AS build
WORKDIR /app
# # Copy csproj and restore as distinct layers
COPY ./*.sln ./NuGet.config ./
COPY ./libs ./libs
COPY ./src ./src
WORKDIR /app/src/Tests/
RUN dotnet build
FROM microsoft/mssql-server-windows-developer:2017-latest
WORKDIR /app/
COPY --from=build /app/src/Tests/ .
RUN dotnet xunit
After several unsuccessful attempts of trying to install the dotnet build tools and prerequisites for running the tests on the 'microsoft/mssql-server-windows-developer' image, I found that installing Sql Server on the `microsoft/dotnet-framework:4.7.2-sdk' image was probably easier. A lot easier actually.
Though I followed this guide https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/install/build-tools-container.md is ran in to all kinds of trouble actually running the tests.
But the other way around works. Build takes some time, but it works. The start of my Dockerfile is:
# The 'docker build' command must be run with '-m 4g' argument for sql server.
FROM microsoft/dotnet-framework:4.7.2-sdk
SHELL [ "powershell.exe", "-Command" ]
RUN curl.exe -L -o sql.box https://go.microsoft.com/fwlink/?linkid=840944
RUN curl.exe -L -o sql.exe https://go.microsoft.com/fwlink/?linkid=840945
RUN Start-Process -Wait -FilePath .\sql.exe -ArgumentList /qs, /x:setup
RUN .\setup\setup.exe /q /ACTION=Install /INSTANCENAME=MSSQLSERVER /FEATURES=SQLEngine /UPDATEENABLED=0 /SQLSVCACCOUNT='NT AUTHORITY\System' /SQLSYSADMINACCOUNTS='BUILTIN\ADMINISTRATORS' /TCPENABLED=1 /NPENABLED=0 /IACCEPTSQLSERVERLICENSETERMS
ENV MSSQL_IS_RUNNING_IN_DOCKER true
The rest is just standard copy/dotnet build/dotnet test stuff.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.