This blog post will guide you through the process of containerizing a simple Go REST API and deploying it to Azure Container Apps. We'll cover everything from creating the Go application to configuring Azure Container Apps for seamless deployment.

Prerequisites

Step 1. Creating the Go REST API

Let's create a simple Go REST API that returns a "Hello, Azure Container Apps!" message.

// main.go
package main

import (
        "fmt"
        "log"
        "net/http"
        "os"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, Azure Container Apps!")
}

func main() {
        http.HandleFunc("/", helloHandler)
        port := os.Getenv("PORT")
        if port == "" {
                port = "8080"
        }
        fmt.Printf("Server listening on port %s\n", port)
        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

Explanation

Step 2. Containerizing the Go Application

Create a `Dockerfile` to containerize the Go application.

# Dockerfile
# Use the official Golang image to create a build artifact.
FROM golang:1.21-alpine AS builder

# Create a working directory inside the container.
WORKDIR /app

# Copy go mod and sum files
COPY go.mod go.sum ./

# Download all dependencies.
RUN go mod download

# Copy the source from the current directory to the Working Directory inside the container
COPY . .

# Build the Go app
RUN go build -o main .

# Use a minimal Alpine Linux image.
FROM alpine:latest

# Set the working directory to /app
WORKDIR /app

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

# Expose port 8080 to the outside world
EXPOSE 8080

# Command to run the executable
CMD ["./main"]

Explanation

Build the docker image

docker build -t go-api:latest

Tag the image and push it to your container registry (replace `<your-registry>` with your Azure Container Registry or Docker Hub name):

docker tag go-api:latest <your-registry>/go-api:latest
docker push <your-registry>/go-api:latest

Step 3. Creating Azure Resources

Use the Azure CLI to create the necessary Azure resources.

az group create --name myResourceGroup --location eastus

Container Apps Environment

az containerapp env create --name myContainerAppEnv --resource-group myResourceGroup --location eastus

Container App

az containerapp create --name go-api-app --resource-group myResourceGroup --environment myContainerAppEnv --image <your-registry>/go-api:latest --target-port 8080 --ingress external --min-replicas 1 --max-replicas 3

Explanation

Step 4. Verifying the Deployment

Once the deployment is complete, you can retrieve the application's URL:

az containerapp show --name go-api-app --resource-group myResourceGroup --query properties.configuration.ingress.fqdn --output tsv

Open the URL in your browser or use `curl` to verify that the API is running.

curl <your-app-url>

You should see the "Hello, Azure Container Apps!" message.

Step 5. Monitoring and Scaling

Azure Container Apps provides built-in monitoring and scaling features. You can view logs, metrics, and configure auto-scaling rules through the Azure portal or the Azure CLI.

Conclusion

This blog post walks you through the steps to build a straightforward Go REST API, package it up with Docker, and launch it on Azure Container Apps. Azure Container Apps makes it a breeze to deploy and manage your containerized applications, so you can concentrate on crafting your APIs. Plus, this approach is super scalable and makes deploying and updating your apps a piece of cake.