Share via


Creating a Release Build of an ASP.NET Core Project using Docker ASP.NET Core Build Image

Following wiki Creating and Debugging Docker Enabled .NET Core Project in Visual Studio 2017 and in this post wiki see how we can create a release build of an ASP.NET Core Project using official Docker ASP.NET Core Build Image.

Right now we have a Docker support enabled ASP.NET Core MVC Application created (If you don't know how you can read above-mentioned wiki).

Note: we have created the solution using Visual Studio 2017, but you don't need to have Visual Studio 2017 to create this solution. dotnet SDK and Visual Studio Code should be more than enough. For the next steps, we only need dotnet SDK and cmd/PowerShell.

Let's proceed. We have a clean solution, no obj/bin folders present yet.

https://lh3.googleusercontent.com/-1nHnpisz2ZQ/WS7hi-gViQI/AAAAAAAAEgE/sRVVCSpMEpABq1eUaGUobkszimxD8um6QCHM/SNAGHTML9e60569_thumb%255B2%255D?imgmax=800
No obj/bin folders

We have a PowerShell window opened at the root folder level.

PS C:\Users\Jaliya\Desktop\DockerComposeWebApp>

Next to do a Release build what we would do is do a dotnet restore and a publish like below.

# Restore packages
dotnet restore .\DockerComposeWebApp\DockerComposeWebApp.csproj
 
# Publish on Release mode
dotnet publish -c Release .\DockerComposeWebApp\DockerComposeWebApp.csproj

But in that case the build would be done on my local machine, instead, here we want to use a Docker Container to do the building.

Now let’s open up the docker-compose.ci.build.yml in our local folder. Remember we did not write this file ourselves, Visual Studio 2017 created this for us when we have enabled Docker Support for our project (if you want to know how to get these docker-compose.xxxxx.yml files created when using Visual Studio Code, you can read this wiki Running ASP.NET Core MVC Application inside a Docker Linux Container from Windows).

docker-compose.ci.build.yml

version: '2'
services:
  ci-build:
    image: microsoft/aspnetcore-build:1.0-1.1
    volumes:
      - .:/src
    working_dir: /src    
    command: /bin/bash -c "dotnet restore ./DockerComposeWebApp.sln && dotnet publish ./DockerComposeWebApp.sln -c Release -o ./obj/Docker/publish"

Here you can see that we have a service named ci-build which uses microsoft/aspnetcore-build Image and that is the official Image for building ASP.NET Core applications (version will be changed from time to time). Then our current folder is getting mapped to src folder in the ci-build Container (which is not yet created). After Container is created and running, it’s working directory will get changed to src and the given command will get executed. And basically what the command does is a dotnet restore and a dotnet publish (here output path is set to .\obj\Docker\publish).

Now from PowerShell let’s run the following command.

# docker-compose up: Builds, (re)creates, starts, and attaches to containers for a service.
PS C:\Users\Jaliya\Desktop\DockerComposeWebApp> docker-compose -f .\docker-compose.ci.build.yml up
https://lh3.googleusercontent.com/-_zHLJ0LG60k/WS7hoDYIGSI/AAAAAAAAEgM/I8is8915L6wsxiWZU7tPI0fvIPHay218wCHM/image_thumb%255B2%255D?imgmax=800
docker-compose up

(Note: since we already had microsoft/aspnetcore-build Image locally, it didn't get downloaded again. If not, the downloading status of the image will be displayed here.)

Now if we examine the \DockerComposeWebApp project folder, we can see obj/bin folders have been created and if we go to .\obj\Docker\publish folder, build contents are there.

https://lh3.googleusercontent.com/-HheLGbYhbuM/WS7hrAOZn1I/AAAAAAAAEgU/XzqyNHWALg4elzCYOe81fO53pg_yH3mVACHM/image_thumb%255B5%255D?imgmax=800
Published Folder

Now we have a Release build created using a Docker Container (remember we didn’t do the build on our local machine).

For a further step, to make sure all is good, let’s package this content into another Docker Container and see whether it works.

docker-compose.yml

version: '2'
services:
  dockercomposewebapp:
    image: dockercomposewebapp    
    build:
      context: ./DockerComposeWebApp      
      dockerfile: Dockerfile

Dockerfile

FROM microsoft/aspnetcore:1.1
ARG source
WORKDIR /app
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "DockerComposeWebApp.dll"]

docker-compose.override.yml

version: '2'
services:
  dockercomposewebapp:
    environment:
      - ASPNETCORE_ENVIRONMENT=Development    
    ports:
      - "80"
PS C:\Users\Jaliya\Desktop\DockerComposeWebApp> docker-compose -f .\docker-compose.yml -f .\docker-compose.override.yml up
https://lh3.googleusercontent.com/-dp2ppX_6H5w/WS7htqLEgUI/AAAAAAAAEgc/6WcqIIjsfDAteGT7bS8CChKloYdN3I2FQCHM/image_thumb%255B9%255D?imgmax=800
docker-compose up
https://lh3.googleusercontent.com/-mxKnE364uTo/WS7hvUnC7OI/AAAAAAAAEgk/UOR3o1YL0GU_uUT5GB90qjDC5tMCr68SACHM/SNAGHTMLa547c48_thumb%255B2%255D?imgmax=800
docker images & docker ps

:latest Image of a build has been created and a Container is running which got instantiated from that Image.

Now let’s just browse the running port. And yes, it is working.

https://lh3.googleusercontent.com/-_V2p14hV8Y4/WS7hw5vU24I/AAAAAAAAEgs/kQn8m-7KBgAIImWNe_nEY-rP7m79qkZGgCHM/image_thumb%255B15%255D?imgmax=800
Site Running inside a Docker Container

Happy Coding.