Files
docker-docs/engine/examples/dotnetcore.md
Sebastiaan van Stijn 331554f4b3 Fix various links that were generating URLs with .md (#10548)
* Fix incorrect links in compose section

there's a bug causing wrapped links to not work, and replacing
some links to point to the .md file, so that IDE's can check
if the anchors are valid. Also replaced some links to point
to their new location.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

* engine/swarm: update links

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

* Fix various broken links

There's a bug in the "jekyll-relative-links" plugin that causes wrapped links to not work.
Also replacing some links to point to the .md file, so that IDE's can check if the anchors
are valid. Finally, replaced some links to point to their new locations, so that users don't
get redirected..

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-04-01 12:05:50 +01:00

4.1 KiB

description, keywords, title
description keywords title
Create a Docker image by layering your ASP.NET Core app on debian for Linux Containers or with Windows Nano Server containers using a Dockerfile. dockerize, dockerizing, dotnet, .NET, Core, article, example, platform, installation, containers, images, image, dockerfile, build, asp.net, asp.net core Dockerize an ASP.NET Core application

Introduction

This example demonstrates how to dockerize an ASP.NET Core application.

Why build ASP.NET Core?

  • Open-source
  • Develop and run your ASP.NET Core apps cross-platform on Windows, MacOS, and Linux
  • Great for modern cloud-based apps, such as web apps, IoT apps, and mobile backends
  • ASP.NET Core apps can run on .NET Core or on the full .NET Framework
  • Designed to provide an optimized development framework for apps that are deployed to the cloud or run on-premises
  • Modular components with minimal overhead retain flexibility while constructing your solutions

Prerequisites

This example assumes you already have an ASP.NET Core app on your machine. If you are new to ASP.NET you can follow a simple tutorial to initialize a project or clone our ASP.NET Docker Sample.

Create a Dockerfile for an ASP.NET Core application

  1. Create a Dockerfile in your project folder.
  2. Add the text below to your Dockerfile for either Linux or Windows Containers. The tags below are multi-arch meaning they pull either Windows or Linux containers depending on what mode is set in Docker Desktop for Windows. Read more on switching containers.
  3. The Dockerfile assumes that your application is called aspnetapp. Change the Dockerfile to use the DLL file of your project.
FROM mcr.microsoft.com/dotnet/core/sdk:2.2 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 mcr.microsoft.com/dotnet/core/aspnet:2.2
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
  1. To make your build context as small as possible add a .dockerignore file to your project folder and copy the following into it.
bin\
obj\

Build and run the Docker image

  1. Open a command prompt and navigate to your project folder.
  2. Use the following commands to build and run your Docker image:
$ docker build -t aspnetapp .
$ docker run -d -p 8080:80 --name myapp aspnetapp

View the web page running from a container

  • Go to localhost:8080 to access your app in a web browser.
  • If you are using the Nano Windows Container and have not updated to the Windows Creator Update there is a bug affecting how Windows 10 talks to Containers via "NAT" (Network Address Translation). You must hit the IP of the container directly. You can get the IP address of your container with the following steps:
    1. Run docker inspect -f "{% raw %}{{ .NetworkSettings.Networks.nat.IPAddress }}{% endraw %}" myapp
    2. Copy the container IP address and paste into your browser. (For example, 172.16.240.197)

Further reading