Dockerize your Web App

Introduction

Let’s dockerize our web app! We will be performing all operations using the Azure CLI, and all of our work will be built using Visual Studio Code on Bash On Windows, Linux, Mac OS. If you have not already, make sure you have a .NET Core Web App ready to go.

Prerequisites

Build

NOTE: all command statements with multiple lines ignore the need for a newline escape.

Build and pubish docker image

Dockerfile

First, we need a dockerfile. Minimally, we need a docker file that looks like this.

1
2
3
4
5
6
7
FROM microsoft/aspnetcore
LABEL name="NetCoreHello"
ENTRYPOINT ["dotnet", "NetCoreHello.dll"]
ARG source=bin/Release/netcoreapp1.1/publish
WORKDIR /app
EXPOSE 80
COPY $source .

Build image

Next, let’s create a docker image of our web app.

1
2
$ dotnet publish -c Release
$ docker build -t twitchax/netcorehello:v1 .

If you would like, you can check that your image was built properly.

1
2
3
4
5
6
$ docker run -p 80:80 -it twitchax/netcorehello:v1

Hosting environment: Production
Content root path: /app
Now listening on: http://+:80
Application started. Press Ctrl+C to shut down.

We can verify that our site is working by navigating to http://localhost/api/name/Aaron (or any endpoint in your app).

Publish image

Let’s pubish our image to docker hub (though, you can use any container registry).

1
2
$ docker login
$ docker push twitchax/netcorehello:v1

Create and deploy web app

First, we need to do some setup if you have not already done so (some of these steps may have been completed before, but we need an --is-linux container).

1
2
3
$ az group create -n DemoGroup -l westus
$ az appservice plan create -n DemoPlan -g DemoGroup --location westus --is-linux --sku B1
$ az appservice web create -n DockerNetCoreHello -p DemoPlan -g DemoGroup

Finally, we need to configure our web app to run of our docker image.

1
$ az appservice web config container update --docker-custom-image-name twitchax/netcorehello:v1 -n DockerNetCoreHello -g DemoGroup

Test

You can use whatever method you prefer to test your new web app interaction with DocumentDB. In my case, I am using curl with Bash On Windows.

Get from an endpoint.

1
2
3
4
$ curl http://https://dockernetcorehello.azurewebsites.net/api/hello/Aaron
{
"response": "Hello, Awesome Aaron‽"
}

Done

That’s it! In about 10 minutes, we have dockerized our web app and deployed it to Azure!