summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikkel Thestrup <mikkel_thestrup@mithe.dk>2025-12-06 23:20:13 +0100
committerMikkel Thestrup <mikkel_thestrup@mithe.dk>2025-12-06 23:20:13 +0100
commitd243b30606673baec988c5cd0a31e450f2794ca4 (patch)
treea9bee187e9b968fe5a13e048a1c222c4d448094c
parent94646c018dfc87a7f1f9e11dc3e85d1af57899f0 (diff)
downloadweb-portfolio-d243b30606673baec988c5cd0a31e450f2794ca4.tar.gz
web-portfolio-d243b30606673baec988c5cd0a31e450f2794ca4.zip
Containerize the webserver
-rw-r--r--Dockerfile18
-rw-r--r--cmd/main.go21
-rw-r--r--docker-compose.yml14
3 files changed, 51 insertions, 2 deletions
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..a5d236c
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,18 @@
+# build
+FROM golang:1.25.4-alpine AS builder
+WORKDIR /app
+COPY go.mod go.sum ./
+RUN go mod download
+COPY . .
+RUN CGO_ENABLED=0 GOOS=linux go build -o main ./cmd/main.go
+
+# run
+FROM alpine:latest
+WORKDIR /app
+COPY --from=builder /app/main .
+COPY --from=builder /app/views ./views
+COPY --from=builder /app/css ./css
+COPY --from=builder /app/js ./js
+
+EXPOSE 8080
+CMD ["./main"]
diff --git a/cmd/main.go b/cmd/main.go
index cd8d4fe..24ba0c9 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -1,14 +1,31 @@
package main
import (
+ "fmt"
"log"
+ "os"
"git.mithe.dk/web-portfolio/internal"
)
func main() {
- server := internal.NewSever()
- if err := server.Start("localhost:42069"); err != nil {
+ port := os.Getenv("PORT")
+ if port == "" {
+ port = "42069"
+ }
+
+ // In Docker, listen on 0.0.0.0; locally use localhost
+ host := os.Getenv("HOST")
+ if host == "" {
+ host = "0.0.0.0"
+ }
+
+ addr := fmt.Sprintf("%s:%s", host, port)
+
+ server := internal.NewServer()
+ log.Printf("Starting server on %s", addr)
+
+ if err := server.Start(addr); err != nil {
log.Fatal(err)
}
}
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..0709354
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,14 @@
+services:
+ web:
+ build:
+ context: .
+ dockerfile: Dockerfile
+ ports:
+ - "8080:8080"
+ environment:
+ - PORT=8080
+ volumes:
+ - ./views:/app/views
+ - ./css:/app/css
+ - ./js:/app/js
+ restart: unless-stopped