Skip to content

Instantly share code, notes, and snippets.

@BernieWhite
Last active June 28, 2017 11:49
Show Gist options
  • Save BernieWhite/ef603cdf37802bd85d79e2f26d9ba721 to your computer and use it in GitHub Desktop.
Save BernieWhite/ef603cdf37802bd85d79e2f26d9ba721 to your computer and use it in GitHub Desktop.
Windows containers demo script: aspnetcore
# STEP : Create docker file in the current path
Set-Content -Path .\dockerfile -Value '# Sample Dockerfile
# Example docker file for helo world
# Select a base image
FROM microsoft/dotnet:latest
# Set metadata
MAINTAINER bernie@contoso.local
# Run .NET Core commands
# Create sample mvc app
RUN dotnet new mvc -o hwapp
WORKDIR hwapp
# Restore packages and build sample
RUN dotnet restore
COPY About.cshtml C:/hwapp/Views/Home/
COPY HomeController.cs C:/hwapp/Controllers/
RUN dotnet build
# Set .NET Core binding
ENV ASPNETCORE_URLS http://+:80
# Set start command for container
CMD [ "dotnet run" ]
'
# Write updated web application files
Set-Content -Path .\About.cshtml -Value '
@model IEnumerable<KeyValuePair<string, string>>
@{
ViewData["Title"] = "About";
}
<h2>@ViewData["Title"]</h2>
<h3>Environment variables</h3>
<table>
<thead>
<th>Name</th>
<th>Value</th>
</thead>
<tbody>
@foreach (var ev in Model)
{
<tr>
<td>@ev.Key</td>
<td>@ev.Value</td>
</tr>
}
</tbody>
</table>
'
Set-Content -Path .\HomeController.cs -Value '
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace hwapp.Controllers
{
public class HomeController : Controller
{
private readonly string _Hostname;
private readonly Dictionary<string, string> _Environment;
public HomeController()
{
_Hostname = Environment.GetEnvironmentVariable("ComputerName");
_Environment = new Dictionary<string, string>();
foreach (var key in Environment.GetEnvironmentVariables().Keys)
{
_Environment.Add((string)key, Environment.GetEnvironmentVariable((string)key));
}
}
public IActionResult Index()
{
ViewData["Hostname"] = _Hostname;
return View();
}
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View(_Environment);
}
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
public IActionResult Error()
{
return View();
}
}
}
'
# STEP : Pull base image
# Pull the dotnet image
docker pull microsoft/dotnet:latest
# STEP : Build hello world app image
# Build a docker container called hwapp, with a dockerfile in the current directory
docker build -t hwapp .
# STEP : Run container
# Run the container on host port 80
docker run -d -p 80:80 --restart=unless-stopped hwapp:latest
# Run the container on host port 8000 using container ip 192.168.235.233
# docker run -d -p 8000:80 --ip=192.168.235.233 --restart=unless-stopped hwapp:latest
# List running containers
docker ps
# STEP : Cleanup
# Stop all running containers
docker stop $(docker ps -q)
# Remove non running containers
docker rm $(docker ps -q -f status=exited)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment