Skip to content

Minimal example on how to ship ASP.NET Core applications using Docker

Notifications You must be signed in to change notification settings

DhyanB/asp-net-core-docker-minimal

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ASP.NET Core and Docker

Minimal example on how to ship ASP.NET Core applications using Docker.

5 minutes Docker introduction

https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/container-docker-introduction/docker-defined

Build the example .NET application

You can do this on any machine having .NET Core and Visual Studio installed.

  • Create empty remote repository /aspnetcoredocker/
  • Clone repository to /aspnetcoredocker/
  • Add .NET specific .gitignore file to filter out Visual Studio temp files, build results, boilerplate etc.
  • Open Visual Studio
  • Create new project
    • Project type: ASP.NET Core API
    • Project name: "AspNetCoreDocker"
    • Project location: /aspnetcoredocker/ (uncheck "create solution folder" to use the existing repo folder)
  • Close Visual Studio
  • Verify that the app is working
    • Open shell and navigate to /aspnetcoredocker/
    • Run dotnet restore and dotnet build
    • Navigate to /aspnetcoredocker/AspNetCoreDocker/
    • Run dotnet run
    • Open http://localhost:49895/api/values in a browser to verify all is working fine
    • Run Ctrl + C to stop the app

Dockerize the application

  • Create the Dockerfile in /aspnetcoredocker/, use Microsofts aspnetcore Docker image from https://hub.docker.com/r/microsoft/aspnetcore/
  • Open /aspnetcoredocker/AspNetCoreDocker/Properties/launchSettings.jsonand replace localhost by 0.0.0.0 in the "applicationUrl, so the container will listen to all IPv4 addresses. If this is set to localhost, you cannot access your application from outside the container. In a real world scenario you might want to override the port defined in launchSettings.json by means of external configuration, instead of having it in the application itself. Ways of doing this include setting ENV ASPNETCORE_URLS http://0.0.0.0:49895 in the Dockerfile or passing parameters to docker run.
  • Run git add ., git commit and git push origin master

Build the Docker image

You can do this on any machine having Docker installed, .NET is not required.

  • Clone repository to /aspnetcoredocker/
  • In a shell, navigate to /aspnetcoredocker/
  • Run docker build -t dhyanb/aspnetcoredocker:0.5 . to build and tag a Docker image using the current folder as context. Replace dhyanb with your registry username and 0.5 with the current version.
  • Run docker push dhyanb/aspnetcoredocker:0.5 to publish the image to a registry (Docker Hub in this case).

Run containers anywhere

You can do this on any machine having Docker installed, .NET is not required.

  • Run docker pull dhyanb/aspnetcoredocker to fetch the image from the registry
  • Run docker run -d -p 8080:49895 -t aspnetcoredocker:0.5 to create and run a container
  • Shell into the container: docker exec -i -t <container-nickname> /bin/bash
  • Run curl 172.17.0.2:49895/api/values (using the container's ip) or curl 127.0.0.1:8080/api/values (using one of the host's IP4v addresses) to test the application.

Run containers in Azure

Summary from https://docs.microsoft.com/en-us/azure/container-instances/container-instances-tutorial-prepare-acr.

Push image to Azure registry

Create an Azure container instance

See also: https://docs.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-create

  • Create a container instance named aspnetdocker from the registry image and set a DNS name label: az container create -g "docker-test-rg" -n "aspnetdocker" --image "<registry-name>.azurecr.io/aspnetcoredocker:0.5" --dns-name-label "aspnetdockerdemo" --ip-address public --ports 80 -l "westus"
  • Check that container is running: az container logs -g "docker-test-rg" -n "aspnetdocker"
  • Output should look like this:
    Using launch settings from /app/AspNetCoreDocker/Properties/launchSettings.json...
    warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
        No XML encryptor configured. Key {88888888-4444-4444-4444-121212121212} may be persisted to storage in unencrypted form.
    Hosting environment: Development
    Content root path: /app/AspNetCoreDocker
    Now listening on: http://0.0.0.0:49895
    Application started. Press Ctrl+C to shut down.
    

Play with the Azure container instance

  • Use az container show -g "docker-test-rg" -n "aspnetdocker" to show the container's properties and its FQDN. In my case the FQDN was aspnetdockerdemo.westus.azurecontainer.io.
  • Use az container exec -g "docker-test-rg" -n "aspnetdocker" --exec-command "/bin/bash" do open a shell in the container
  • Use ip a to find out the internal IPv4 address, e.g. 10.244.83.2
  • Test the application from inside the container using curl 10.244.83.2:49895/api/values
  • [OPEN-ISSUE] Find out how to access the container and its application from the outside. Ideally the example API would be accessable using http://aspnetdockerdemo.westus.azurecontainer.io/api/values. Ideas:
    • Maybe some network configuration on Azure is required to open IPs and/or ports externally?
    • Maybe Azure only allows HTTPS connections from outside, so the container should be created using --ports 443 80
    • Maybe some other Azure service type has to be used to make apps externally available, e.g. have a look at az webapp create and az webapp config container set. See also: https://docs.microsoft.com/en-us/azure/app-service/containers/tutorial-custom-docker-image.

Delete the Azure container instance

  • Remove the container: az container delete -g "docker-test-rg" -n "aspnetdocker"
  • Verify that the container has been deleted: az container list -g "docker-test-rg" --output table

About

Minimal example on how to ship ASP.NET Core applications using Docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages