Dapr and Azure Functions: Part 2 – Azure Functions Containerization in Docker
In Part 1, we created the basic Functions Hello World app.
At this point, Dapr and Functions are running in separate processes on the local machine with Dapr acting as a forwarding proxy.
Now our goal is to containerize the whole thing by starting with the Functions app first.
Step 1: Setup Docker Account and Add Dockerfile
if you haven’t already, you need to go into Docker Hub and set up your account. Once you’ve set it up, log into your account on Docker Desktop.
Since we didn’t add a Dockerfile
when we created the Function, we need to do that now with the following command:
func init --docker-only
This will create the default Dockerfile
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env COPY . /src/dotnet-function-app RUN cd /src/dotnet-function-app && \ mkdir -p /home/site/wwwroot && \ dotnet publish *.csproj --output /home/site/wwwroot # To enable ssh & remote debugging on app service change the base image to the one below # FROM mcr.microsoft.com/azure-functions/dotnet:3.0-appservice FROM mcr.microsoft.com/azure-functions/dotnet:3.0 ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ AzureFunctionsJobHost__Logging__Console__IsEnabled=true COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"] |
Step 2: Build Docker Image
I’m following along with the Microsoft article Create a Custom Linux Image.
We need to execute the command:
docker build --tag cdigs/azurefunctionsimage:v3 .
NOTE: Take note of the .
at the end of that command!
Replace cdigs
with your Docker Hub account ID.
This will take docker a bit of time to pull down the necessary image.
Once it’s done, you’ll be able to see it in Docker Desktop:
Step 3: Run Docker Image
We can run the docker image 2 ways: by clicking the Run button in Docker Desktop or executing the following command from Visual Studio:
docker run -p 8181:80 -it cdigs/azurefunctionsimage:v3
Let’s break this down:
docker run -p 8181:80
instructs docker to expose the port 8181 on the container and map that to port 80 on the Function app.-it cdigs/azurefunctionsimage:v3
identifies the image to run
In Visual Studio Code, we should see the following as the Functions runtime starts up:
We can hit the Function endpoint via the URL: http://localhost:8181/api/helloworld
Our Function app is now in a container and conceptually looks like this:
Note that there is no Dapr at this point what-so-ever.
2 Responses
[…] Part 2 – Azure Functions Containerization in Docker […]
[…] In Part 2, we created the Dockerfile and deployed the Functions app to a container. […]