ASP.NET Core 2 + React + Docker + ServiceStack
Microsoft recently dropped ASP.NET Core 2, and it’s pretty amazing. One of the first things I wanted to do was get their React template running in Docker (with all their new Docker support). Unfortunately, it’s not available as an option :(.
And so began many nights of research. This post will show my project structure and my Dockerfile setup.
After creating the project, I remember finding the option to “Add Docker Support” later. This caused an issue in the build portion related to Microsoft.Docker.Sdk not being found. Removing the Docker compose project from the .sln file fixed the Microsoft.Docker.Sdk not found issue.
I’m using ServiceStack, so my project structure is something like this:
/src/Project.Web (Actual Web App Host)
/src/Project.ServiceModel (Service Models)
My Dockerfile is placed in the directory above src, with the following contents.
UPDATE: For whatever reason, my original use of the microsoft/aspnetcore-build image did not work (complained about node issues, the above mentioned Microsoft.Docker.Sdk not found error). This might have been a fluke, or due to late night coding.
I’ve updated the below Dockerfile because now all runs just fine with the microsoft/aspnetcore-build image. Microsoft has become obsessed with performance, so I’d much rather use their pre-built “build” image than something I hacked together at 2 A.M.
# Build Image
FROM microsoft/aspnetcore-build AS build-env
COPY src /app
RUN dotnet restore Project.sln
RUN dotnet publish -c Release -o out Project.sln
# Runtime Image
COPY --from=build-env /app/Project.Web/out ./
ENTRYPOINT ["dotnet", "Project.Web.dll"]
That’s pretty much it. I make light of it, but that Dockerfile took me 2–3 nights to figure out (I’m a Docker newbie).
Change directory to the ../src folder (folder above src — where you put the Dockerfile), and run the following commands to build / run:
docker build -t webapp .
docker run -d -p 8000:80 webapp
Browse to localhost:8000 and you’re good to go!
Note that the final, resulting image is a production-ready one (mine reads 304MB).
To the community: Can I reduce the image size somehow? Any Docker pros know of a better way? Can’t get it to work? Leave a message!