* Update samples for 2.0

* Small wording updates

* Small wording updates

* Update READMEs

* Update wording

* Update per feedback

* Update per feedback on --rm

* Fix slash problem for unix

* Update for linker

* Updates

* Update READMEs for uniformity

* Update per feedback

* Add dotnetapp-prod variant for ARM32

* Use dotnet-nightly until rtw

* Add Dockerfile for self-contained for ARM32

* Update instructions for ARM32
This commit is contained in:
Rich Lander 2017-08-14 00:32:00 -07:00 коммит произвёл GitHub
Родитель a984c3eac7
Коммит a474cf55a8
20 изменённых файлов: 416 добавлений и 138 удалений

Просмотреть файл

@ -1,5 +1,4 @@
.NET Core Docker Samples # .NET Core Docker Samples
========================
This repo contains samples that demonstrate various .NET Core Docker configurations, which you can use as the basis of your own Docker images. These samples depend on the [.NET Core Docker images](https://hub.docker.com/r/microsoft/dotnet/) on Docker Hub, provided by the .NET Team at Microsoft. This repo contains samples that demonstrate various .NET Core Docker configurations, which you can use as the basis of your own Docker images. These samples depend on the [.NET Core Docker images](https://hub.docker.com/r/microsoft/dotnet/) on Docker Hub, provided by the .NET Team at Microsoft.
@ -7,56 +6,57 @@ Docker uses [docker/whalesay](https://hub.docker.com/r/docker/whalesay/) as a ge
You can pick the sample that best fits the scenario you are interested in. The instructions for each sample describe how to target [Windows](http://aka.ms/windowscontainers) or Linux Docker images, from Windows, Linux or macOS. You can pick the sample that best fits the scenario you are interested in. The instructions for each sample describe how to target [Windows](http://aka.ms/windowscontainers) or Linux Docker images, from Windows, Linux or macOS.
The samples use .NET Core 2.0. They use Docker [multi-stage build](https://github.com/dotnet/announcements/issues/18) and [multi-arch tags](https://github.com/dotnet/announcements/issues/14) where appropriate.
You need to have the [.NET Core SDK](https://www.microsoft.com/net/download/core#/sdk) and [Git](https://git-scm.com/downloads) and [Docker client 17.06 or newer](https://www.docker.com/products/docker) clients installed to use these samples. You need to have the [.NET Core SDK](https://www.microsoft.com/net/download/core#/sdk) and [Git](https://git-scm.com/downloads) and [Docker client 17.06 or newer](https://www.docker.com/products/docker) clients installed to use these samples.
### Getting Started ## Getting Started
You can run a [sample application](https://hub.docker.com/r/microsoft/dotnet-samples/) (Linux image) that runs from a pre-built image that has been published to Docker Hub. The source of this sample application is the [dotnetapp-prod](dotnetapp-prod) sample. You can run a [sample application](https://hub.docker.com/r/microsoft/dotnet-samples/) that runs from a pre-built image that has been published to Docker Hub. The source of this sample application is the [dotnetapp-prod](dotnetapp-prod) sample.
To run the **Linux* image:
```console ```console
docker run microsoft/dotnet-samples docker run microsoft/dotnet-samples
``` ```
To run the **Windows* image:
```console
docker run microsoft/dotnet-samples:dotnetapp-nanoserver
```
It is recomended to run the sample twice. The second run will not include downloading the image, so is more representative of typical Docker use. It is recomended to run the sample twice. The second run will not include downloading the image, so is more representative of typical Docker use.
Samples ## Samples
-------
The following samples show different ways to use .NET Core images. The following samples show different ways to use .NET Core images.
### Development ### Development
- [dotnetapp-dev](dotnetapp-dev) - This sample is good for development and building since it relies on the .NET Core SDK image. It performs `dotnet` commands on your behalf, reducing the time it takes to create Docker images (assuming you make changes and then test them in a container, iteratively). * [.NET Core Development Sample](dotnetapp-dev) - This sample is good for development and building since it relies on the .NET Core SDK image. It performs `dotnet` commands on your behalf, reducing the time it takes to create Docker images (assuming you make changes and then test them in a container, iteratively).
### Production ### Production
- [dotnetapp-prod](dotnetapp-prod) - This sample is good for production since it relies on the .NET Core Runtime image, not the larger .NET Core SDK image. Most apps only need the runtime, reducing the size of your application image. * [.NET Core Docker Production Sample](dotnetapp-prod) - This sample is good for production since it relies on the .NET Core Runtime image, not the larger .NET Core SDK image. Most apps only need the runtime, reducing the size of your application image.
- [dotnetapp-selfcontained](dotnetapp-selfcontained) - This sample is also good for production scenarios since it relies on an operating system image (without .NET Core). [Self-contained .NET Core apps](https://docs.microsoft.com/dotnet/articles/core/deploying/) include .NET Core as part of the app and not as a centrally installed component in a base image. * [.NET Core self-contained application Docker Production Samp](dotnetapp-selfcontained) - This sample is also good for production scenarios since it relies on an operating system image (without .NET Core). [Self-contained .NET Core apps](https://docs.microsoft.com/dotnet/articles/core/deploying/) include .NET Core as part of the app and not as a centrally installed component in a base image.
- [aspnetapp](aspnetapp) - This samples demonstrates a Dockerized ASP.NET Core Web App. * [ASP.NET Core Docker Production Sample](aspnetapp) - This samples demonstrates a Dockerized ASP.NET Core Web App.
### Multi-Stage Build ### ARM32 / Raspberry Pi
The aspnet, production, and self-contained apps all use [multi-stage build](https://docs.docker.com/engine/userguide/eng-image/multistage-build/) Dockerfiles. This enables the app to build in a .NET SDK container and copy the published output to a lightweight production container that uses only the .NET runtime image as its base image. The Dockerfiles of these samples have two different `FROM` lines because they use different base images in different stages of execution in order to produce the slimmest image for production use. See [Multi-stage build announcement](https://github.com/dotnet/announcements/issues/18).
Notes * [.NET Core Docker Production Sample](dotnetapp-prod) - This sample includes instructions for running a runtime image with Linux on a Raspberry Pi.
-----
The dotnetbot-prod sample is best for production scenarios where there are multiple .NET Core containers being hosted in an environment, to enable sharing of base images, including the .NET Core Runtime. If there is just one or a few .NET Core containers, then the dotnetbot-selfcontained sample could be a better choice. ## Related Repositories
The current tools for creating [self-contained .NET Core apps](https://docs.microsoft.com/dotnet/articles/core/deploying/) is not yet optimal. These tools will be improved so that self-contained apps are *much* smaller and also easier to produce.
There is a pattern of names ("-prod", "-selfcontained") established by the samples. They are not suggested as general purpose names, but are used here almost like tags to differentiate multiple usese of the single dotnetapp sample. There is also a pattern of Dockerfile naming (Dockerfile, Dockerfile.nano). This pattern of naming is only needed in cases where an app is deployed to multiple operating system. If your app has only one configuration, you should just use the default "Dockerfile" naming, which is simpler and skips the need for the Docker `-f` argument.
Related Repositories
--------------------
See the following related Docker Hub repos: See the following related Docker Hub repos:
- [microsoft/dotnet](https://hub.docker.com/r/microsoft/dotnet/) for .NET Core images. * [microsoft/dotnet](https://hub.docker.com/r/microsoft/dotnet/) for .NET Core images.
- [microsoft/dotnet-samples](https://hub.docker.com/r/microsoft/dotnet-samples/) for .NET Core sample images. * [microsoft/dotnet-samples](https://hub.docker.com/r/microsoft/dotnet-samples/) for .NET Core sample images.
- [microsoft/aspnetcore](https://hub.docker.com/r/microsoft/aspnetcore/) for ASP.NET Core images. * [microsoft/aspnetcore](https://hub.docker.com/r/microsoft/aspnetcore/) for ASP.NET Core images.
- [microsoft/aspnet](https://hub.docker.com/r/microsoft/aspnet/) for ASP.NET Web Forms and MVC images. * [microsoft/aspnet](https://hub.docker.com/r/microsoft/aspnet/) for ASP.NET Web Forms and MVC images.
- [microsoft/dotnet-framework](https://hub.docker.com/r/microsoft/dotnet-framework/) for .NET Framework images (for web applications, see microsoft/aspnet). * [microsoft/dotnet-framework](https://hub.docker.com/r/microsoft/dotnet-framework/) for .NET Framework images (for web applications, see microsoft/aspnet).
See the following related GitHub repos: See the following related GitHub repos:
- [microsoft/dotnet-framework-docker-samples](https://github.com/microsoft/dotnet-framework-docker-samples/) for .NET Framework samples. * [dotnet/announcements](https://github.com/dotnet/announcements/issues?q=is%3Aissue+is%3Aopen+label%3ADocker) for Docker-related announcements.
* [microsoft/dotnet-framework-docker-samples](https://github.com/microsoft/dotnet-framework-docker-samples/) for .NET Framework samples.

Просмотреть файл

@ -1,4 +1,4 @@
FROM microsoft/aspnetcore-build:1.1 AS build-env FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app WORKDIR /app
# copy csproj and restore as distinct layers # copy csproj and restore as distinct layers
@ -10,7 +10,7 @@ COPY . ./
RUN dotnet publish -c Release -o out RUN dotnet publish -c Release -o out
# build runtime image # build runtime image
FROM microsoft/aspnetcore:1.1 FROM microsoft/aspnetcore:2.0
WORKDIR /app WORKDIR /app
COPY --from=build-env /app/out . COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"] ENTRYPOINT ["dotnet", "aspnetapp.dll"]

Просмотреть файл

@ -1,36 +1,114 @@
# aspnetapp Sample # ASP.NET Core Docker Production Sample
The aspnetapp sample demonstrates how to Dockerize an ASP.NET Core app. If you'd like to see how to Dockerize a .NET Framework app check out the [.NET Framework Docker samples](https://github.com/Microsoft/dotnet-framework-docker-samples). This ASP.NET Core Docker sample demonstrates a best practice pattern for building Docker images for ASP.NET Core apps for production. The sample works with both Linux and Windows containers.
The instructions assume that you already have [.NET Core 1.1](https://www.microsoft.com/net/download/core#/sdk), [Git](https://git-scm.com/downloads), and [Docker](https://www.docker.com/products/docker) clients installed. They also assume you already know how to target Linux or Windows containers. Do try both image types. You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). For additional tutorials on ASP.NET Core see [ASP.NET Core Getting Started](https://www.asp.net/get-started). The [sample Dockerfile](Dockerfile) creates an ASP.NET Core application Docker image based off of the [ASP.NET Core Runtime Docker base image](https://hub.docker.com/r/microsoft/aspnetcore/).
Instructions It uses the [Docker multi-stage build feature](https://github.com/dotnet/announcements/issues/18) to build the sample in a container based on the larger [ASP.NET Core Build Docker base image](https://hub.docker.com/r/microsoft/aspnetcore-build/) and then copies the final build result into a Docker image based on the smaller [ASP.NET Core Docker Runtime base image](https://hub.docker.com/r/microsoft/aspnetcore/). The build image contains tools that are required to build applications while the runtime image does not.
------------
First, prepare your environment by cloning the repository and navigating to the sample: This sample requires [Docker 17.06](https://docs.docker.com/release-notes/docker-ce) or later of the [Docker client](https://www.docker.com/products/docker). You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). The instructions assume you have the [Git](https://git-scm.com/downloads) client installed.
## Getting the sample
The easiest way to get the sample is by cloning the samples repository with git, using the following instructions. You can also just download the repository (it is small) as a zip from the [.NET Core Docker samples](https://github.com/dotnet/dotnet-docker-samples/) respository.
```console ```console
git clone https://github.com/dotnet/dotnet-docker-samples/ git clone https://github.com/dotnet/dotnet-docker-samples/
cd dotnet-docker-samples/aspnetapp
``` ```
Follow these steps to run the sample locally: ## Build and run the sample with Docker for Linux containers
You can build and run the sample in Docker using Linux containers using the following commands. The instructions assume that you are in the root of the repository.
```console ```console
dotnet restore cd aspnetapp
docker build -t aspnetapp .
docker run -it --rm -p 8000:80 --name aspnetcore_sample aspnetapp
```
After the application starts, visit `http://localhost:8000` in your web browser.
Note: The `-p` argument maps port 8000 on you 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 paramaters.
## Build and run the sample with Docker for Windows containers
You can build and run the sample in Docker using Windows containers using the following commands. The instructions assume that you are in the root of the repository.
```console
cd aspnetapp
docker build -t aspnetapp .
docker run -it --rm --name aspnetcore_sample aspnetapp
```
You must navigate to the container IP (as opposed to http://localhost) in your browser directly when using Windows containers. You can get the IP address of your container with the following steps:
1. Open up another command prompt.
1. Run `docker ps` to see your running containers. The "aspnetcore_sample" container should be there.
1. Run `docker exec aspnetcore_sample ipconfig`.
1. Copy the container IP address and paste into your browser (for example, `172.29.245.43`).
Note: `docker exec` supports identifying containers with name or hash. The name is used above.
See the following example of how to get the IP address of a running Windows container.
```console
C:\git\dotnet-docker-samples\aspnetapp>docker exec aspnetcore_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` runs a new command in a running command. See the [Docker exec reference](https://docs.docker.com/engine/reference/commandline/exec/) for more information on commandline paramaters.
## Build and run the sample locally
You can build and run the sample locally with the [.NET Core 2.0 SDK](https://www.microsoft.com/net/download/core) using the following commands. The commands assume that you are in the root of the repository.
```console
cd aspnetapp
dotnet run dotnet run
``` ```
Follow these steps to run this sample in a Linux or Windows container: After the application starts, visit `http://localhost:8000` in your web browser.
You can produce an application that is ready to deploy to production locally using the following command.
```console ```console
docker build -t aspnetapp . dotnet publish -c release -o published
docker run -d -p 80:80 aspnetapp
``` ```
## View your web page running from your container You can run the application on **Windows** using the following command.
* If you are using a linux container you can simply browse to http://localhost:80 to access your app in a web browser.
* If you are using the Nano [Windows Container](https://docs.docker.com/docker-for-windows/) there is currently a bug that affects how [Windows 10 talks to Containers via "NAT"](https://github.com/Microsoft/Virtualization-Documentation/issues/181#issuecomment-252671828) (Network Address Translation). Today you must hit the IP of the container directly. You can get the IP address of your container with the following steps: ```console
1. Run `docker ps` and copy the hash of your container ID dotnet published\aspnetapp.dll
3. Run `docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" HASH` where `HASH` is replaced with your container ID. ```
4. Copy the container ip address and paste into your browser with port 80. (Ex: 172.16.240.197:80)
You can run the application on **Linux or macOS** using the following command.
```console
dotnet published/aspnetapp.dll
```
Note: The `-c release` argument builds the application in release mode (the default is debug mode). See the [dotnet run reference](https://docs.microsoft.com/dotnet/core/tools/dotnet-run) for more information on commandline parameters.
## Docker Images used in this sample
The following Docker images are used in this sample
* [microsoft/aspnetcore-build:2.0](https://hub.docker.com/r/microsoft/aspnetcore-build)
* [microsoft/aspnetcore:2.0](https://hub.docker.com/r/microsoft/aspnetcore/)
## Related Resources
* [ASP.NET Core Getting Started Tutorials](https://www.asp.net/get-started)
* [.NET Core Production Docker sample](../dotnetapp-prod/README.md)
* [.NET Core Docker samples](../README.md)
* [.NET Framework Docker samples](https://github.com/Microsoft/dotnet-framework-docker-samples)

Просмотреть файл

@ -1,17 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="1.1.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
</Project> </Project>

Просмотреть файл

@ -1,4 +1,4 @@
FROM microsoft/dotnet:1.1-sdk FROM microsoft/dotnet:2.0-sdk
WORKDIR /app WORKDIR /app
# copy csproj and restore as distinct layers # copy csproj and restore as distinct layers

Просмотреть файл

@ -15,7 +15,7 @@ public static class Program
WriteLine(GetBot(message)); WriteLine(GetBot(message));
WriteLine("**Environment**"); WriteLine("**Environment**");
WriteLine($"Platform: .NET Core 1.1"); WriteLine($"Platform: .NET Core 2.0");
WriteLine($"OS: {RuntimeInformation.OSDescription}"); WriteLine($"OS: {RuntimeInformation.OSDescription}");
WriteLine(); WriteLine();
} }

Просмотреть файл

@ -1,30 +1,67 @@
dotnetapp-dev Sample # .NET Core Development Sample
====================
The dotnetapp-dev sample demonstrates how you can build and run the dotnetapp sample using the [.NET Core SDK Docker image](https://hub.docker.com/r/microsoft/dotnet/). It's a great option for iterative development if you want to use a container as your development environment. It's also great way to get started with .NET Core, because of the convenience and simplicity. You don't even need .NET Core installed on your local machine to use it. This .NET Core Docker sample demonstrates how to use Docker in your .NET Core development process. The sample works with both Linux and Windows containers.
The instructions assume that you already have [.NET Core 1.1](https://www.microsoft.com/net/download/core#/sdk), [Git](https://git-scm.com/downloads), and [Docker](https://www.docker.com/products/docker) installed. They also assume you already know how to target Linux or Windows containers. Please do try both image types. You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). The [sample Dockerfile](Dockerfile) creates a .NET Core application image based off of the [.NET Core SDK Docker base image](https://hub.docker.com/r/microsoft/dotnet/). It builds and runs the application with the same image, which is a useful strategy for interative development but not optimal for production. Take a look at the [.NET Core Production Sample](../dotnetapp-prod/README.md) or [ASP.NET Core Production Sample](../aspnetapp/README.md) for production-oriented samples.
Instructions This sample requires [Docker 17.06](https://docs.docker.com/release-notes/docker-ce) or later of the [Docker client](https://www.docker.com/products/docker). You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). The instructions assume you have the [Git](https://git-scm.com/downloads) client installed.
------------
First, prepare your environment by cloning the repository and navigating to the sample: ## Getting the sample
The easiest way to get the sample is by cloning the samples repository with git, using the following instructions. You can also just download the repository (it is small) as a zip from the [.NET Core Docker samples](https://github.com/dotnet/dotnet-docker-samples/) respository.
```console ```console
git clone https://github.com/dotnet/dotnet-docker-samples/ git clone https://github.com/dotnet/dotnet-docker-samples/
cd dotnet-docker-samples/dotnetapp-dev
``` ```
Follow these steps to run this sample locally: ## 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 ```console
dotnet restore cd dotnetapp-dev
docker build -t dotnetapp-dev .
docker run --rm dotnetapp-dev Hello .NET Core from Docker
```
Note: The instructions above work for both Linux and Windows containers. The .NET Core docker images use [multi-arch tags](https://github.com/dotnet/announcements/issues/14), which abstract away different operating system choices for most use-cases.
## Build and run the sample locally
You can build and run the sample locally with the [.NET Core 2.0 SDK](https://www.microsoft.com/net/download/core) using the following instructions. The instructions assume that you are in the root of the repository.
```console
cd dotnetapp-dev
dotnet run Hello .NET Core dotnet run Hello .NET Core
``` ```
Follow these steps to run this sample in a Linux or Windows container environment: You can produce an application that is ready to deploy to production locally using the following command.
```console ```console
docker build -t dotnetapp . dotnet publish -c release -o published
docker run dotnetapp Hello .NET Core from Docker
``` ```
You can run the application on **Windows** using the following command.
```console
dotnet published\dotnetapp.dll
```
You can run the application on **Linux or macOS** using the following command.
```console
dotnet published/dotnetapp.dll
```
Note: The `-c release` argument builds the application in release mode (the default is debug mode). See the [dotnet run reference](https://docs.microsoft.com/dotnet/core/tools/dotnet-run) for more information on commandline parameters.
## Docker Images used in this sample
The following Docker images are used in this sample
* [microsoft/dotnet:2.0-sdk](https://hub.docker.com/r/microsoft/dotnet)
## Related Resources
* [.NET Core Docker samples](../README.md)
* [.NET Framework Docker samples](https://github.com/Microsoft/dotnet-framework-docker-samples)

Просмотреть файл

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

Просмотреть файл

@ -1,4 +1,4 @@
FROM microsoft/dotnet:1.1-sdk AS build-env FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app WORKDIR /app
# copy csproj and restore as distinct layers # copy csproj and restore as distinct layers
@ -10,7 +10,7 @@ COPY . ./
RUN dotnet publish -c Release -o out RUN dotnet publish -c Release -o out
# build runtime image # build runtime image
FROM microsoft/dotnet:1.1-runtime FROM microsoft/dotnet:2.0-runtime
WORKDIR /app WORKDIR /app
COPY --from=build-env /app/out ./ COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "dotnetapp.dll"] ENTRYPOINT ["dotnet", "dotnetapp.dll"]

Просмотреть файл

@ -0,0 +1,16 @@
FROM microsoft/dotnet:2.0-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.0.0-runtime-stretch-arm32v7
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "dotnetapp.dll"]

Просмотреть файл

@ -15,7 +15,7 @@ public static class Program
WriteLine(GetBot(message)); WriteLine(GetBot(message));
WriteLine("**Environment**"); WriteLine("**Environment**");
WriteLine($"Platform: .NET Core 1.1"); WriteLine($"Platform: .NET Core 2.0");
WriteLine($"OS: {RuntimeInformation.OSDescription}"); WriteLine($"OS: {RuntimeInformation.OSDescription}");
WriteLine(); WriteLine();
} }

Просмотреть файл

@ -1,30 +1,99 @@
dotnetapp-prod Sample # .NET Core Docker Production Sample
=====================
The dotnetapp-prod sample demonstrates how you can build and run the dotnetapp sample using the [.NET Core Runtime Docker image](https://hub.docker.com/r/microsoft/dotnet/). It's a great option for creating a Docker image for production. This .NET Core Docker sample demonstrates a best practice pattern for building Docker images for .NET Core apps for production. The sample works with both Linux and Windows containers.
The instructions assume that you already have [.NET Core 1.0](https://www.microsoft.com/net/download/core#/sdk) and [Git](https://git-scm.com/downloads) and [Docker](https://www.docker.com/products/docker) clients installed. They also assume you already know how to target Linux or Windows containers. Do try both image types. You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). The [sample Dockerfile](Dockerfile) creates an .NET Core application Docker image based off of the [.NET Core Runtime Docker base image](https://hub.docker.com/r/microsoft/dotnet/).
Instructions It uses the [Docker multi-stage build feature](https://github.com/dotnet/announcements/issues/18) to build the sample in a container based on the larger [.NET Core SDK Docker base image](https://hub.docker.com/r/microsoft/dotnet/) and then copies the final build result into a Docker image based on the smaller [.NET Core Docker Runtime base image](https://hub.docker.com/r/microsoft/dotnet/). The SDK image contains tools that are required to build applications while the runtime image does not.
------------
First, prepare your environment by cloning the repository and navigating to the sample: This sample requires [Docker 17.06](https://docs.docker.com/release-notes/docker-ce) or later of the [Docker client](https://www.docker.com/products/docker). You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). The instructions assume you have the [Git](https://git-scm.com/downloads) client installed.
## Getting the sample
The easiest way to get the sample is by cloning the samples repository with git, using the following instructions. You can also just download the repository (it is small) as a zip from the [.NET Core Docker samples](https://github.com/dotnet/dotnet-docker-samples/) respository.
```console ```console
git clone https://github.com/dotnet/dotnet-docker-samples/ git clone https://github.com/dotnet/dotnet-docker-samples/
cd dotnet-docker-samples/dotnetapp-prod
``` ```
Follow these steps to run the sample locally: ## 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 ```console
dotnet restore cd dotnetapp-prod
dotnet run Hello .NET Core from Docker docker build -t dotnetapp-prod .
docker run --rm dotnetapp-prod Hello .NET Core from Docker
``` ```
Follow these steps to run this sample in a Linux or Windows container environment: Note: The instructions above work for both Linux and Windows containers. The .NET Core docker images use [multi-arch tags](https://github.com/dotnet/announcements/issues/14), which abstract away different operating system choices for most use-cases.
## Build on Windows or macOS and run the sample with Docker on Linux + ARM32 (Raspberry Pi)
The goal of this section is to create and run a Docker .NET Core runtime-based image on a Raspberry Pi running Linux. The .NET Core SDK does not run on the Linux + ARM32 configuration. As a result, the instructions used for X64 don't work. There are multiple ways to get around this limitation, primarily:
* Build app on X64 and copy via scp (or pscp) to ARM32 device and then build and run a Docker runtime image on the ARM32 device, or
* Build final ARM32 image on Windows, push image to a Docker registry and then pull and run from the ARM32 device.
The second option is only supported on Windows. Linux and macOS user must use the first option. For simplicity, the Windows option is provided below.
The instructions assume that you are in the root of the repository.
Type the following commands in Docker "Linux mode" on Windows. The instructions assume that you have a personal Docker user account called `mydockername`. You will need to change that to your actual docker account name, such as [richlander](https://hub.docker.com/u/richlander/) in the case of the author of this sample. You will also need to create a Docker repo called `dotnetapp-prod-arm32`. You can create new repos in the Docker web UI.
You need to be signed into the Docker client to `docker push` to Docker Hub.
```console ```console
docker build -t dotnetapp . cd dotnetapp-prod
docker run dotnetapp Hello .NET Core from Docker docker build -t dotnetapp-prod-arm32 -f Dockerfile.arm32 .
docker tag dotnetapp-prod-arm32 mydockername/dotnetapp-prod-arm32
docker push
``` ```
Switch to your Raspberry Pi, with Linux and Docker installed. Type the following command.
```console
docker run --rm mydockername/dotnetapp-prod-arm32 Hello .NET Core from Docker
```
## Build and run the sample locally
You can build and run the sample locally with the [.NET Core 2.0 SDK](https://www.microsoft.com/net/download/core) using the following instructions. The instructions assume that you are in the root of the repository.
```console
cd dotnetapp-prod
dotnet run Hello .NET Core
```
You can produce an application that is ready to deploy to production locally using the following command.
```console
dotnet publish -c release -o published
```
You can run the application on **Windows** using the following command.
```console
dotnet published\dotnetapp.dll
```
You can run the application on **Linux or macOS** using the following command.
```console
dotnet published/dotnetapp.dll
```
Note: The `-c release` argument builds the application in release mode (the default is debug mode). See the [dotnet run reference](https://docs.microsoft.com/dotnet/core/tools/dotnet-run) for more information on commandline parameters.
## Docker Images used in this sample
The following Docker images are used in this sample
* [microsoft/dotnet:2.0-sdk](https://hub.docker.com/r/microsoft/dotnet)
* [microsoft/dotnet:2.0-runtime](https://hub.docker.com/r/microsoft/dotnet)
## Related Resources
* [ASP.NET Core Production Docker sample](../aspnetapp/README.md)
* [.NET Core Docker samples](../README.md)
* [.NET Framework Docker samples](https://github.com/Microsoft/dotnet-framework-docker-samples)

Просмотреть файл

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

Просмотреть файл

@ -1,16 +1,17 @@
FROM microsoft/dotnet:1.1-sdk AS build-env FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app WORKDIR /app
# copy csproj and restore as distinct layers # copy csproj and restore as distinct layers
COPY nuget.config ./
COPY *.csproj ./ COPY *.csproj ./
RUN dotnet restore RUN dotnet restore
# copy everything else and build # copy everything else and build
COPY . ./ COPY . ./
RUN dotnet publish -c Release -o out -r debian.8-x64 RUN dotnet publish -c Release -r linux-x64 -o out
# build runtime image # build runtime image
FROM microsoft/dotnet:1.1-runtime-deps FROM microsoft/dotnet:2.0-runtime-deps
WORKDIR /app WORKDIR /app
COPY --from=build-env /app/out ./ COPY --from=build-env /app/out ./
ENTRYPOINT ["./dotnetapp"] ENTRYPOINT ["./dotnetapp"]

Просмотреть файл

@ -0,0 +1,17 @@
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app
# copy csproj and restore as distinct layers
COPY nuget.config ./
COPY *.csproj ./
RUN dotnet restore
# copy everything else and build
COPY . ./
RUN dotnet publish -c Release -r linux-arm -o out
# build runtime image
FROM microsoft/dotnet:2.0.0-runtime-stretch-arm32v7
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "dotnetapp.dll"]

Просмотреть файл

@ -1,13 +1,14 @@
FROM microsoft/dotnet:1.0-sdk AS build-env FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app WORKDIR /app
# copy csproj and restore as distinct layers # copy csproj and restore as distinct layers
COPY nuget.config ./
COPY *.csproj ./ COPY *.csproj ./
RUN dotnet restore RUN dotnet restore
# copy everything else and build # copy everything else and build
COPY . ./ COPY . ./
RUN dotnet publish -c Release -o out -r win7-x64 RUN dotnet publish -c Release -r win-x64 -o out
# build runtime image # build runtime image
FROM microsoft/nanoserver:10.0.14393.1593 FROM microsoft/nanoserver:10.0.14393.1593

Просмотреть файл

@ -15,7 +15,7 @@ public static class Program
WriteLine(GetBot(message)); WriteLine(GetBot(message));
WriteLine("**Environment**"); WriteLine("**Environment**");
WriteLine($"Platform: .NET Core 1.1"); WriteLine($"Platform: .NET Core 2.0");
WriteLine($"OS: {RuntimeInformation.OSDescription}"); WriteLine($"OS: {RuntimeInformation.OSDescription}");
WriteLine(); WriteLine();
} }

Просмотреть файл

@ -1,72 +1,126 @@
dotnetapp-selfcontained Sample # .NET Core self-contained application Docker Production Sample
==============================
The dotnetapp-selfcontained sample demonstrates how you can build and run the dotnetapp sample as a [self-contained .NET Core application](https://docs.microsoft.com/en-us/dotnet/articles/core/deploying/) that relies only on an operating system image (plus dependencies). It's a good option for creating a Docker image for production. This .NET Core Docker sample demonstrates a best practice pattern for building Docker images for [self-contained .NET Core applications](https://docs.microsoft.com/dotnet/core/deploying/). This is the type of image you would want to use if you want the smallest possible container in production and do not see a [benefit from sharing .NET base images between containers](https://docs.docker.com/engine/userguide/storagedriver/imagesandcontainers/) (you would still potentially share lower Docker layers). The sample works with both Linux and Windows containers.
The instructions assume that you already have [.NET Core 1.0](https://www.microsoft.com/net/download/core#/sdk) and [Git](https://git-scm.com/downloads) and [Docker](https://www.docker.com/products/docker) clients installed. They also assume you already know how to target Linux or Windows containers. Do try both image types. You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). This [sample Dockerfile for Linux](Dockerfile) creates an .NET Core application image based off the [.NET Core Runtime Dependencies Docker base image](https://hub.docker.com/r/microsoft/dotnet/), which is based on [Debian 9 (Stretch) base image](https://hub.docker.com/_/debian/).
Instructions This [sample Dockerfile for Windows Nanoserver](Dockerfile.nano) creates an .NET Core application image based off the [Windows Nanoserver base image](https://hub.docker.com/r/microsoft/nanoserver/).
------------
First, prepare your environment by cloning the repository and navigating to the sample: The sample uses the [Docker multi-stage build feature](https://github.com/dotnet/announcements/issues/18) for Linux and Windows to build the sample in a container based on the larger [.NET Core SDK Docker base image](https://hub.docker.com/r/microsoft/dotnet/) and then copies the final build result into a smaller Docker image based on the appropriate base image mentioned above (based whether you are using Windows or Linux containers).
This sample requires [Docker 17.06](https://docs.docker.com/release-notes/docker-ce) or later of the [Docker client](https://www.docker.com/products/docker). You need the latest Windows 10 or Windows Server 2016 to use [Windows containers](http://aka.ms/windowscontainers). The instructions assume you have the [Git](https://git-scm.com/downloads) client installed.
The sample uses an experimental linker for removing code that your final application does not need. The linker helps to produce Docker images that are significantly smaller. The linker is not required and can be removed from the sample or disabled on the commandline if you do not want to use it.
## Getting the sample
The easiest way to get the sample is by cloning the samples repository with git, using the following instructions. You can also just download the repository (it is small) as a zip from the [.NET Core Docker samples](https://github.com/dotnet/dotnet-docker-samples/) respository.
```console ```console
git clone https://github.com/dotnet/dotnet-docker-samples/ git clone https://github.com/dotnet/dotnet-docker-samples/
cd dotnet-docker-samples/dotnetapp-selfcontained
``` ```
Follow these steps to run the sample locally: ## Build and run the sample with Docker for Linux containers
You can build and run the sample in Docker using Linux containers using the following commands. The instructions assume that you are in the root of the repository.
```console ```console
dotnet restore cd dotnetapp-selfcontained
dotnet run Hello .NET Core from Docker docker build -t dotnetapp-selfcontained .
docker run --rm dotnetapp-selfcontained Hello .NET Core from Docker
``` ```
Follow these steps to build and run the sample locally as a self-contained app on Linux: ## Build and run the sample with Docker for Windows containers
You can build and run the sample in Docker using Windows containers using the following commands. The instructions assume that you are in the root of the repository.
```console ```console
dotnet restore cd dotnetapp-selfcontained
dotnet publish -c Release -o out docker build -t dotnetapp-selfcontained -f Dockerfile.nano .
./out/dotnetapp docker run dotnetapp-selfcontained Hello .NET Core from Docker
``` ```
## Build on Windows or macOS and run the sample with Docker on Linux + ARM32 (Raspberry Pi)
Follow these steps to build and run the sample locally as a self-contained app on macOS Sierra: The goal of this section is to create and run a Docker .NET Core runtime-based image on a Raspberry Pi running Linux. The .NET Core SDK does not run on the Linux + ARM32 configuration. As a result, the instructions used for X64 don't work. There are multiple ways to get around this limitation, primarily:
* Build app on X64 and copy via scp (or pscp) to ARM32 device and then build and run a Docker runtime image on the ARM32 device, or
* Build final ARM32 image on Windows, push image to a Docker registry and then pull and run from the ARM32 device.
The second option is only supported on Windows. Linux and macOS user must use the first option. For simplicity, the Windows option is provided below.
The instructions assume that you are in the root of the repository.
Type the following commands in Docker "Linux mode" on Windows. The instructions assume that you have a personal Docker user account called `mydockername`. You will need to change that to your actual docker account name, such as [richlander](https://hub.docker.com/u/richlander/) in the case of the author of this sample. You will also need to create a Docker repo called `dotnetapp-selfcontained-arm32`. You can create new repos in the Docker web UI.
You need to be signed into the Docker client to `docker push` to Docker Hub.
```console ```console
dotnet restore cd dotnetapp-prod
dotnet publish -c Release -o out -r osx.10.11-x64 docker build -t dotnetapp-selfcontained-arm32 -f Dockerfile.arm32 .
./out/dotnetapp docker tag dotnetapp-selfcontained-arm32 mydockername/dotnetapp-selfcontained-arm32
docker push
``` ```
Note: The macOS Sierra instructions should be the same as the Linux instructions. Sierra is not yet fully supported, so requires workarounds until .NET Core is updated. Switch to your Raspberry Pi, with Linux and Docker installed. Type the following command.
Follow these steps to build and run the sample locally as a self-contained app on Windows:
```console ```console
dotnet restore docker run --rm mydockername/dotnetapp-selfcontained-arm32 Hello .NET Core from Docker
dotnet publish -c Release -o out
out\dotnetapp.exe
``` ```
Follow these steps to run this sample in a Linux container: ## Build, Run and Publish the sample locally
You can build and run the sample locally with the [.NET Core 2.0 SDK](https://www.microsoft.com/net/download/core) using the following instructions. The instructions assume that you are in the root of the repository.
```console ```console
docker build -t dotnetapp . cd dotnetapp-selfcontained
docker run dotnetapp Hello .NET Core from Docker dotnet run
``` ```
Follow these steps to run this sample in a Windows container: ### Publishing on Windows
You can publish an application locally that is ready to deploy to production using the following commands.
```console ```console
docker build -t dotnetapp -f Dockerfile.nano . dotnet publish -c release -r win-x64 -o published-selfcontained-win-x64
docker run dotnetapp Hello .NET Core from Docker published-selfcontained-win-x64\dotnetapp.exe
``` ```
Notes Note: The `-c release` argument builds the application in release mode (the default is debug mode). See the [dotnet run reference](https://docs.microsoft.com/dotnet/core/tools/dotnet-run) for more information on commandline parameters.
-----
Self-contained applications are very similar to framework-dependent applications from a source code perspective. They only differ in terms of their reference to the .NET Core metapackage. Framework-dependent applications include a `"type": "platform"' property and standalone apps do not. The commands used to publish both apps are the same. ### Publishing on Linux
Self-contained apps are operating system- and chip-specific after they are published. The -r argument instructs the publish command to select the correct native assets to include. See [.NET Core Runtime IDentifier (RID) catalog](https://docs.microsoft.com/dotnet/articles/core/rid-catalog) for more information. You can publish an application locally that is ready to deploy to production using the following commands.
The `runtimes` node in project.json lists the suppored RIDs for a given app. A large set is listed in this sample to enable it to be uses on multiple operating systems. ```console
dotnet publish -c release -r linux-x64 -o selfcontained-linux-x64
./selfcontained-linux-x64/dotnetapp
```
Note: The `-r` argument specifies which runtime target the application should be built and published for. See the [dotnet run reference](https://docs.microsoft.com/dotnet/core/tools/dotnet-run) for more information on commandline parameters.
Note: You can publish for other architectures with .NET Core. For example, you can publish for `linux-x64` on Windows or macOS. You can use the `linux-arm` runtime ID you are targeting the Raspberry Pi on Linux, for example.
### Publishing on macOS
You can publish an application locally that is ready to deploy to production using the following commands.
```console
dotnet publish -c release -r osx-x64 -o selfcontained-osx-x64
./selfcontained-osx-x64/dotnetapp
```
## Docker Images used in this sample
The following Docker images are used in this sample
* [microsoft/dotnet:2.0-sdk](https://hub.docker.com/r/microsoft/dotnet)
* [microsoft/dotnet:2.0-runtime-deps](https://hub.docker.com/r/microsoft/dotnet)
* [microsoft/nanoserver](https://hub.docker.com/r/microsoft/nanoserver)
## Related Resources
* [Self-contained .NET Core applications](https://docs.microsoft.com/dotnet/core/deploying/)
* [ASP.NET Core Production Docker sample](../aspnetapp/README.md)
* [.NET Core Production Docker sample](../dotnetapp-prod/README.md)
* [.NET Core Docker samples](../README.md)
* [.NET Framework Docker samples](https://github.com/Microsoft/dotnet-framework-docker-samples)

Просмотреть файл

@ -1,10 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0"> <Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeIdentifiers>centos.7-x64; debian.8-x64; ol.7.0-x64; ol.7.1-x64; ol.7.2-x64; opensuse.13.2-x64; osx.10.10-x64; osx.10.11-x64; linuxmint.17-x64; linuxmint.17.1-x64; linuxmint.17.2-x64; linuxmint.17.3-x64; rhel.7.2-x64; ubuntu.14.04-x64; ubuntu.14.10-x64; ubuntu.15.04-x64; ubuntu.15.10-x64; ubuntu.16.04-x64; win7-x64; win7-x86</RuntimeIdentifiers>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
</Project> <PackageReference Include="ILLink.Tasks" Version="0.1.4-preview-906439" />
</ItemGroup>
</Project>

Просмотреть файл

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
</packageSources>
</configuration>