# ASP.NET MVC Docker Sample

This [sample](Dockerfile) demonstrates how to use ASP.NET MVC and Docker together. See [ASP.NET Docker Sample](../aspnetapp/README.md) and [.NET Framework Console Docker Sample](../dotnetapp/README.md) for using Docker in other scenarios.

The sample builds the application in a container based on the larger [.NET Framework SDK Docker image](../../README.sdk.md). It builds the application and then copies the final build result into a Docker image based on the smaller [ASP.NET Runtime Docker image](../../README.aspnet.md). It uses Docker [multi-stage build](https://github.com/dotnet/announcements/issues/18) and [multi-arch tags](https://github.com/dotnet/announcements/issues/14).

This sample requires the [Docker Desktop for Windows](https://docs.docker.com/desktop/install/windows-install/).

## Try a pre-built ASP.NET Docker Image

You can quickly run a container with a pre-built [sample ASP.NET Docker image](../../README.samples.md), based on the [ASP.NET Docker sample](../aspnetapp/README.md).

Type the following [Docker](https://www.docker.com/products/docker) command:

```console
docker run --name aspnet_sample --rm -it -p 8000:80 mcr.microsoft.com/dotnet/framework/samples:aspnetapp
```

After the application starts, navigate to `http://localhost:8000` in your web browser. You need to navigate to the application via IP address instead of `localhost` for earlier Windows versions, which is demonstrated in [View the ASP.NET app in a running container on Windows](https://github.com/microsoft/dotnet-framework-docker/blob/main/samples/aspnetapp/README.md#view-the-aspnet-app-in-a-running-container-on-windows).

## Getting the sample

The easiest way to get the sample is by cloning the samples repository with [git](https://git-scm.com/downloads), using the following instructions:

```console
git clone https://github.com/microsoft/dotnet-framework-docker/
```

You can also [download the repository as a zip](https://github.com/microsoft/dotnet-framework-docker/archive/main.zip).

## Build and run the sample with Docker

You can build and run the sample in Docker using the following commands. The instructions assume that you are in the root of the repository.

```console
cd samples
cd aspnetmvcapp
docker build --pull -t aspnetmvcapp .
docker run --name aspnetmvc_sample --rm -it -p 8000:80 aspnetmvcapp
```

You should see the following console output as the application starts.

```console
C:\git\dotnet-framework-docker\samples\aspnetapp>docker run --name aspnetmvc_sample --rm -it -p 8000:80 aspnetmvcapp
Service 'w3svc' has been stopped

Service 'w3svc' started
```

After the application starts, navigate to `http://localhost:8000` in your web browser. You need to navigate to the application via IP address instead of `localhost` for earlier Windows versions, which is demonstrated in [View the ASP.NET app in a running container on Windows](https://github.com/microsoft/dotnet-framework-docker/blob/main/samples/aspnetapp/README.md#view-the-aspnet-app-in-a-running-container-on-windows).

Note: The `-p` argument maps port 8000 on your local machine to port 80 in the container (the form of the port mapping is `host:container`). See the [Docker run reference](https://docs.docker.com/engine/reference/commandline/run/) for more information on commandline parameters.

### View the ASP.NET app in a running container on Windows

After the application starts, navigate to the container IP (as opposed to `http://localhost`) in your web browser with the the following instructions:

1. Open up another command prompt.
1. Run `docker exec aspnetmvc_sample ipconfig`.
1. Copy the container IP address and paste into your browser (for example, `172.29.245.43`).

See the following example of how to get the IP address of a running Windows container.

```console
C:\git\dotnet-framework-docker\samples\aspnetmvcapp>docker exec aspnetmvc_sample ipconfig

Windows IP Configuration


Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . : contoso.com
   Link-local IPv6 Address . . . . . : fe80::1967:6598:124:cfa3%4
   IPv4 Address. . . . . . . . . . . : 172.29.245.43
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . : 172.29.240.1
```

Note: [`docker exec`](https://docs.docker.com/engine/reference/commandline/exec/) supports identifying containers with name or hash. The container name is used in the preceding instructions. `docker exec` runs a new command (as opposed to the [entrypoint](https://docs.docker.com/engine/reference/builder/#entrypoint)) in a running container.

Some people prefer using `docker inspect` for this same purpose, as demonstrated in the following example.

```console
C:\git\dotnet-framework-docker\samples\aspnetmvcapp>docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" aspnetcore_sample
172.25.157.148
```

## .NET Resources

More Samples

* [.NET Framework Docker Samples](../README.md)
* [.NET Docker Samples](https://github.com/dotnet/dotnet-docker/blob/main/samples/README.md)

Docs and More Information:

* [.NET Docs](https://docs.microsoft.com/dotnet/)
* [ASP.NET Docs](https://docs.microsoft.com/aspnet/)
* [dotnet/core](https://github.com/dotnet/core) for starting with .NET on GitHub.
* [dotnet/announcements](https://github.com/dotnet/announcements/issues) for .NET announcements.

## Related Docker Hub Repos

.NET Framework:

* [dotnet/framework/sdk](../../README.sdk.md): .NET Framework SDK
* [dotnet/framework/aspnet](../../README.aspnet.md): ASP.NET Web Forms and MVC
* [dotnet/framework/runtime](../../README.runtime.md): .NET Framework Runtime
* [dotnet/framework/wcf](../../README.wcf.md): Windows Communication Foundation (WCF)
* [dotnet/framework/samples](../../README.samples.md): .NET Framework, ASP.NET and WCF Samples

.NET:

* [dotnet](https://github.com/dotnet/dotnet-docker/blob/main/README.md): .NET
* [dotnet/samples](https://github.com/dotnet/dotnet-docker/blob/main/README.samples.md): .NET Samples
