Back to Blog
Tutorial6 min read2026-05-01

Deploy a Go Application to PandaStack with Docker

Go apps compile to tiny binaries — perfect for container deployments. Here's how to Dockerize a Go app and deploy it to PandaStack in minutes.

# 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 tidy

Your 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.*
*.test

Step 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

  1. 1Push your code (including Dockerfile) to GitHub
  2. 2Log in to [dashboard.pandastack.io](https://dashboard.pandastack.io)
  3. 3Click New DeploymentContainer App
  4. 4Connect your GitHub repository
  5. 5PandaStack detects the Dockerfile automatically
  6. 6Set environment variables:
VariableValue
PORT8080
DATABASE_URLYour PostgreSQL connection string (if needed)
ENVIRONMENTproduction
  1. 1Click Deploy

Step 7: Add a Managed Database (Optional)

If your Go app needs PostgreSQL:

  1. 1Go to DatabasesNew DatabasePostgreSQL
  2. 2Copy the connection string
  3. 3Add it as DATABASE_URL in your deployment's environment variables

Popular Go database libraries:

  • pgx/v5 for PostgreSQL
  • sqlx for SQL with struct scanning
  • gorm for 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 → DomainsAdd 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).

Ready to deploy?

Start free on PandaStack — no credit card required.

Start free on PandaStack

More in Tutorial

Browse all Tutorial articles →

See also