Skip to content
This repository has been archived by the owner on Mar 1, 2018. It is now read-only.

Add sample to illustrate usage of the Alpine preview images #94

Merged
merged 2 commits into from
Nov 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ The following samples show different ways to use .NET Core images.
* [.NET Core self-contained application Docker Production Sample](dotnetapp-selfcontained) - This sample includes instructions for running a self-contained image with Linux on a Raspberry Pi.
* Related: See [.NET Core on Raspberry Pi](https://github.com/dotnet/core/blob/master/samples/RaspberryPiInstructions.md)

### Alpine Preview

* [.NET Core Docker Alpine Production Sample](dotnetapp-prod-alpine-preview) - This sample illustrates how to use the new lightweight Alpine based .NET Core Runtime image that is currently in preview.

* Related: See [.NET Core Alpine Docker Image announcement](https://github.com/dotnet/dotnet-docker-nightly/issues/500)

## Related Repositories

See the following related Docker Hub repos:
Expand Down
2 changes: 2 additions & 0 deletions dotnetapp-prod-alpine-preview/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
obj/
18 changes: 18 additions & 0 deletions dotnetapp-prod-alpine-preview/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM microsoft/dotnet-nightly:2.1-sdk AS build-env
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
COPY NuGet.config ./
RUN dotnet restore

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


# build runtime image
FROM microsoft/dotnet-nightly:2.1-runtime-alpine
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "dotnetapp.dll"]
25 changes: 25 additions & 0 deletions dotnetapp-prod-alpine-preview/Dockerfile.globalization
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM microsoft/dotnet-nightly:2.1-sdk AS build-env
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.csproj ./
COPY NuGet.config ./
RUN dotnet restore

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


# build runtime image
FROM microsoft/dotnet-nightly:2.1-runtime-alpine

ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT false
RUN apk add --no-cache icu-libs

ENV LC_ALL en_US.UTF-8
ENV LANG en_US.UTF-8

WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "dotnetapp.dll"]
9 changes: 9 additions & 0 deletions dotnetapp-prod-alpine-preview/NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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" />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
71 changes: 71 additions & 0 deletions dotnetapp-prod-alpine-preview/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
using static System.Console;

public static class Program
{
public static void Main(string[] args)
{
string message = "Dotnet-bot: Welcome to using .NET Core!";

if (args.Length > 0)
{
message = String.Join(" ", args);
}

WriteLine(GetBot(message));
WriteLine("**Environment**");
WriteLine($"Platform: .NET Core 2.0");
WriteLine($"OS: {RuntimeInformation.OSDescription}");
WriteLine($"Culture: {CultureInfo.CurrentCulture.DisplayName}");
WriteLine();
}

public static string GetBot(string message)
{
string bot = $"\n {message}";
bot += @"
__________________
\
\
....
....'
....
..........
.............'..'..
................'..'.....
.......'..........'..'..'....
........'..........'..'..'.....
.'....'..'..........'..'.......'.
.'..................'... ......
. ......'......... .....
. ......
.. . .. ......
.... . .......
...... ....... ............
................ ......................
........................'................
......................'..'...... .......
.........................'..'..... .......
........ ..'.............'..'.... ..........
..'..'... ...............'....... ..........
...'...... ...... .......... ...... .......
........... ....... ........ ......
....... '...'.'. '.'.'.' ....
....... .....'.. ..'.....
.. .......... ..'........
............ ..............
............. '..............
...........'.. .'.'............
............... .'.'.............
.............'.. ..'..'...........
............... .'..............
......... ..............
.....

";
return bot;
}
}
56 changes: 56 additions & 0 deletions dotnetapp-prod-alpine-preview/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# .NET Core Docker Alpine Production Sample (Preview)

This .NET Core Docker sample demonstrates a best practice pattern for building Alpine based Docker images for .NET Core apps for production. The primary goal of Alpine is very small deployments. Images can be pulled quicker and will have a smaller attack surface area. The .NET Core Alpine Docker images are currently in preview. See the [.NET Core Alpine Docker Image announcement](https://github.com/dotnet/dotnet-docker-nightly/issues/500) for additional details.

The [sample Dockerfile](Dockerfile) creates an .NET Core application Docker image based off of the [.NET Core Runtime Alpine Preview Docker image](https://hub.docker.com/r/microsoft/dotnet-nightly/).

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.

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
git clone https://github.com/dotnet/dotnet-docker-samples/
```

## 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 dotnetapp-prod-alpine-preview
docker build -t dotnetapp-prod-alpine-preview .
docker run --rm dotnetapp-prod-alpine-preview Hello .NET Core from Docker
```

Note: The instructions above work only with Linux containers.

## Build and run the sample without the Globalization Invariant Mode

The Alpine based .NET Core Runtime Docker image has the [.NET Core 2.0 Globalization Invariant Mode](https://github.com/dotnet/announcements/issues/20) enabled in order to reduce the default size of the image. Use cases that cannot tolerate Globalization Invariant Mode can reset the `DOTNET_SYSTEM_GLOBALIZATION_INVARIANT` environment variable and install the required ICU package. The [Globalization Dockerfile](Dockerfile.globalization) illustrates how this can be done.

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 dotnetapp-prod-alpine-preview
docker build -t dotnetapp-prod-alpine-preview -f Dockerfile.globalization .
docker run --rm dotnetapp-prod-alpine-preview Hello .NET Core from Docker
```

Note: The instructions above work only with Linux containers.

## Docker Images used in this sample

The following Docker images are used in this sample

* [microsoft/dotnet-nightly:2.1-sdk](https://hub.docker.com/r/microsoft/dotnet-nightly)
* [microsoft/dotnet-nightly:2.1-runtime-alpine](https://hub.docker.com/r/microsoft/dotnet-nightly)

## 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)
10 changes: 10 additions & 0 deletions dotnetapp-prod-alpine-preview/dotnetapp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

<PropertyGroup>

<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RuntimeFrameworkVersion>2.1.0-preview1-25919-02</RuntimeFrameworkVersion>
</PropertyGroup>

</Project>