Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

How to build a Sample Album Viewer application using Windows containers

3 min read

This tutorial walks you through building and running the sample Album Viewer application with Windows containers. The Album Viewer app is an ASP.NET Core application, maintained by Microsoft MVP Rick Strahl. There is a fork at dockersamples/dotnet-album-viewer which uses Docker Windows containers.

Docker isn’t just for new apps built with .NET Core. You can run full .NET Framework apps in Docker Windows containers. Check out the labs for Modernizing .NET apps with Docker.

Using Docker Compose on Windows

Docker Compose is a great way develop distributed applications, where all the components run in their own containers. In this lab you’ll use Docker Compose to run SQL Server in a container, as the data store for an ASP.NET Core web application running in another container.

Docker Compose is installed with Docker Desktop on Windows 10. If you’ve installed the Docker Engine as a Windows Service instead, you can download the compose command line using PowerShell:

Invoke-WebRequest https://github.com/docker/compose/releases/download/1.23.2/docker-compose-Windows-x86_64.exe -UseBasicParsing -OutFile $env:ProgramFiles\docker\docker-compose.exe

To run the sample application in multiple Docker Windows containers, start by cloning the GithUb dockersamples/dotnet-album-viewer repository:

git clone https://github.com/dockersamples/dotnet-album-viewer.git

The Dockerfile for the application uses Docker multi-stage builds, where the app is compiled inside a container and then packaged into a Docker image. That means you don’t need .NET Core installed on your machine to build and run the app from source:

cd dotnet-album-viewer
docker-compose build

You’ll see a lot of output here. Docker will pull the .NET Core images if you don’t already have them, then it will run dotnet restore and dotnet build inside a container. You will see the usual NuGet and MSBuild output – you don’t need to have the .NET Core SDK installed, because it is part of the Docker image.

When the build completes, run the app with:

docker-compose up -d

Docker starts a database container using TiDB, which is a modern distributed database system compatible with MySQL. When the database is running it starts the application container. The database and application containers are in the same Docker network, so they can reach each other.

The container for the web application publishes port 80 on the host, so you can browse to your http://localhost and check out the working site.

Organizing Distributed Solutions with Docker Compose

Take a closer look at the docker-compose.yml file. There are two services defined, which are the different components of the app that will run in Docker containers. The first is the MySQL-compatible database:

    image: dockersamples/tidb:nanoserver-1809
      - "3306:4000"

The second service is the ASP.NET Core web application, which uses the custom image you built at the start of the lab:

    image: dockersamples/dotnet-album-viewer
      context: .
      dockerfile: docker/app/Dockerfile
      - "80:80"
      - "Data:Provider=MySQL"
      - "Data:ConnectionString=Server=db;Port=4000;Database=AlbumViewer;User=root;SslMode=None"      
      - db

The build details capture the path to the Dockerfile. The environment variables are used to configure the app – they override the settings in appsettings.json. This configuration uses MySQL rather than the default SQLite database, and sets the connection string to use the TiDB database container.

The database container has a built-in user called root with no password, and this is the account used by the web application in the connection string.

The app definition also captures the dependency on the database server, and publishes port 80 so any traffic coming into the host gets directed by Docker into the container.

Packaging ASP.NET Core apps in Docker

How can you compile and run this app without .NET Core installed? Docker compiles and runs the app using containers. The tasks are in the Dockerfile, which captures the app dependencies so the only pre-requisite you need is Docker. The first stage in the Dockerfile publishes the app:

FROM microsoft/dotnet:2.1-sdk-nanoserver-1809 AS builder

WORKDIR /album-viewer
COPY AlbumViewerNetCore.sln .
COPY src/AlbumViewerNetCore/AlbumViewerNetCore.csproj src/AlbumViewerNetCore/AlbumViewerNetCore.csproj
COPY src/AlbumViewerBusiness/AlbumViewerBusiness.csproj src/AlbumViewerBusiness/AlbumViewerBusiness.csproj
COPY src/Westwind.Utilities/Westwind.Utilities.csproj src/Westwind.Utilities/Westwind.Utilities.csproj
RUN dotnet restore src/AlbumViewerNetCore/AlbumViewerNetCore.csproj

COPY src src
RUN dotnet publish .\src\AlbumViewerNetCore\AlbumViewerNetCore.csproj

This uses Microsoft’s .NET Core Docker image as the base in the FROM instruction. It uses a specific version of the image, with the .NET Core 2.1 SDK installed, running on the 1809 release of Microsoft Nano Server. Then the COPY instructions copy the project files and solution files into the image, and the RUN instruction executes dotnet restore to restore packages.

Docker caches parts of the image as it build them, and this Dockerfile separates out the restore part to take advantage of that. Unless the solution or project files change, Docker will re-use the image layer with the dependencies already restored, saving time on the dotnet restore operation.

After the restore, the rest of the source code is copied into the image and Docker runs dotnet publish to compile and publish the app.

The final stage in the Dockerfile packages the published application:

# app image
FROM microsoft/dotnet:2.1-aspnetcore-runtime-nanoserver-1809

WORKDIR /album-viewer
COPY --from=builder /album-viewer/src/AlbumViewerNetCore/bin/Debug/netcoreapp2.0/publish/ .
CMD ["dotnet", "AlbumViewerNetCore.dll"]

This uses a different variant of the dotnet base image, which is optimized for running ASP.NET Core apps. It has the .NET Core runtime, but not the SDK, and the ASP.NET core packages are already installed. The COPY instruction copies the published .NET Core app from the previous stage in the Dockerfile (called builder), and the CMD instruction tells Docker how to start the app.

The Dockerfile syntax is simple. You only need to learn a handful of instructions to build production-grade Docker images. Inside the Dockerfile you can use PowerShell to deploy MSIs, update Windows Registry settings, set file permissions and do anything else you need.

Next Steps

This lab walked you through building and running a simple .NET Core web application using Docker Windows containers. Take a look at some more Windows container labs to see how your existing apps can be moved into Docker:

This post was originally published under DockerLabs.

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

54 Replies to “How to build a Sample Album Viewer application using…”

  1. Greetings from Ohio! I’m bored at work so I decided to check out your website on my iphone during lunch break. I enjoy the information you present here and can’t wait to take a look when I get home. I’m shocked at how quick your blog loaded on my phone .. I’m not even using WIFI, just 3G .. Anyhow, great blog!

  2. Hi, I think your site might be having browser compatibility issues. When I look at your website in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, wonderful blog!

  3. Thanks for every other great post. The place else may just anybody get that type of info in such a perfect approach of writing? I have a presentation next week, and I’m on the search for such information.

  4. It’s a pity you don’t have a donate button! I’d definitely donate to this superb blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to fresh updates and will talk about this website with my Facebook group. Talk soon!

  5. Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way I’ll be subscribing to your feeds and even I achievement you access consistently fast.

  6. I got what you intend, regards for posting.Woh I am glad to find this website through google. “Since the Exodus, freedom has always spoken with a Hebrew accent.” by Heinrich Heine.

  7. I would like to thnkx for the efforts you have put in writing this blog. I am hoping the same high-grade blog post from you in the upcoming as well. In fact your creative writing abilities has inspired me to get my own blog now. Really the blogging is spreading its wings quickly. Your write up is a good example of it.

  8. This web site is mostly a walk-via for the entire information you wanted about this and didn’t know who to ask. Glimpse right here, and also you’ll undoubtedly discover it.

  9. I was curious if you ever considered changing the layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or two pictures. Maybe you could space it out better?

  10. I have been exploring for a bit for any high-quality articles or weblog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this web site. Reading this info So i¦m happy to express that I have a very good uncanny feeling I came upon just what I needed. I most indubitably will make sure to do not disregard this web site and provides it a glance on a continuing basis.

  11. I’m still learning from you, while I’m trying to achieve my goals. I certainly liked reading all that is posted on your site.Keep the aarticles coming. I liked it!

  12. I am writing to make you know of the exceptional encounter my princess gained viewing your web site. She noticed so many details, not to mention what it’s like to have a wonderful teaching style to get other people effortlessly grasp a number of multifaceted issues. You actually did more than our expected results. Thanks for offering such beneficial, trusted, explanatory and in addition fun tips about your topic to Jane.

  13. I appreciate, cause I found just what I was looking for. You have ended my four day long hunt! God Bless you man. Have a nice day. Bye

  14. Good ?V I should definitely pronounce, impressed with your web site. I had no trouble navigating through all tabs as well as related information ended up being truly simple to do to access. I recently found what I hoped for before you know it at all. Reasonably unusual. Is likely to appreciate it for those who add forums or anything, web site theme . a tones way for your client to communicate. Nice task..

  15. Thank you for any other informative site. The place else may just I am getting that kind of info written in such an ideal way? I have a venture that I’m just now operating on, and I’ve been at the look out for such information.

  16. After study a few of the blog posts on your web site now, and I truly like your manner of blogging. I bookmarked it to my bookmark web site list and will probably be checking back soon. Pls check out my website as well and let me know what you think.

  17. Hi! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My weblog looks weird when browsing from my apple iphone. I’m trying to find a theme or plugin that might be able to fix this issue. If you have any recommendations, please share. Appreciate it!

  18. whoah this blog is magnificent i love reading your posts. Keep up the good work! You know, lots of people are hunting around for this info, you could help them greatly.

  19. Thanks for sharing excellent informations. Your website is very cool. I am impressed by the details that you have on this site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found simply the information I already searched all over the place and simply could not come across. What a great web-site.

  20. I would like to thank you for the efforts you’ve put in writing this website. I’m hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing skills has inspired me to get my own website now. Actually the blogging is spreading its wings rapidly. Your write up is a good example of it.

  21. Howdy would you mind sharing which blog platform you’re using? I’m going to start my own blog in the near future but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I’m looking for something unique. P.S Sorry for being off-topic but I had to ask!

  22. What i don’t realize is in fact how you’re no longer really a lot more smartly-favored than you might be now. You are so intelligent. You know thus significantly on the subject of this subject, produced me individually believe it from a lot of various angles. Its like men and women aren’t involved except it is something to accomplish with Lady gaga! Your own stuffs excellent. Always handle it up!

  23. Hello! I could have sworn I’ve been to this blog before but after browsing through some of the post I realized it’s new to me. Anyways, I’m definitely happy I found it and I’ll be book-marking and checking back frequently!

  24. What i do not understood is actually how you’re not really much more well-liked than you may be right now. You’re so intelligent. You realize thus considerably relating to this subject, made me personally consider it from numerous varied angles. Its like women and men aren’t fascinated unless it is one thing to accomplish with Lady gaga! Your own stuffs great. Always maintain it up!

  25. Its such as you read my thoughts! You appear to know so much approximately this, such as you wrote the e-book in it or something. I believe that you just can do with some p.c. to power the message home a little bit, however instead of that, this is excellent blog. An excellent read. I will certainly be back.

Leave a Reply

Your email address will not be published.

© Copyright Collabnix Inc

Built for Collabnix Community, by Community