%3CLINGO-SUB%20id%3D%22lingo-sub-317567%22%20slang%3D%22en-US%22%3EFirst%20steps%20with%20Docker%20-%20%22Dockerizing%22%20your%20first%20application%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-317567%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3EFirst%20published%20on%20MSDN%20on%20Sep%2011%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2018%2F09%2F10%2Ffirst-steps-with-docker-introduction%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20In%20the%20previous%20post%20%3C%2FA%3E%20we%20have%20learned%20what%20Docker%20is%20and%20which%20are%20the%20benefits%20of%20using%20it%20to%20modernizing%20your%20services%20or%20web%20applications.%20However%2C%20most%20of%20the%20content%20was%20theoretical.%20We%20started%20to%20use%20the%20technology%2C%20but%20just%20to%20see%20some%20basic%20scenario%2C%20like%20running%20an%20application%20or%20a%20web%20server%20inside%20a%20container.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20In%20this%20post%20we're%20going%20to%20get%20our%20hands%20dirty%20and%20we'll%20learn%20how%20to%20take%20an%20application%20we%20have%20developed%20and%20to%20store%20it%20inside%20a%20container.%20%3CBR%20%2F%3E%3CH3%20id%3D%22lets-start-with-a-web-application%22%20id%3D%22toc-hId-1535866627%22%20id%3D%22toc-hId-1563689469%22%3ELet's%20start%20with%20a%20web%20application%3C%2FH3%3E%3CBR%20%2F%3E%20As%20first%20step%2C%20we're%20going%20to%20host%20a%20web%20application%20inside%20our%20container.%20We're%20going%20to%20use%20.NET%20Core%20as%20a%20framework%2C%20since%20it%20fits%20perfectly%20into%20the%20container%20world%3A%20it's%20fast%2C%20it's%20cross-platform%20and%20it%20can%20scale%20very%20well.%20%3CBR%20%2F%3E%20Let's%20move%20out%20again%20from%20our%20comfort%20zone%20and%2C%20instead%20of%20using%20Visual%20Studio%2C%20let's%20use%20Visual%20Studio%20Code%20and%20the%20.NET%20Core%20CLI%20to%20create%20our%20web%20application.%20%3CBR%20%2F%3E%20The%20main%20advantages%20of%20using%20this%20approach%20are%20that%3A%20%3CBR%20%2F%3E%3CUL%3E%3CBR%20%2F%3E%3CLI%3EBoth%20the%20CLI%20and%20Visual%20Studio%20Code%20are%20cross-platform.%20You%20can%20follow%20this%20tutorial%20no%20matter%20which%20is%20the%20operating%20system%20you're%20using.%3C%2FLI%3E%3CBR%20%2F%3E%3CLI%3EVisual%20Studio%20Code%20offers%20a%20really%20good%20Docker%20extension%2C%20which%20adds%20Intellisense%20and%20a%20user%20interface%20to%20control%20your%20Docker%20containers%20and%20images%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FUL%3E%3CBR%20%2F%3E%20After%20you%20have%20downloaded%20and%20installed%20Visual%20Studio%20Code%20%3CA%20href%3D%22https%3A%2F%2Fcode.visualstudio.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20from%20the%20official%20website%20%3C%2FA%3E%20%2C%20make%20sure%20to%20install%20the%20following%20extensions%20from%20the%20Marketplace%3A%20%3CA%20href%3D%22https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3Dms-vscode.csharp%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20C%23%20%3C%2FA%3E%20%2C%20in%20order%20to%20get%20C%23%20editing%20and%20debugging%20support%2C%20and%20%3CA%20href%3D%22https%3A%2F%2Fmarketplace.visualstudio.com%2Fitems%3FitemName%3DPeterJausovec.vscode-docker%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Docker%20%3C%2FA%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20The%20last%20piece%20of%20the%20puzzle%20you%20need%20is%20the%20.NET%20Core%20SDK.%20You%20might%20have%20it%20already%20installed%20if%20you%20have%20enabled%20web%20development%20in%20Visual%20Studio%202017.%20Otherwise%2C%20you%20can%20get%20it%20%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fnet%2Fdownload%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20from%20the%20official%20website%20%3C%2FA%3E%20.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20Now%20create%20a%20new%20folder%20on%20your%20PC%2C%20which%20will%20going%20to%20contain%20our%20web%20application.%20Then%20open%20Visual%20Studio%2C%20choose%20%3CSTRONG%3E%20File%20-%26gt%3B%20Open%20Folder%20%3C%2FSTRONG%3E%20and%20select%20the%20folder%20you%20have%20just%20created.%20The%20editor%20will%20be%20opened%20using%20your%20folder%20as%20a%20project.%20However%2C%20for%20the%20moment%20we%20don't%20have%20anything%20to%20work%20with%2C%20since%20the%20folder%20is%20empty.%20%3CBR%20%2F%3E%20Let's%20create%20a%20web%20application.%20We're%20going%20to%20use%20the%20default%20template%20available%20in%20the%20.NET%20Core%20SDK%2C%20which%20is%20based%20on%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fweb-pages%2Foverview%2Fgetting-started%2Fintroducing-razor-syntax-c%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%20Razor%20%3C%2FA%3E%20%2C%20a%20programming%20syntax%20to%20build%20dynamic%20web%20applications%20based%20on%20ASP.NET.%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%20To%20create%20the%20base%20project%20we're%20going%20to%20use%20the%20CLI.%20The%20easiest%20way%20to%20access%20it%20is%20to%20choose%20%3CSTRONG%3E%20Terminal%20-%26gt%3B%20New%20Terminal%20%3C%2FSTRONG%3E%20in%20Visual%20Studio%20Code.%20A%20new%20PowerShell%20prompt%20will%20be%20opened%20directly%20inside%20the%20editor%2C%20already%20pointing%20to%20the%20folder%20you%20have%20just%20created.%20To%20create%20the%20project%20just%20execute%20the%20following%20command%3A%20%3CBR%20%2F%3E%3CCODE%3E%0A%20%20%20dotnet%20new%20razor%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20After%20a%20few%20seconds%2C%20you%20will%20see%20in%20the%20folder%20appearing%20all%20the%20files%20which%20compose%20the%20web%20application.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67506i93BA28D6B80164CC%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20If%20you%20want%20to%20test%20the%20website%2C%20you%20can%20just%20return%20to%20the%20PowerShell%20prompt%20and%20type%20the%20following%20command%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20dotnet%20run%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20You%20will%20see%20an%20output%20like%20the%20following%20one%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%26gt%3B%20dotnet%20run%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Using%20launch%20settings%20from%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%5CProperties%5ClaunchSettings.json...%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20info%3A%20Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager%5B0%5D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20User%20profile%20is%20available.%20Using%20'C%3A%5CUsers%5Cmpagani%5CAppData%5CLocal%5CASP.NET%5CDataProtection-Keys'%20as%20key%20repository%20and%20Windows%20DPAPI%20to%20encrypt%20keys%20at%20rest.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Hosting%20environment%3A%20Development%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Content%20root%20path%3A%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Now%20listening%20on%3A%20https%3A%2F%2Flocalhost%3A5001%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Now%20listening%20on%3A%20http%3A%2F%2Flocalhost%3A5000%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Application%20started.%20Press%20Ctrl%2BC%20to%20shut%20down.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Application%20is%20shutting%20down...%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20The%20web%20server%20which%20hosts%20your%20web%20application%20has%20started.%20As%20you%20can%20see%2C%20it's%20listening%20on%20port%205000%20on%20your%20local%20machine.%20As%20such%2C%20it's%20enough%20to%20open%20your%20browser%20and%20point%20it%20to%0A%20%20%3CSTRONG%3E%0A%20%20%20%3CA%20href%3D%22http%3A%2F%2Flocalhost%3A5000%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20%20http%3A%2F%2Flocalhost%3A5000%0A%20%20%20%3C%2FA%3E%0A%20%20%3C%2FSTRONG%3E%0A%20%20to%20see%20the%20website%20running%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67507i58CF141E7B3D6091%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20If%20you%20need%20a%20full%20debugging%20experience%2C%20it's%20enough%20in%20Visual%20Studio%20Code%20to%20click%20on%20the%20Debug%20symbol%20and%2C%20in%20the%20dropdown%20menu%20at%20the%20top%2C%20press%20the%20Play%20button%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67508i929C6485E1C9246E%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20The%20web%20server%20will%20be%20started%20and%20a%20new%20instance%20of%20your%20favorite%20browser%20will%20be%20automatically%20opened.%20This%20way%2C%20you%20can%20experience%20a%20full%20debugging%20session%20like%20in%20Visual%20Studio%202017%3A%20you%20can%20place%20breakpoints%3B%20add%20watches%3B%20etc.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CH3%20id%3D%22creating-our-first-image%22%20id%3D%22toc-hId--1016290334%22%20id%3D%22toc-hId--988467492%22%3E%0A%20%20%20Creating%20our%20first%20image%0A%20%20%3C%2FH3%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20that%20we%20have%20a%20website%2C%20we%20want%20to%20move%20it%20inside%20a%20container%2C%20which%20will%20host%20both%20the%20web%20server%20and%20our%20application.%20The%20way%20to%20do%20this%20is%20to%20build%20an%20image%2C%20which%20we%20can%20use%20to%20create%20on%20or%20more%20containers.%0A%20%20%3CBR%20%2F%3E%0A%20%20When%20you%20create%20an%20image%2C%20you%20hardly%20start%20from%20scratch.%20Usually%20an%20image%20is%20built%20starting%20from%20an%20existing%20one%2C%20which%20provides%20the%20framework%20%2F%20platform%20we%20need%20to%20host%20our%20application.%0A%20%20%3CBR%20%2F%3E%0A%20%20In%20our%20case%2C%20since%20it's%20a%20.NET%20Core%20application%2C%20we%20need%20to%20build%20an%20image%20which%2C%20other%20than%20hosting%20our%20website%2C%20it%20must%20include%20also%20the%20.NET%20Core%20runtime.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Images%20are%20defined%20by%20a%20text%20file%20called%0A%20%20%3CSTRONG%3E%0A%20%20%20Dockerfile%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%20which%20describes%20all%20the%20features%20that%20the%20image%20must%20incorporate%3A%20which%20ports%20to%20expose%3B%20which%20commands%20to%20execute%3B%20which%20is%20the%20entry%20point%20to%20launch%20when%20the%20container%20starts%3B%20etc.%0A%20%20%3CBR%20%2F%3E%0A%20%20There%20are%20many%20commands%20that%20you%20can%20include%20in%20a%20Dockerfile.%20You%20can%20find%20the%20full%20reference%0A%20%20%3CA%20href%3D%22https%3A%2F%2Fdocs.docker.com%2Fengine%2Freference%2Fbuilder%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20in%20the%20official%20documentation%0A%20%20%3C%2FA%3E%0A%20%20.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20A%20Dockerfile%20is%20simply%20a%20text%20file.%20As%20such%2C%20just%20click%20in%20Visual%20Studio%20Code%20on%20the%20icon%20to%20add%20a%20new%20file%20(you'll%20find%20it%20near%20the%20name%20of%20your%20project)%20and%20name%20it%0A%20%20%3CSTRONG%3E%0A%20%20%20Dockerfile%0A%20%20%3C%2FSTRONG%3E%0A%20%20(without%20extension).%20Let's%20see%20how%20our%20file%20looks%20like%2C%20then%20we're%20going%20to%20explain%20it%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20FROM%20microsoft%2Fdotnet%3A2.1-sdk%20AS%20build-env%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%23%20Copy%20csproj%20and%20restore%20as%20distinct%20layers%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20*.csproj%20.%2F%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20restore%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%23%20Copy%20everything%20else%20and%20build%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20.%20.%2F%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20publish%20-c%20Release%20-o%20out%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%23%20Build%20runtime%20image%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20FROM%20microsoft%2Fdotnet%3A2.1-aspnetcore-runtime%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20--from%3Dbuild-env%20%2Fapp%2Fout%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20ENTRYPOINT%20%5B%22dotnet%22%2C%20%22WebSample.dll%22%5D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20EXPOSE%2080%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20You%20can%20notice%20some%20uppercase%20keywords%2C%20like%0A%20%20%3CSTRONG%3E%0A%20%20%20FROM%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%0A%20%20%3CSTRONG%3E%0A%20%20%20COPY%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%20etc.%20These%20are%20the%20commands%20that%20Docker%20sends%20to%20the%20service%20to%20build%20the%20image.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20In%20this%20specific%20case%2C%20this%20dockerfile%20builds%20the%20image%20in%20two%20separate%20steps%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CUL%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20first%20one%20takes%20the%20whole%20project%20and%20compiles%20it.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20second%20one%20takes%20the%20output%20of%20compilation%20and%20runs%20it.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FUL%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20When%20you%20use%20this%20approach%20Docker%20generates%20intermediate%20containers%2C%20which%20are%20then%20deleted%20at%20the%20end%20of%20the%20process.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Let's%20start%20from%20the%20first%20block%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3COL%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20FROM%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20is%20the%20command%20used%20to%20define%20which%20image%20we%20want%20to%20use%20as%20a%20starting%20point%20for%20our%20own%20image.%20As%20I%20said%2C%20in%20the%20first%20step%20we%20are%20going%20to%20build%20our%20web%20application%20from%20the%20source.%20As%20such%2C%20we%20need%20to%20download%20the%20official%20.NET%20Core%20image%20with%20the%20full%20SDK%2C%20since%20it%20contains%20the%20compilation%20tool%20chain.%20After%20the%20FROM%20statement%2C%20we%20specify%20the%20full%20name%20of%20the%20image%20(%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20microsoft%2Fdotnet%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20)%2C%20followed%20by%20the%20tag%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%202.1-sdk%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20.%20We're%20going%20to%20use%2C%20as%20a%20starting%20point%2C%20a%20Linux%20image%20with%20the%20.NET%20Core%202.1%20SDK.%20By%20using%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20AS%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%2C%20we%20assign%20an%20alias%20to%20the%20container%2C%20so%20that%20we%20can%20reference%20it%20later.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20WORKDIR%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20is%20the%20command%20used%20to%20define%20which%20is%20the%20starting%20folder%20where%20we%20want%20to%20operate%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20COPY%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20is%20the%20command%20used%20to%20copy%20some%20files%20from%20one%20location%20to%20another.%20When%20you%20create%20an%20image%2C%20you%20need%20to%20pass%20a%20build%20context%2C%20which%20are%20all%20the%20files%20on%20your%20machine%20that%20are%20required%20to%20build%20your%20app.%20In%20our%20case%2C%20we%20will%20pass%20the%20full%20content%20of%20the%20folder%20containing%20our%20web%20project.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20RUN%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20is%20used%20to%20execute%20commands%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20inside%20the%20container%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20.%20By%20running%20the%20command%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20dotnet%20restore%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20%2C%20we%20force%20the%20restore%20of%20all%20the%20dependencies%20(like%20the%20NuGet%20packages)%20required%20to%20build%20the%20web%20application.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20We%20use%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20COPY%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%20again%20to%20copy%20every%20additional%20file%20that%20might%20have%20been%20downloaded%20during%20the%20restore%20process.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20We%20use%20again%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20RUN%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%2C%20this%20time%20to%20build%20our%20web%20application.%20We%20execute%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20dotnet%20publish%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%2C%20specifying%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20Release%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20as%20compilation%20mode%20(with%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20-c%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20parameter)%20and%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20out%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20as%20folder%20(inside%20the%20container)%20where%20to%20store%20the%20build%20output.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FOL%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20At%20the%20end%20of%20this%20process%2C%20we%20have%20a%20container%20which%20includes%20the%20full%20compiled%20version%20of%20our%20web%20application.%20Now%20we're%20ready%20to%20generate%20the%20final%20image%2C%20which%20is%20created%20in%20the%20second%20part%20of%20the%20file%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3COL%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20We%20use%20again%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20FROM%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%20to%20define%20the%20starting%20image.%20In%20this%20case%2C%20we%20don't%20need%20any%20more%20to%20compile%20the%20application%2C%20but%20just%20to%20run%20it.%20As%20such%2C%20we%20can%20grab%20the%20runtime%20image%2C%20which%20is%20lighter%20and%20smaller.%20We%20use%20again%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20microsoft%2Fdotnet%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20image%20as%20a%20starting%20point%2C%20but%20this%20time%20we%20specify%20the%20tag%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%202.1-aspnetcore-runtime%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20We%20set%20again%20the%20base%20dire%20using%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20WORKDIR%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20If%20you%20remember%2C%20in%20the%20previous%20step%20we%20have%20assigned%20the%20alias%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20build-env%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20to%20the%20first%20container.%20Thanks%20to%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20--from%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20parameter%2C%20we%20can%20reference%20it%20and%20access%20to%20the%20build%20output%2C%20which%20is%20stored%20in%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20%2Fapp%2Fout%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20folder.%20By%20using%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20COPY%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%2C%20we%20copy%20the%20content%20of%20this%20folder%20to%20the%20root%20of%20the%20image.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20We%20can%20see%20now%20a%20new%20command%2C%20called%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20ENTRYPOINT%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20.%20It's%20used%20to%20specify%20which%20is%20the%20default%20command%20to%20execute%20when%20the%20container%20is%20started.%20In%20this%20case%2C%20we%20lunch%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20dotnet%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20command%20followed%20by%20the%20name%20of%20the%20DLL%20which%20hosts%20our%20website.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FOL%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBLOCKQUOTE%3E%0A%20%20%20%3CSTRONG%3E%0A%20%20%20%20Pay%20attention!%0A%20%20%20%3C%2FSTRONG%3E%0A%20%20%20Make%20sure%20that%20the%20name%20of%20the%20DLL%20exactly%20matches%20the%20one%20created%20during%20the%20build%20process%2C%20including%20uppercase%20and%20lowercase.%20We%20are%20going%20to%20run%20our%20image%20inside%20a%20Linux%20container%2C%20which%20is%20a%20case%20sentive%20operating%20system.%20As%20such%2C%20websample.dll%20and%20WebSample.dll%20are%20considered%20two%20different%20files.%0A%20%20%3C%2FBLOCKQUOTE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3COL%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20last%20command%2C%20called%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20EXPOSE%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20%2C%20is%20used%20to%20specify%20which%20port%20will%20be%20used%20to%20listen%20to%20incoming%20communication%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20inside%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20the%20container.%20This%20is%20a%20very%20important%20concept%20to%20understand.%20If%20you%20want%20to%20forward%20the%20internal%20port%20to%20the%20host%20machine%2C%20you%20will%20still%20need%20to%20run%20the%20container%20using%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20-p%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20parameter%2C%20like%20we%20did%0A%20%20%20%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2018%2F09%2F10%2Ffirst-steps-with-docker-introduction%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20%20%20in%20the%20previous%20post%0A%20%20%20%20%3C%2FA%3E%0A%20%20%20%20when%20we%20have%20started%20a%20web%20server%20using%20a%20NGINX%20container.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FOL%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20that%20we%20have%20a%20proper%20Dockerfile%2C%20we%20can%20generate%20the%20image%20using%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20build%0A%20%20%3C%2FSTRONG%3E%0A%20%20command.%20Open%20again%20the%20terminal%20inside%20Visual%20Studio%20code%20on%20the%20folder%20which%20contains%20your%20project%20and%20run%20the%20following%20command%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CWebSample%26gt%3B%20docker%20build%20-t%20qmatteoq%2Fwebsample%3Alatest%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Sending%20build%20context%20to%20Docker%20daemon%20%204.474MB%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%201%2F11%20%3A%20FROM%20microsoft%2Fdotnet%3A2.1-sdk%20AS%20build-env%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%207e0402debc11%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%202%2F11%20%3A%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%205d1059fee5f7%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%203%2F11%20%3A%20COPY%20*.csproj%20.%2F%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20fa5b2af8e3b7%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%204%2F11%20%3A%20RUN%20dotnet%20restore%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%202df0c6cae62d%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%205%2F11%20%3A%20COPY%20.%20.%2F%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20f26c6108e9a5%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%206%2F11%20%3A%20RUN%20dotnet%20publish%20-c%20Release%20-o%20out%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20bab372a643da%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%207%2F11%20%3A%20FROM%20microsoft%2Fdotnet%3A2.1-aspnetcore-runtime%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20a6cd65b10ab6%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%208%2F11%20%3A%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%2017d4a4528792%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%209%2F11%20%3A%20COPY%20--from%3Dbuild-env%20%2Fapp%2Fout%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20adceeed37516%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%2010%2F11%20%3A%20ENTRYPOINT%20%5B%22dotnet%22%2C%20%22WebSample.dll%22%5D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%201900eef5e657%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Step%2011%2F11%20%3A%20EXPOSE%2080%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%20Using%20cache%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20---%26gt%3B%2051b6cb719f13%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Successfully%20built%2051b6cb719f13%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Successfully%20tagged%20qmatteoq%2Fwebsample%3Alatest%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20The%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20build%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%20requires%2C%20at%20minimum%2C%20two%20parameters%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CUL%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20name%20of%20the%20image%20you%20want%20to%20create%2C%20which%20is%20specified%20using%20the%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20-t%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20parameter.%20Notice%20how%20I%20have%20followed%20the%20naming%20convention%20username%2Fname%20of%20the%20image%3Atag.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20build%20context%2C%20which%20is%20the%20set%20of%20files%20and%20folders%20that%20are%20required%20to%20build%20the%20image.%20In%20this%20case%2C%20since%20we%20are%20running%20the%20build%20operation%20from%20the%20same%20folder%20that%20contains%20our%20web%20application%2C%20we%20pass%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20.%20(dot)%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20as%20context.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FUL%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20As%20you%20can%20notice%2C%20each%20row%20of%20the%20Dockerfile%20is%20treated%20as%20a%20different%20step%2C%20which%20output%20is%20stored%20locally.%20This%20way%2C%20if%20you%20need%20to%20build%20the%20image%20again%2C%20the%20steps%20that%20didn't%20change%20will%20be%20retrieved%20from%20the%20local%20cache%20instead%20of%20being%20executed%20again.%20You%20can%20easily%20this%20by%20running%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20build%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%20again.%20This%20time%20the%20image%20will%20be%20generated%20in%20less%20than%20a%20second%2C%20because%20nothing%20has%20changed.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20If%20you've%20done%20everything%20properly%2C%20you%20should%20be%20able%20to%20see%20your%20image%20listed%20among%20the%20others%20by%20running%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20image%20list%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CWebSample%26gt%3B%20docker%20image%20list%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20REPOSITORY%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20TAG%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20IMAGE%20ID%20%20%20%20%20%20%20%20%20%20%20%20CREATED%20%20%20%20%20%20%20%20%20%20%20%20%20SIZE%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20qmatteoq%2Fwebsample%20%20%20%20%20%20%20%20%20latest%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2051b6cb719f13%20%20%20%20%20%20%20%20About%20an%20hour%20ago%20%20%20258MB%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20that%20we%20have%20an%20image%2C%20we%20can%20easily%20test%20it%20by%20running%20a%20container%20based%20on%20it.%20We%20have%20already%20seen%2C%0A%20%20%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fappconsult%2F2018%2F09%2F10%2Ffirst-steps-with-docker-introduction%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20in%20the%20previous%20post%0A%20%20%3C%2FA%3E%0A%20%20%2C%20how%20to%20run%20a%20container%20which%20must%20stay%20active%20in%20background%2C%20so%20we%20can%20reuse%20the%20same%20knowledge%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20docker%20run%20-p%208080%3A80%20--rm%20-d%20qmatteoq%2Fwebsample%3Alatest%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20You%20can%20run%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20ps%0A%20%20%3C%2FSTRONG%3E%0A%20%20to%20check%20that%20the%20container%20is%20indeed%20up%20%26amp%3B%20running%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%26gt%3B%20docker%20ps%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20CONTAINER%20ID%20%20%20%20%20%20%20%20IMAGE%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20COMMAND%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20CREATED%20%20%20%20%20%20%20%20%20%20%20%20%20STATUS%20%20%20%20%20%20%20%20%20%20%20%20%20%20PORTS%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20NAMES%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20884e99c66b4c%20%20%20%20%20%20%20%20qmatteoq%2Ftestwebapp%20%20%20%22dotnet%20TestWebApp.d%E2%80%A6%22%20%20%205%20seconds%20ago%20%20%20%20%20%20%20Up%203%20seconds%20%20%20%20%20%20%20%200.0.0.0%3A8080-%26gt%3B80%2Ftcp%20%20%20youthful_lumiere%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20We%20can%20test%20it%20by%20opening%20the%20browser%20and%20pointing%20it%20to%20the%20URL%0A%20%20%3CSTRONG%3E%0A%20%20%20%3CA%20href%3D%22http%3A%2F%2Flocalhost%3A8080%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20%20http%3A%2F%2Flocalhost%3A8080%0A%20%20%20%3C%2FA%3E%0A%20%20%3C%2FSTRONG%3E%0A%20%20(since%20we%20have%20forwarded%20the%2080%20port%20inside%20the%20container%20to%20the%208080%20port%20on%20our%20host%20machine).%0A%20%20%3CBR%20%2F%3E%0A%20%20If%20you%20did%20everything%20correctly%2C%20you%20should%20see%20the%20same%20website%20we%20have%20seen%20before%20when%20we%20have%20launched%20it%20directly%20from%20our%20machine.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CH3%20id%3D%22using-visual-studio-code-and-the-docker-extension-to-make-your-life-easier%22%20id%3D%22toc-hId-726520001%22%20id%3D%22toc-hId-754342843%22%3E%0A%20%20%20Using%20Visual%20Studio%20Code%20and%20the%20Docker%20extension%20to%20make%20your%20life%20easier%0A%20%20%3C%2FH3%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Thanks%20to%20the%20Docker%20extension%20we%20have%20installed%20at%20the%20beginning%20of%20the%20post%2C%20building%20images%20is%20a%20much%20easier%20experience.%20First%2C%20because%20we%20have%20support%20for%20Intellisense.%20Go%20to%20the%20Dockerfile%20you%20have%20just%20created%2C%20place%20the%20cursor%20on%20a%20new%20line%20and%20press%20CTRL-SPACE.%20Visual%20Studio%20Code%20will%20suggest%20you%20all%20the%20available%20commands%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67509i53DCA77D060FE5E8%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Additionally%2C%20if%20you%20place%20the%20cursor%20near%20a%20command%2C%20you%20will%20see%20the%20documentation%20popping%20out%2C%20showing%20the%20supported%20syntax.%20And%20if%20you%20want%20to%20create%20the%20image%20starting%20from%20the%20Dockerfile%2C%20you%20don't%20have%20to%20manually%20run%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20build%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%2C%20but%20you%20can%20just%20right%20click%20on%20the%20file%20in%20the%20Explorer%20and%20choose%0A%20%20%3CSTRONG%3E%0A%20%20%20Build%20Image%0A%20%20%3C%2FSTRONG%3E%0A%20%20%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67510i914B262B6EC5E24B%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Visual%20Studio%20Code%20will%20ask%20you%20first%20to%20specify%20the%20name%20of%20the%20image%20and%20then%2C%20automatically%2C%20it%20will%20open%20up%20a%20terminal%20and%20execute%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20build%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%20with%20the%20right%20parameters.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67512i07CBC5945DC018D7%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20The%20Docker%20extension%20can%20even%20do%20more%20than%20this.%20Starting%20from%20the%20structure%20of%20your%20project%2C%20it's%20able%20automatically%20to%20generate%20a%20Dockerfile%20for%20you.%0A%20%20%3CBR%20%2F%3E%0A%20%20First%20press%20CTRL-ALT-P%20to%20bring%20up%20the%20list%20of%20commands%20availble%20and%20look%20for%20one%20called%0A%20%20%3CSTRONG%3E%0A%20%20%20Docker%3A%20Add%20files%20to%20your%20workspace%0A%20%20%3C%2FSTRONG%3E%0A%20%20.%0A%20%20%3CBR%20%2F%3E%0A%20%20You%20will%20be%20asked%20a%20few%20questions%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3COL%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20Application%20Platform.%20Choose%20.%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20NET%20Core%20Console%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20and%20not%20ASP.NET%20Core.%20I'll%20explain%20in%20a%20bit%20why.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20Operating%20System.%20Choose%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20Linux%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20The%20port%20your%20app%20listen%20on.%20Leave%20the%20default%20value%2C%20which%20is%2080.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FOL%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20The%20tool%20will%20automatically%20generate%20two%20files%20for%20you%20in%20the%20folder.%20One%20is%20the%20Dockerfile%2C%20which%20we%20already%20know.%20The%20other%20one%20is%20called%0A%20%20%3CSTRONG%3E%0A%20%20%20.dockerignore%0A%20%20%3C%2FSTRONG%3E%0A%20%20and%20it%20works%20in%20a%20similar%20way%20as%20the%20.gitignore%20one%20used%20by%20Git.%20It%20contains%20a%20lists%20of%20files%20and%20folder%20which%20should%20be%20ignored%20during%20the%20build%20process%20and%20not%20copied%20over%20the%20container%2C%20like%20the%20Dockerfile%20itself%2C%20log%20files%2C%20Git%20files%2C%20etc.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Here%20is%20a%20sample%20of%20the%20Dockerfile%20generated%20by%20Visual%20Studio%20Code%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20FROM%20microsoft%2Fdotnet%3A2.0-runtime%20AS%20base%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20FROM%20microsoft%2Fdotnet%3A2.0-sdk%20AS%20build%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fsrc%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20TestWebApp.csproj%20TestWebApp%2F%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20restore%20TestWebApp%2FTestWebApp.csproj%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fsrc%2FTestWebApp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20.%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20build%20TestWebApp.csproj%20-c%20Release%20-o%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20FROM%20build%20AS%20publish%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20publish%20TestWebApp.csproj%20-c%20Release%20-o%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20FROM%20base%20AS%20final%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20--from%3Dpublish%20%2Fapp%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20ENTRYPOINT%20%5B%22dotnet%22%2C%20%22TestWebApp.dll%22%5D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Unfortunately%20the%20Docker%20extension%20isn't%20really%20up%20to%20date%20and%20it%20isn't%20leveraging%20the%20latest%20images%20published%20by%20Microsoft.%20This%20is%20the%20reason%20why%20I%20told%20you%20to%20choose%20.NET%20Core%20Console%20as%20application%20platform%2C%20even%20if%20we%20have%20built%20an%20ASP.NET%20Core%20website.%20The%20ASP.NET%20Core%20Dockerfile%2C%20in%20fact%2C%20points%20to%20an%20old%20image%20that%20doesn't%20exist%20anymore%2C%20called%0A%20%20%3CSTRONG%3E%0A%20%20%20aspnetcore%0A%20%20%3C%2FSTRONG%3E%0A%20%20.%20Since%20a%20while%2C%20in%20fact%2C%20Microsoft%20has%20unified%20all%20the%20.NET%20Core%20images%20under%20the%20same%20name%20(%0A%20%20%3CSTRONG%3E%0A%20%20%20dotnet%0A%20%20%3C%2FSTRONG%3E%0A%20%20)%20and%20it%20uses%20tags%20to%20differentate%20among%20the%20various%20workloads.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20However%2C%20the%20generated%20Dockerfile%20isn't%20still%20perfect.%20There%20are%20a%20couple%20of%20changes%20we%20need%20to%20make%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3COL%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20We%20have%20built%20an%20application%20using%20.NET%20Core%202.1%2C%20but%20the%20Dockerfile%20downloads%20the%20image%20for%20.NET%20Core%202.0.%20As%20such%2C%20we%20need%20to%20change%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%202.0%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20to%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%202.1%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20inside%20the%20tag.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CLI%3E%0A%20%20%20%20Since%20this%20is%20the%20Dockerfile%20for%20.NET%20Core%20console%20apps%2C%20the%20base%20image%20doesn't%20include%20all%20the%20required%20libraries%20to%20host%20a%20website.%20As%20such%2C%20we%20need%20to%20change%20the%20base%20image%20from%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20runtime%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20to%0A%20%20%20%20%3CSTRONG%3E%0A%20%20%20%20%20aspnetcore-runtime%0A%20%20%20%20%3C%2FSTRONG%3E%0A%20%20%20%20.%0A%20%20%20%3C%2FLI%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FOL%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20This%20is%20how%20the%20fixed%20Dockerfile%20looks%20like%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20FROM%20microsoft%2Fdotnet%3A2.1-aspnetcore-runtime%20AS%20base%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20FROM%20microsoft%2Fdotnet%3A2.1-sdk%20AS%20build%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fsrc%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20TestWebApp.csproj%20TestWebApp%2F%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20restore%20TestWebApp%2FTestWebApp.csproj%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fsrc%2FTestWebApp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20.%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20build%20TestWebApp.csproj%20-c%20Release%20-o%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20FROM%20build%20AS%20publish%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20RUN%20dotnet%20publish%20TestWebApp.csproj%20-c%20Release%20-o%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20FROM%20base%20AS%20final%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WORKDIR%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20COPY%20--from%3Dpublish%20%2Fapp%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20ENTRYPOINT%20%5B%22dotnet%22%2C%20%22TestWebApp.dll%22%5D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CH3%20id%3D%22pushing-an-image-on-docker-hub%22%20id%3D%22toc-hId--1825636960%22%20id%3D%22toc-hId--1797814118%22%3E%0A%20%20%20Pushing%20an%20image%20on%20Docker%20Hub%0A%20%20%3C%2FH3%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20that%20you%20have%20an%20image%2C%20you%20can%20share%20it%20with%20other%20people%20or%20your%20customers.%20The%20easiest%20way%20is%20to%20push%20it%20on%20Docker%20Hub.%20First%2C%20you%20will%20need%20to%20create%20an%20account%2C%20by%20registering%0A%20%20%3CA%20href%3D%22https%3A%2F%2Fhub.docker.com%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20on%20the%20website%0A%20%20%3C%2FA%3E%0A%20%20.%20From%0A%20%20%3CA%20href%3D%22https%3A%2F%2Fhub.docker.com%2Fbilling-plans%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20a%20billing%20point%20of%20view%0A%20%20%3C%2FA%3E%0A%20%20%2C%20Docker%20Hub%20works%20in%20a%20similar%20way%20as%20GitHub.%20With%20the%20free%20plan%2C%20you%20can%20create%20unlimited%20public%20repostories%2C%20but%20only%201%20private%20repositories.%20Paid%20plans%2C%20instead%2C%20offer%20a%20greater%20number%20of%20private%20repositories.%20Additionally%2C%20Docker%20Hub%20supports%20also%20a%20build%20pipeline.%20You%20can%20point%20it%20to%20a%20repository%20which%20hosts%20your%20project's%20code%20and%20have%20it%20automatically%20generate%20and%20push%20a%20new%20image%20every%20time%20you%20make%20some%20changes.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Once%20you%20have%20registered%2C%20you%20need%20to%20login%20as%20first%20step%2C%20by%20invoking%20the%20command%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20login%0A%20%20%3C%2FSTRONG%3E%0A%20%20.%20You%20can%20also%20login%20directly%20in%20the%20Docker%20for%20Windows%20application.%20This%20way%2C%20the%20CLI%20will%20be%20automatically%20able%20to%20leverage%20the%20same%20authentication%20token.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20you're%20ready%20to%20push%20your%20image%20with%20a%20very%20simple%20command%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%26gt%3B%20docker%20push%20qmatteoq%2Ftestwebapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20The%20push%20refers%20to%20repository%20%5Bdocker.io%2Fqmatteoq%2Ftestwebapp%5D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20bdc78d020741%3A%20Pushed%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20973c6d532b06%3A%20Mounted%20from%20qmatteoq%2Fnewsfeed%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%2005dff760e6df%3A%20Mounted%20from%20qmatteoq%2Fnewsfeed%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%2085614e3f95f6%3A%20Mounted%20from%20qmatteoq%2Fnewsfeed%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%209104e47cd398%3A%20Mounted%20from%20qmatteoq%2Fnewsfeed%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20cdb3f9544e4c%3A%20Mounted%20from%20qmatteoq%2Fnewsfeed%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20latest%3A%20digest%3A%20sha256%3A07c2b0694a1474563912f2cabc807d1927fe925ded1cedae84c0accd0e423b86%20size%3A%201580%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20By%20default%2C%20unless%20you%20specify%20a%20different%20configuration%2C%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20push%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%20will%20assume%20that%20you%20want%20to%20push%20the%20image%20on%20Docker%20Hub.%20At%20the%20end%20of%20the%20process%2C%20you'll%20be%20able%20to%20see%20your%20image%20in%20your%20repository%20on%20the%20website%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67513iFA528555291474DA%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Your%20image%20is%20now%20stored%20in%20the%20cloud%20and%20ready%20to%20be%20reused.%20You%20can%20easily%20see%20this%20behavior%20by%20removing%20the%20local%20copy%20you%20have%20on%20your%20machine%2C%20by%20using%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20image%20rm%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%2C%20followed%20by%20the%20name%20of%20the%20image.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%26gt%3B%20docker%20image%20rm%20qmatteoq%2Ftestwebapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Untagged%3A%20qmatteoq%2Ftestwebapp%3Alatest%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Untagged%3A%20qmatteoq%2Ftestwebapp%40sha256%3A07c2b0694a1474563912f2cabc807d1927fe925ded1cedae84c0accd0e423b86%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Deleted%3A%20sha256%3Ae893220c97a69766a728855d13deafe911f25ee814a5f361f9c250c2a3c7c125%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Deleted%3A%20sha256%3Af385d8433f2a5dd139a6e1fa189f4bccde435a3908396cd15ffd4ef2059f1872%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Deleted%3A%20sha256%3Aabfa55c74332b2322d76e89b32b789eeb853e2486e65f2278679479fa8fee917%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20try%20to%20run%20it%20again%20with%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20run%0A%20%20%3C%2FSTRONG%3E%0A%20%20command%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%26gt%3B%20docker%20run%20-p%208080%3A80%20--rm%20-d%20qmatteoq%2Ftestwebapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Unable%20to%20find%20image%20'qmatteoq%2Ftestwebapp%3Alatest'%20locally%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20latest%3A%20Pulling%20from%20qmatteoq%2Ftestwebapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20be8881be8156%3A%20Already%20exists%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%204c13ce9875a3%3A%20Already%20exists%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20b40109ab8fda%3A%20Already%20exists%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20a7130f23f49b%3A%20Already%20exists%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%200b0b625a5a54%3A%20Already%20exists%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%207cd454006d0b%3A%20Pull%20complete%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Digest%3A%20sha256%3A07c2b0694a1474563912f2cabc807d1927fe925ded1cedae84c0accd0e423b86%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Status%3A%20Downloaded%20newer%20image%20for%20qmatteoq%2Ftestwebapp%3Alatest%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20You%20can%20see%20how%20the%20image%20wasn't%20found%20in%20the%20local%20repository%20but%2C%20since%20it's%20now%20stored%20in%20cloud%2C%20Docker%20has%20been%20able%20to%20pull%20it%20anyway.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CH3%20id%3D%22updating-our-website%22%20id%3D%22toc-hId--82826625%22%20id%3D%22toc-hId--55003783%22%3E%0A%20%20%20Updating%20our%20website%0A%20%20%3C%2FH3%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Let's%20say%20that%20we%20need%20to%20modify%20our%20web%20application%20and%20release%20an%20update.%20How%20we%20can%20update%20the%20running%20container%3F%20Well%2C%20we%20don't%20%3D)%3C%2Fimg%3E%20As%20we%20have%20seen%2C%20one%20of%20the%20benefits%20of%20Docker%20is%20that%20containers%20are%20really%20lightweight%20and%20fast%20to%20start.%20As%20such%2C%20when%20you%20need%20to%20update%20your%20application%2C%20you%20don't%20update%20the%20running%20container%2C%20but%20you%20just%20update%20the%20base%20image.%20Then%20you%20spin%20up%20a%20new%20container%2C%20using%20the%20updated%20image.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Updating%20the%20image%20is%20really%20easy.%20You%20just%20need%20to%20run%20the%20same%0A%20%20%3CSTRONG%3E%0A%20%20%20docker%20build%0A%20%20%3C%2FSTRONG%3E%0A%20%20command.%20Since%20Docker%20will%20find%20that%20you%20already%20have%20an%20image%20with%20the%20same%20name%20and%20tag%2C%20it%20will%20just%20replace%20it.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Let's%20try%20this%20by%20doing%20a%20minor%20change%20in%20our%20website.%20Open%20the%20file%0A%20%20%3CSTRONG%3E%0A%20%20%20Index.cshtml%0A%20%20%3C%2FSTRONG%3E%0A%20%20inside%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20Pages%0A%20%20%3C%2FSTRONG%3E%0A%20%20folder%20of%20your%20project%20and%20edit%20something.%20For%20example%2C%20change%20the%20title%20of%20the%20page%20in%20the%20first%20lines%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20%40page%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%40model%20IndexModel%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%40%7B%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20ViewData%5B%22Title%22%5D%20%3D%20%22Home%20page%20running%20inside%20Docker%22%3B%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%7D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20build%20the%20image%20again%2C%20either%20by%20right%20clicking%20on%20the%20Dockerfile%20and%20by%20choosing%0A%20%20%3CSTRONG%3E%0A%20%20%20Build%20Image%0A%20%20%3C%2FSTRONG%3E%0A%20%20or%20by%20manually%20running%20the%20following%20command%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20docker%20build%20-t%20qmatteoq%2Fwebsample%3Alatest%20.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20You%20will%20notice%20that%2C%20this%20time%2C%20many%20steps%20won't%20be%20executed%20from%20scratch%20but%20they%20will%20be%20retrieved%20from%20the%20cache.%20For%20example%2C%20the%0A%20%20%3CSTRONG%3E%0A%20%20%20dotnet%20restore%0A%20%20%3C%2FSTRONG%3E%0A%20%20step%20won't%20be%20excuted%20again%2C%20because%20the%20dependencies%20leveraged%20by%20the%20web%20application%20haven't%20changed.%20The%0A%20%20%3CSTRONG%3E%0A%20%20%20COPY%0A%20%20%3C%2FSTRONG%3E%0A%20%20and%0A%20%20%3CSTRONG%3E%0A%20%20%20dotnet%20build%0A%20%20%3C%2FSTRONG%3E%0A%20%20steps%2C%20instead%2C%20will%20be%20repeated%2C%20because%20the%20content%20of%20the%20website%20has%20changed%20and%20it%20must%20be%20compiled%20again.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Now%20run%20again%20the%20image%20with%20the%20following%20command%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20docker%20run%20-p%208080%3A80%20--rm%20-d%20qmatteoq%2Ftestwebapp%3Alatest%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Open%20again%20your%20browser%20and%20point%20it%20to%0A%20%20%3CSTRONG%3E%0A%20%20%20%3CA%20href%3D%22http%3A%2F%2Flocalhost%3A8080%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%0A%20%20%20%20http%3A%2F%2Flocalhost%3A8080%0A%20%20%20%3C%2FA%3E%0A%20%20%3C%2FSTRONG%3E%0A%20%20.%20If%20you%20have%20done%20everything%20properly%2C%20you%20should%20see%20the%20same%20website%20but%20with%20a%20different%20title%20in%20the%20tab%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67515iC510CE2288C107AA%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CH3%20id%3D%22managing-docker-in-visual-studio-code%22%20id%3D%22toc-hId-1659983710%22%20id%3D%22toc-hId-1687806552%22%3E%0A%20%20%20Managing%20Docker%20in%20Visual%20Studio%20Code%0A%20%20%3C%2FH3%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Once%20you%20have%20installed%20the%20Docker%20extension%20in%20Visual%20Studio%20Code%2C%20you%20will%20find%20a%20new%20tab%20with%20the%20Docker%20logo%2C%20which%20will%20give%20you%20access%20to%20a%20set%20of%20helpful%20visual%20tools.%20You%20will%20be%20able%2C%20in%20fact%2C%20to%20explore%20all%20the%20images%2C%20running%20containers%20and%20registries%20on%20our%20machine%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67516iD4AF0E5F0EDEECA6%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20You%20have%20access%20to%20three%20sections.%0A%20%20%3CSTRONG%3E%0A%20%20%20Images%0A%20%20%3C%2FSTRONG%3E%0A%20%20will%20display%20all%20the%20images%20you%20currently%20have%20on%20your%20local%20machine.%20By%20right%20clicking%20on%20one%20of%20them%2C%20you'll%20be%20able%20to%20push%20it%20on%20Docker%20Hub%2C%20run%20it%2C%20delete%20it%2C%20etc.%20Personally%2C%20even%20if%20it's%20very%20convenient%2C%20I%20rarely%20use%20the%20run%20command%2C%20because%20it%20lacks%20the%20opportunity%20to%20customize%20the%20various%20parameter.%20For%20example%2C%20you%20can't%20specify%20the%20port%20you%20want%20to%20use%20to%20expose%20the%20service%20on%20the%20host%2C%20but%20it%20will%20automatically%20use%20the%20default%20one%20exposed%20inside%20the%20container.%20This%20means%20that%20if%20you're%20running%20a%20web%20server%20on%20port%2080%2C%20it%20will%20mirror%20it%20on%20port%2080%20also%20on%20your%20host%20machine.%20Sometimes%20this%20can%20be%20inconvenient%2C%20because%20the%20port%20used%20inside%20the%20container%20can%20be%20already%20occupied%20on%20the%20host.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CSTRONG%3E%0A%20%20%20Containers%0A%20%20%3C%2FSTRONG%3E%0A%20%20will%20display%20all%20the%20running%20containers.%20By%20right%20clicking%20on%20one%20of%20them%2C%20you'll%20be%20able%20to%20restart%20the%20container%2C%20stop%20it%2C%20or%20completely%20remove%20it.%20Two%20useful%20options%20are%0A%20%20%3CSTRONG%3E%0A%20%20%20Show%20logs%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%20which%20will%20display%20the%20last%20output%20on%20the%20console%2C%20and%0A%20%20%3CSTRONG%3E%0A%20%20%20Attach%20Shell%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%20which%20will%20connect%20to%20the%20container%20using%20a%20terminal%20so%20that%20you%20can%20perform%20commands.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20For%20example%2C%20if%20you%20use%0A%20%20%3CSTRONG%3E%0A%20%20%20Show%20logs%0A%20%20%3C%2FSTRONG%3E%0A%20%20on%20the%20container%20we%20have%20previously%20created%2C%20you%20will%20see%20the%20output%20of%20the%20web%20server%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%26gt%3B%20docker%20logs%20-f%208ecf2d283e7a3a8723a6d09222b2aa92829373c81709c51bbac16e06c49ee157%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20warn%3A%20Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager%5B35%5D%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20No%20XML%20encryptor%20configured.%20Key%20%7Ba595c102-eabf-4bfd-a35b-eef1b27d0de0%7D%20may%20be%20persisted%20to%20storage%20in%20unencrypted%20form.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Hosting%20environment%3A%20Production%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Content%20root%20path%3A%20%2Fapp%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Now%20listening%20on%3A%20http%3A%2F%2F%5B%3A%3A%5D%3A80%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20Application%20started.%20Press%20Ctrl%2BC%20to%20shut%20down.%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20If%20you%20use%2C%20instead%2C%0A%20%20%3CSTRONG%3E%0A%20%20%20Attach%20Shell%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%20you%20will%20be%20connected%20to%20the%20Linux%20machine%20which%20is%20hosting%20the%20web%20server%20and%20you'll%20be%20able%20to%20perform%20UNIX%20command.%20For%20example%2C%20if%20you%20type%0A%20%20%3CSTRONG%3E%0A%20%20%20ls%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%20you%20will%20see%20all%20the%20files%20which%20compose%20your%20application%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CCODE%3E%0A%20%20%20PS%20C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CNetCoreApi%5CTestWebApp%26gt%3B%20docker%20exec%20-it%208ecf2d283e7a3a8723a6d09222b2aa92829373c81709c51bbac16e06c49ee157%20%2Fbin%2Fsh%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%23%20ls%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WebSample.Views.dll%20%20WebSample.deps.json%20%20WebSample.pdb%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20appsettings.Development.json%20%20web.config%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20WebSample.Views.pdb%20%20WebSample.dll%20%20%20%20%20%20%20%20WebSample.runtimeconfig.json%20%20appsettings.json%20%20%20%20%20%20%20%20%20%20%20%20%20%20wwwroot%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%20%23%0A%20%20%20%3CBR%20%2F%3E%0A%20%20%3C%2FCODE%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20The%20last%20section%20is%0A%20%20%3CSTRONG%3E%0A%20%20%20Registries%0A%20%20%3C%2FSTRONG%3E%0A%20%20%2C%20which%20gives%20you%20access%20to%20all%20your%20registries%2C%20either%20local%20or%20in%20the%20cloud.%20For%20example%2C%20in%20the%20previous%20image%2C%20you%20can%20see%20how%20I%20can%20explore%20my%20account%20on%20Docker%20Hub%20and%20see%20all%20the%20images%20I've%20published.%0A%20%20%3CBR%20%2F%3E%0A%20%20If%20you%20right%20click%20on%20an%20image%20you%20will%20have%20access%20also%20to%20a%20very%20useful%20option%20called%0A%20%20%3CSTRONG%3E%0A%20%20%20Deploy%20Image%20to%20Azure%20App%20Service%0A%20%20%3C%2FSTRONG%3E%0A%20%20%3A%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F67518i362EF60695310D9B%22%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20By%20following%20a%20very%20simple%20wizard%20you%20will%20be%20able%20to%20deploy%20very%20quickly%20your%20Docker%20image%20on%20an%20Azure%20App%20Service%2C%20so%20that%20your%20containerized%20application%20can%20be%20hosted%20on%20the%20cloud.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CH3%20id%3D%22wrapping-up%22%20id%3D%22toc-hId--892173251%22%20id%3D%22toc-hId--864350409%22%3E%0A%20%20%20Wrapping%20up%0A%20%20%3C%2FH3%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20In%20this%20post%20we%20made%20a%20step%20forward%20and%20we%20have%20started%20to%20build%20our%20own%20image%2C%20which%20is%20the%20basic%20requirement%20to%20move%20our%20applications%20inside%20a%20container%20and%20deploy%20them.%20For%20learning%20purpose%20we%20have%20used%20a%20.NET%20Core%20website%20and%20we%20have%20leveraged%20the%20default%20ASP.NET%20MVC%20template%20which%20is%20included%20in%20the%20SDK.%0A%20%20%3CBR%20%2F%3E%0A%20%20We%20have%20also%20learned%20how%20to%20update%20an%20image%2C%20how%20to%20push%20it%20on%20Docker%20Hub%20and%20how%20Visual%20Studio%20Code%20can%20help%20us%20in%20creating%20an%20image%20in%20easier%20way.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20In%20the%20next%20post%20we're%20going%20to%20make%20things%20a%20little%20bit%20more%20complicated.%20We're%20going%20to%20build%20a%20REST%20API%20to%20support%20our%20website%20and%20we're%20going%20to%20put%20it%20into%20another%20container.%20Then%20we're%20going%20to%20see%20how%20to%20let%20the%20two%20containers%20communicate%20together.%0A%20%20%3CBR%20%2F%3E%0A%20%20%3CBR%20%2F%3E%0A%20%20Happy%20coding!%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-317567%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Sep%2011%2C%202018%20In%20the%20previous%20post%20we%20have%20learned%20what%20Docker%20is%20and%20which%20are%20the%20benefits%20of%20using%20it%20to%20modernizing%20your%20services%20or%20web%20applications.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-317567%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDocker%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
First published on MSDN on Sep 11, 2018
In the previous post we have learned what Docker is and which are the benefits of using it to modernizing your services or web applications. However, most of the content was theoretical. We started to use the technology, but just to see some basic scenario, like running an application or a web server inside a container.

In this post we're going to get our hands dirty and we'll learn how to take an application we have developed and to store it inside a container.

Let's start with a web application


As first step, we're going to host a web application inside our container. We're going to use .NET Core as a framework, since it fits perfectly into the container world: it's fast, it's cross-platform and it can scale very well.
Let's move out again from our comfort zone and, instead of using Visual Studio, let's use Visual Studio Code and the .NET Core CLI to create our web application.
The main advantages of using this approach are that:

  • Both the CLI and Visual Studio Code are cross-platform. You can follow this tutorial no matter which is the operating system you're using.

  • Visual Studio Code offers a really good Docker extension, which adds Intellisense and a user interface to control your Docker containers and images


After you have downloaded and installed Visual Studio Code from the official website , make sure to install the following extensions from the Marketplace: C# , in order to get C# editing and debugging support, and Docker .

The last piece of the puzzle you need is the .NET Core SDK. You might have it already installed if you have enabled web development in Visual Studio 2017. Otherwise, you can get it from the official website .

Now create a new folder on your PC, which will going to contain our web application. Then open Visual Studio, choose File -> Open Folder and select the folder you have just created. The editor will be opened using your folder as a project. However, for the moment we don't have anything to work with, since the folder is empty.
Let's create a web application. We're going to use the default template available in the .NET Core SDK, which is based on Razor , a programming syntax to build dynamic web applications based on ASP.NET.

To create the base project we're going to use the CLI. The easiest way to access it is to choose Terminal -> New Terminal in Visual Studio Code. A new PowerShell prompt will be opened directly inside the editor, already pointing to the folder you have just created. To create the project just execute the following command:
dotnet new razor

After a few seconds, you will see in the folder appearing all the files which compose the web application.



If you want to test the website, you can just return to the PowerShell prompt and type the following command:
dotnet run

You will see an output like the following one:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp> dotnet run
Using launch settings from C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp\Properties\launchSettings.json...
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using 'C:\Users\mpagani\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
Hosting environment: Development
Content root path: C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Application is shutting down...

The web server which hosts your web application has started. As you can see, it's listening on port 5000 on your local machine. As such, it's enough to open your browser and point it to http://localhost:5000 to see the website running:



If you need a full debugging experience, it's enough in Visual Studio Code to click on the Debug symbol and, in the dropdown menu at the top, press the Play button:



The web server will be started and a new instance of your favorite browser will be automatically opened. This way, you can experience a full debugging session like in Visual Studio 2017: you can place breakpoints; add watches; etc.

Creating our first image


Now that we have a website, we want to move it inside a container, which will host both the web server and our application. The way to do this is to build an image, which we can use to create on or more containers.
When you create an image, you hardly start from scratch. Usually an image is built starting from an existing one, which provides the framework / platform we need to host our application.
In our case, since it's a .NET Core application, we need to build an image which, other than hosting our website, it must include also the .NET Core runtime.

Images are defined by a text file called Dockerfile , which describes all the features that the image must incorporate: which ports to expose; which commands to execute; which is the entry point to launch when the container starts; etc.
There are many commands that you can include in a Dockerfile. You can find the full reference in the official documentation .

A Dockerfile is simply a text file. As such, just click in Visual Studio Code on the icon to add a new file (you'll find it near the name of your project) and name it Dockerfile (without extension). Let's see how our file looks like, then we're going to explain it:
FROM microsoft/dotnet:2.1-sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "WebSample.dll"]
EXPOSE 80

You can notice some uppercase keywords, like FROM , COPY , etc. These are the commands that Docker sends to the service to build the image.

In this specific case, this dockerfile builds the image in two separate steps:

  • The first one takes the whole project and compiles it.

  • The second one takes the output of compilation and runs it.


When you use this approach Docker generates intermediate containers, which are then deleted at the end of the process.

Let's start from the first block:

  1. FROM is the command used to define which image we want to use as a starting point for our own image. As I said, in the first step we are going to build our web application from the source. As such, we need to download the official .NET Core image with the full SDK, since it contains the compilation tool chain. After the FROM statement, we specify the full name of the image ( microsoft/dotnet ), followed by the tag 2.1-sdk . We're going to use, as a starting point, a Linux image with the .NET Core 2.1 SDK. By using the AS command, we assign an alias to the container, so that we can reference it later.

  2. WORKDIR is the command used to define which is the starting folder where we want to operate

  3. COPY is the command used to copy some files from one location to another. When you create an image, you need to pass a build context, which are all the files on your machine that are required to build your app. In our case, we will pass the full content of the folder containing our web project.

  4. RUN is used to execute commands inside the container . By running the command dotnet restore , we force the restore of all the dependencies (like the NuGet packages) required to build the web application.

  5. We use the COPY command again to copy every additional file that might have been downloaded during the restore process.

  6. We use again the RUN command, this time to build our web application. We execute the dotnet publish command, specifying Release as compilation mode (with the -c parameter) and out as folder (inside the container) where to store the build output.


At the end of this process, we have a container which includes the full compiled version of our web application. Now we're ready to generate the final image, which is created in the second part of the file:

  1. We use again the FROM command to define the starting image. In this case, we don't need any more to compile the application, but just to run it. As such, we can grab the runtime image, which is lighter and smaller. We use again the microsoft/dotnet image as a starting point, but this time we specify the tag 2.1-aspnetcore-runtime .

  2. We set again the base dire using the WORKDIR command

  3. If you remember, in the previous step we have assigned the alias build-env to the first container. Thanks to the --from parameter, we can reference it and access to the build output, which is stored in the /app/out folder. By using the COPY command, we copy the content of this folder to the root of the image.

  4. We can see now a new command, called ENTRYPOINT . It's used to specify which is the default command to execute when the container is started. In this case, we lunch the dotnet command followed by the name of the DLL which hosts our website.


Pay attention! Make sure that the name of the DLL exactly matches the one created during the build process, including uppercase and lowercase. We are going to run our image inside a Linux container, which is a case sentive operating system. As such, websample.dll and WebSample.dll are considered two different files.


  1. The last command, called EXPOSE , is used to specify which port will be used to listen to incoming communication inside the container. This is a very important concept to understand. If you want to forward the internal port to the host machine, you will still need to run the container using the -p parameter, like we did in the previous post when we have started a web server using a NGINX container.


Now that we have a proper Dockerfile, we can generate the image using the docker build command. Open again the terminal inside Visual Studio code on the folder which contains your project and run the following command:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\WebSample> docker build -t qmatteoq/websample:latest .
Sending build context to Docker daemon 4.474MB
Step 1/11 : FROM microsoft/dotnet:2.1-sdk AS build-env
---> 7e0402debc11
Step 2/11 : WORKDIR /app
---> Using cache
---> 5d1059fee5f7
Step 3/11 : COPY *.csproj ./
---> Using cache
---> fa5b2af8e3b7
Step 4/11 : RUN dotnet restore
---> Using cache
---> 2df0c6cae62d
Step 5/11 : COPY . ./
---> Using cache
---> f26c6108e9a5
Step 6/11 : RUN dotnet publish -c Release -o out
---> Using cache
---> bab372a643da
Step 7/11 : FROM microsoft/dotnet:2.1-aspnetcore-runtime
---> a6cd65b10ab6
Step 8/11 : WORKDIR /app
---> Using cache
---> 17d4a4528792
Step 9/11 : COPY --from=build-env /app/out .
---> Using cache
---> adceeed37516
Step 10/11 : ENTRYPOINT ["dotnet", "WebSample.dll"]
---> Using cache
---> 1900eef5e657
Step 11/11 : EXPOSE 80
---> Using cache
---> 51b6cb719f13
Successfully built 51b6cb719f13
Successfully tagged qmatteoq/websample:latest

The docker build command requires, at minimum, two parameters:

  • The name of the image you want to create, which is specified using the -t parameter. Notice how I have followed the naming convention username/name of the image:tag.

  • The build context, which is the set of files and folders that are required to build the image. In this case, since we are running the build operation from the same folder that contains our web application, we pass . (dot) as context.


As you can notice, each row of the Dockerfile is treated as a different step, which output is stored locally. This way, if you need to build the image again, the steps that didn't change will be retrieved from the local cache instead of being executed again. You can easily this by running the docker build command again. This time the image will be generated in less than a second, because nothing has changed.

If you've done everything properly, you should be able to see your image listed among the others by running the docker image list command:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\WebSample> docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
qmatteoq/websample latest 51b6cb719f13 About an hour ago 258MB

Now that we have an image, we can easily test it by running a container based on it. We have already seen, in the previous post , how to run a container which must stay active in background, so we can reuse the same knowledge:
docker run -p 8080:80 --rm -d qmatteoq/websample:latest

You can run docker ps to check that the container is indeed up & running:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
NAMES
884e99c66b4c qmatteoq/testwebapp "dotnet TestWebApp.d…" 5 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp youthful_lumiere

We can test it by opening the browser and pointing it to the URL http://localhost:8080 (since we have forwarded the 80 port inside the container to the 8080 port on our host machine).
If you did everything correctly, you should see the same website we have seen before when we have launched it directly from our machine.

Using Visual Studio Code and the Docker extension to make your life easier


Thanks to the Docker extension we have installed at the beginning of the post, building images is a much easier experience. First, because we have support for Intellisense. Go to the Dockerfile you have just created, place the cursor on a new line and press CTRL-SPACE. Visual Studio Code will suggest you all the available commands:



Additionally, if you place the cursor near a command, you will see the documentation popping out, showing the supported syntax. And if you want to create the image starting from the Dockerfile, you don't have to manually run the docker build command, but you can just right click on the file in the Explorer and choose Build Image :



Visual Studio Code will ask you first to specify the name of the image and then, automatically, it will open up a terminal and execute the docker build command with the right parameters.



The Docker extension can even do more than this. Starting from the structure of your project, it's able automatically to generate a Dockerfile for you.
First press CTRL-ALT-P to bring up the list of commands availble and look for one called Docker: Add files to your workspace .
You will be asked a few questions:

  1. The Application Platform. Choose . NET Core Console and not ASP.NET Core. I'll explain in a bit why.

  2. The Operating System. Choose Linux .

  3. The port your app listen on. Leave the default value, which is 80.


The tool will automatically generate two files for you in the folder. One is the Dockerfile, which we already know. The other one is called .dockerignore and it works in a similar way as the .gitignore one used by Git. It contains a lists of files and folder which should be ignored during the build process and not copied over the container, like the Dockerfile itself, log files, Git files, etc.

Here is a sample of the Dockerfile generated by Visual Studio Code:
FROM microsoft/dotnet:2.0-runtime AS base
WORKDIR /app

FROM microsoft/dotnet:2.0-sdk AS build
WORKDIR /src
COPY TestWebApp.csproj TestWebApp/
RUN dotnet restore TestWebApp/TestWebApp.csproj
WORKDIR /src/TestWebApp
COPY . .
RUN dotnet build TestWebApp.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish TestWebApp.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestWebApp.dll"]

Unfortunately the Docker extension isn't really up to date and it isn't leveraging the latest images published by Microsoft. This is the reason why I told you to choose .NET Core Console as application platform, even if we have built an ASP.NET Core website. The ASP.NET Core Dockerfile, in fact, points to an old image that doesn't exist anymore, called aspnetcore . Since a while, in fact, Microsoft has unified all the .NET Core images under the same name ( dotnet ) and it uses tags to differentate among the various workloads.

However, the generated Dockerfile isn't still perfect. There are a couple of changes we need to make:

  1. We have built an application using .NET Core 2.1, but the Dockerfile downloads the image for .NET Core 2.0. As such, we need to change 2.0 to 2.1 inside the tag.

  2. Since this is the Dockerfile for .NET Core console apps, the base image doesn't include all the required libraries to host a website. As such, we need to change the base image from runtime to aspnetcore-runtime .


This is how the fixed Dockerfile looks like:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY TestWebApp.csproj TestWebApp/
RUN dotnet restore TestWebApp/TestWebApp.csproj
WORKDIR /src/TestWebApp
COPY . .
RUN dotnet build TestWebApp.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish TestWebApp.csproj -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestWebApp.dll"]

Pushing an image on Docker Hub


Now that you have an image, you can share it with other people or your customers. The easiest way is to push it on Docker Hub. First, you will need to create an account, by registering on the website . From a billing point of view , Docker Hub works in a similar way as GitHub. With the free plan, you can create unlimited public repostories, but only 1 private repositories. Paid plans, instead, offer a greater number of private repositories. Additionally, Docker Hub supports also a build pipeline. You can point it to a repository which hosts your project's code and have it automatically generate and push a new image every time you make some changes.

Once you have registered, you need to login as first step, by invoking the command docker login . You can also login directly in the Docker for Windows application. This way, the CLI will be automatically able to leverage the same authentication token.

Now you're ready to push your image with a very simple command:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp> docker push qmatteoq/testwebapp
The push refers to repository [docker.io/qmatteoq/testwebapp]
bdc78d020741: Pushed
973c6d532b06: Mounted from qmatteoq/newsfeed
05dff760e6df: Mounted from qmatteoq/newsfeed
85614e3f95f6: Mounted from qmatteoq/newsfeed
9104e47cd398: Mounted from qmatteoq/newsfeed
cdb3f9544e4c: Mounted from qmatteoq/newsfeed
latest: digest: sha256:07c2b0694a1474563912f2cabc807d1927fe925ded1cedae84c0accd0e423b86 size: 1580

By default, unless you specify a different configuration, the docker push command will assume that you want to push the image on Docker Hub. At the end of the process, you'll be able to see your image in your repository on the website:



Your image is now stored in the cloud and ready to be reused. You can easily see this behavior by removing the local copy you have on your machine, by using the docker image rm command, followed by the name of the image.
PS C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp> docker image rm qmatteoq/testwebapp
Untagged: qmatteoq/testwebapp:latest
Untagged: qmatteoq/testwebapp@sha256:07c2b0694a1474563912f2cabc807d1927fe925ded1cedae84c0accd0e423b86
Deleted: sha256:e893220c97a69766a728855d13deafe911f25ee814a5f361f9c250c2a3c7c125
Deleted: sha256:f385d8433f2a5dd139a6e1fa189f4bccde435a3908396cd15ffd4ef2059f1872
Deleted: sha256:abfa55c74332b2322d76e89b32b789eeb853e2486e65f2278679479fa8fee917

Now try to run it again with the docker run command:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp> docker run -p 8080:80 --rm -d qmatteoq/testwebapp
Unable to find image 'qmatteoq/testwebapp:latest' locally
latest: Pulling from qmatteoq/testwebapp
be8881be8156: Already exists
4c13ce9875a3: Already exists
b40109ab8fda: Already exists
a7130f23f49b: Already exists
0b0b625a5a54: Already exists
7cd454006d0b: Pull complete
Digest: sha256:07c2b0694a1474563912f2cabc807d1927fe925ded1cedae84c0accd0e423b86
Status: Downloaded newer image for qmatteoq/testwebapp:latest

You can see how the image wasn't found in the local repository but, since it's now stored in cloud, Docker has been able to pull it anyway.

Updating our website


Let's say that we need to modify our web application and release an update. How we can update the running container? Well, we don't =) As we have seen, one of the benefits of Docker is that containers are really lightweight and fast to start. As such, when you need to update your application, you don't update the running container, but you just update the base image. Then you spin up a new container, using the updated image.

Updating the image is really easy. You just need to run the same docker build command. Since Docker will find that you already have an image with the same name and tag, it will just replace it.

Let's try this by doing a minor change in our website. Open the file Index.cshtml inside the Pages folder of your project and edit something. For example, change the title of the page in the first lines:
@page
@model IndexModel
@{
ViewData["Title"] = "Home page running inside Docker";
}

Now build the image again, either by right clicking on the Dockerfile and by choosing Build Image or by manually running the following command:
docker build -t qmatteoq/websample:latest .

You will notice that, this time, many steps won't be executed from scratch but they will be retrieved from the cache. For example, the dotnet restore step won't be excuted again, because the dependencies leveraged by the web application haven't changed. The COPY and dotnet build steps, instead, will be repeated, because the content of the website has changed and it must be compiled again.

Now run again the image with the following command:
docker run -p 8080:80 --rm -d qmatteoq/testwebapp:latest

Open again your browser and point it to http://localhost:8080 . If you have done everything properly, you should see the same website but with a different title in the tab:


Managing Docker in Visual Studio Code


Once you have installed the Docker extension in Visual Studio Code, you will find a new tab with the Docker logo, which will give you access to a set of helpful visual tools. You will be able, in fact, to explore all the images, running containers and registries on our machine:



You have access to three sections. Images will display all the images you currently have on your local machine. By right clicking on one of them, you'll be able to push it on Docker Hub, run it, delete it, etc. Personally, even if it's very convenient, I rarely use the run command, because it lacks the opportunity to customize the various parameter. For example, you can't specify the port you want to use to expose the service on the host, but it will automatically use the default one exposed inside the container. This means that if you're running a web server on port 80, it will mirror it on port 80 also on your host machine. Sometimes this can be inconvenient, because the port used inside the container can be already occupied on the host.

Containers will display all the running containers. By right clicking on one of them, you'll be able to restart the container, stop it, or completely remove it. Two useful options are Show logs , which will display the last output on the console, and Attach Shell , which will connect to the container using a terminal so that you can perform commands.

For example, if you use Show logs on the container we have previously created, you will see the output of the web server:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp> docker logs -f 8ecf2d283e7a3a8723a6d09222b2aa92829373c81709c51bbac16e06c49ee157
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {a595c102-eabf-4bfd-a35b-eef1b27d0de0} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /app
Now listening on: http://[::]:80
Application started. Press Ctrl+C to shut down.

If you use, instead, Attach Shell , you will be connected to the Linux machine which is hosting the web server and you'll be able to perform UNIX command. For example, if you type ls , you will see all the files which compose your application:
PS C:\Users\mpagani\Source\Samples\NetCoreApi\TestWebApp> docker exec -it 8ecf2d283e7a3a8723a6d09222b2aa92829373c81709c51bbac16e06c49ee157 /bin/sh
# ls
WebSample.Views.dll WebSample.deps.json WebSample.pdb appsettings.Development.json web.config
WebSample.Views.pdb WebSample.dll WebSample.runtimeconfig.json appsettings.json wwwroot
#

The last section is Registries , which gives you access to all your registries, either local or in the cloud. For example, in the previous image, you can see how I can explore my account on Docker Hub and see all the images I've published.
If you right click on an image you will have access also to a very useful option called Deploy Image to Azure App Service :



By following a very simple wizard you will be able to deploy very quickly your Docker image on an Azure App Service, so that your containerized application can be hosted on the cloud.

Wrapping up


In this post we made a step forward and we have started to build our own image, which is the basic requirement to move our applications inside a container and deploy them. For learning purpose we have used a .NET Core website and we have leveraged the default ASP.NET MVC template which is included in the SDK.
We have also learned how to update an image, how to push it on Docker Hub and how Visual Studio Code can help us in creating an image in easier way.

In the next post we're going to make things a little bit more complicated. We're going to build a REST API to support our website and we're going to put it into another container. Then we're going to see how to let the two containers communicate together.

Happy coding!