# Deploy a Go Application to PandaStack with Docker
Go's compiled binaries are small, fast, and perfect for containerized deployments. A multi-stage Docker build produces an image that is often under 20MB — compared to hundreds of megabytes for interpreted language runtimes. PandaStack's container hosting makes deploying these lean images straightforward.
Prerequisites
- A Go application (Go 1.21+) pushed to a GitHub repository
- A PandaStack account at [dashboard.pandastack.io](https://dashboard.pandastack.io)
Step 1: Write Your Go Application
A minimal HTTP server with a health endpoint:
package main
import (
"encoding/json"
"log"
"net/http"
"os"
)
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
mux := http.NewServeMux()
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]string{"message": "Hello from Go on PandaStack"})
})
log.Printf("Starting server on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, mux))
}Step 2: Initialize Go Modules
go mod init github.com/your-org/your-app
go mod tidyYour go.mod and go.sum files should be committed to Git.
Step 3: Write a Multi-Stage Dockerfile
The multi-stage build compiles the binary in a Go runtime container, then copies only the binary into a minimal Alpine image:
# Stage 1: Build the binary
FROM golang:1.22-alpine AS builder
WORKDIR /app
# Copy dependency files first (better cache)
COPY go.mod go.sum ./
RUN go mod download
# Copy source and build
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main .
# Stage 2: Minimal runtime image
FROM alpine:3.19
RUN apk --no-cache add ca-certificates tzdata
WORKDIR /app
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]The -ldflags="-s -w" flags strip debug symbols, reducing binary size by ~30%.
Step 4: Add a .dockerignore File
.git
*.md
.env
.env.*
*.testStep 5: Test the Build Locally
docker build -t my-go-app .
docker run -p 8080:8080 -e PORT=8080 my-go-app
# In another terminal
curl http://localhost:8080/health
# {"status":"ok"}Step 6: Deploy to PandaStack
- 1Push your code (including Dockerfile) to GitHub
- 2Log in to [dashboard.pandastack.io](https://dashboard.pandastack.io)
- 3Click New Deployment → Container App
- 4Connect your GitHub repository
- 5PandaStack detects the Dockerfile automatically
- 6Set environment variables:
| Variable | Value |
|---|---|
PORT | 8080 |
DATABASE_URL | Your PostgreSQL connection string (if needed) |
ENVIRONMENT | production |
- 1Click Deploy
Step 7: Add a Managed Database (Optional)
If your Go app needs PostgreSQL:
- 1Go to Databases → New Database → PostgreSQL
- 2Copy the connection string
- 3Add it as
DATABASE_URLin your deployment's environment variables
Popular Go database libraries:
pgx/v5for PostgreSQLsqlxfor SQL with struct scanninggormfor ORM-style access
import "github.com/jackc/pgx/v5/pgxpool"
pool, err := pgxpool.New(ctx, os.Getenv("DATABASE_URL"))Step 8: Connect a Custom Domain
Go to your deployment → Domains → Add Domain. Add a CNAME record in your DNS, and SSL is provisioned automatically.
Step 9: Automatic Deploys
Every push to your configured GitHub branch triggers an automatic rebuild and deployment. The Go binary is compiled fresh on each deploy, ensuring you always run the latest code.
Full documentation: [docs.pandastack.io](https://docs.pandastack.io).