Skip to content

Commit 21ed961

Browse files
Update Dockerfile and documentation for http-server example
Co-authored-by: coolwednesday <[email protected]>
1 parent ff079f9 commit 21ed961

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

examples/http-server/Dockerfile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
# Build stage
2-
FROM golang:1.24 AS build
2+
FROM golang:1.25-alpine AS build
3+
RUN apk add --no-cache build-base
34

45
WORKDIR /src
6+
7+
# Copy go.mod + go.sum first for better caching
8+
COPY go.mod go.sum ./
9+
RUN go mod download
10+
11+
# Copy the rest of the source code
512
COPY . .
6-
RUN go get ./...
7-
RUN go build -ldflags "-linkmode external -extldflags -static" -a -o /app/main main.go
13+
14+
# Move into http-server folder and build the package (not main.go)
15+
WORKDIR /src/examples/http-server
16+
RUN CGO_ENABLED=0 go build -a -o /app/main .
817

918
# Final stage
1019
FROM alpine:3.14
1120
RUN apk add --no-cache tzdata ca-certificates
1221
COPY --from=build /app/main /main
13-
COPY --from=build /src/configs /configs
22+
COPY --from=build /src/examples/http-server/configs /configs
1423
EXPOSE 9000
1524

16-
CMD ["/main"]
25+
CMD ["/main"]

examples/http-server/README.md

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,56 @@
22

33
This GoFr example demonstrates a simple HTTP server which supports Redis and MySQL as datasources.
44

5-
### To run the example follow the steps below:
5+
### To run the example, follow the steps below:
6+
7+
#### 1. Run with Docker Compose (recommended)
8+
9+
From the project root (`/gofr`):
10+
11+
```console
12+
docker compose -f examples/http-server/docker/docker-compose.yml up -d
13+
```
14+
15+
* Explanation:
16+
17+
* `-f examples/http-server/docker/docker-compose.yml` → path to the docker-compose file
18+
* `up -d` → builds (if needed) and runs services in detached mode
19+
20+
---
21+
22+
#### 2. Build & Run Manually (without docker-compose)
23+
24+
##### Build the Docker image
25+
26+
From the project root (`/gofr`):
27+
28+
```console
29+
docker build -f examples/http-server/Dockerfile -t http-server:latest .
30+
```
31+
32+
* Explanation:
33+
34+
* `-f examples/http-server/Dockerfile` → path to the Dockerfile
35+
* `-t http-server:latest` → tag for the Docker image
36+
* `.` → build context (project root; needed for `go.mod` and `go.sum`)
37+
38+
##### Run the Docker container
639

7-
- Run the docker image of the application
840
```console
9-
docker-compose up -d
41+
docker run -p 9000:9000 --name http-server http-server:latest
1042
```
1143

44+
* Explanation:
45+
* `-p 9000:9000` → maps container port 9000 to host port 9000
46+
* `--name http-server` → optional, gives your container a name
47+
48+
* Use **Compose** when you want the whole stack (app + Redis + MySQL + Grafana + Prometheus).
49+
* Use **Docker build/run** when you just want to run the app container alone.
50+
1251
To test the example, follow these steps:
1352

1453
1. Open your browser and navigate to `http://localhost:9000/hello`.
1554
2. To view the GoFr trace, open `https://tracer.gofr.dev` and paste the traceid.
16-
3. To view the Grafana Dashboard open `http://localhost:3000`
17-
55+
3. To access the Grafana Dashboard, open `http://localhost:3000`. The dashboard UI will be displayed. Use the default admin credentials to log in:
56+
- Username: `admin`
57+
- Password: `password`

examples/http-server/docker/docker-compose.yaml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ version: '3.8'
33
services:
44
gofr-http-server:
55
build:
6-
context: ../.
7-
dockerfile: Dockerfile
6+
context: ../../../ # project root (so go.mod and go.sum are included)
7+
dockerfile: examples/http-server/Dockerfile
88
environment:
9-
- TRACE_EXPORTER=gofr
10-
- TRACER_RATIO=0.1
11-
- REDIS_HOST=redisdb
12-
- REDIS_PORT=6379
13-
- DB_HOST=mysqldb
14-
- DB_USER=root
15-
- DB_PASSWORD=password
16-
- DB_NAME=test
17-
- DB_PORT=3306
18-
- DB_DIALECT=mysql
9+
TRACE_EXPORTER: gofr
10+
TRACER_RATIO: 0.1
11+
REDIS_HOST: redisdb
12+
REDIS_PORT: 6379
13+
DB_HOST: mysqldb
14+
DB_USER: root
15+
DB_PASSWORD: password
16+
DB_NAME: test
17+
DB_PORT: 3306
18+
DB_DIALECT: mysql
1919
ports:
2020
- "9000:9000"
2121
- "2121:2121"
@@ -49,8 +49,8 @@ services:
4949
ports:
5050
- "3000:3000"
5151
environment:
52-
- GF_SECURITY_ADMIN_USER=admin
53-
- GF_SECURITY_ADMIN_PASSWORD=password
52+
GF_SECURITY_ADMIN_USER: admin
53+
GF_SECURITY_ADMIN_PASSWORD: password
5454
volumes:
5555
- ./provisioning:/etc/grafana/provisioning
5656
networks:

0 commit comments

Comments
 (0)