How do i use Docker to build Golang Azure functions

raviteja aketi 45 Reputation points
2024-03-04T06:24:56.3233333+00:00

I was using below docker file but couldnot make it work. As golang is a custom one, so how do we install azure function core tools and run the container app.

FROM golang:1.22-alpine AS builder


RUN mkdir /app

ADD . /app

WORKDIR /app

RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o custom_handler custom_handler.go


# Final image based on Azure Functions runtime
FROM mcr.microsoft.com/azure-functions/v3:preview

WORKDIR /home/site/wwwroot

# Copy the compiled binary from the builder stage
COPY --from=builder /app/custom_handler ./

# Set the entrypoint to execute the function app
ENTRYPOINT ["./custom_handler"]

# Expose the port for HTTP triggers
EXPOSE 8080

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. hossein jalilian 10,825 Reputation points Volunteer Moderator
    2024-03-05T02:53:02.1+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    It seems like there might be a couple of issues in your Dockerfile. Here's an updated version:

    WORKDIR /app
    
    # Copy the Go module files
    COPY go.* ./
    
    RUN go mod download
    
    COPY . .
    
    RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o custom_handler .
    
    # Final image based on Azure Functions runtime
    FROM mcr.microsoft.com/azure-functions/go:3.0
    
    WORKDIR /home/site/wwwroot
    
    # Copy the compiled binary from the builder stage
    COPY --from=builder /app/custom_handler ./
    
    # Set the entrypoint to execute the function app
    ENTRYPOINT ["./custom_handler"]
    
    # Expose the port for HTTP triggers
    EXPOSE 8080
    
    
    

    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful


  2. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2024-03-05T16:11:35.99+00:00

    raviteja aketi Thanks for posting your question in Microsoft Q&A. From the description above, you are looking to build Golang function with docker and the question is on installing Azure Function Core Tools.

    We have an Example of an Azure Function App golang custom handler source code and full description about the project is listed here. To install Functions Core Tools, check out Dockerfile which is built from base golang:1.17.6-bullseye and you can update to the latest supported versions. Full detailed instructions are available here (Install the Azure Functions Core Tools). I didn't see alpine is listed as supported but you can give a try with the same instructions or switch to bullseye and validate.

    Note: azure-functions-core-tools-4 for V4 and azure-functions-core-tools-3 for V3

    I hope this helps and let me know if you have any questions or face any errors.


    If you found the answer to your question helpful, please take a moment to mark it as Yes for others to benefit from your experience. Or simply add a comment tagging me and would be happy to answer your questions.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.