Dapr and Azure Functions: Part 2 – Azure Functions Containerization in Docker
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
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
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
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!
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:80instructs docker to expose the port 8181 on the container and map that to port 80 on the Function app.
-it cdigs/azurefunctionsimage:v3identifies 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:
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.