23/12/2019 - DOCKER, GO, NGINX
Nginx'i Go HTTP sunucusunun önüne koymak istiyorsanız, aşağıdaki örneği kullanabilirsiniz. Sonunda API'ye 6000
port (Nginx) veya 8080
port (Go) üzerinden erişebilirsiniz.
.
├── cmd
│ └── server.go
├── docker
│ └── dev
│ ├── docker-compose.yaml
│ ├── go
│ │ └── Dockerfile
│ └── nginx
│ ├── Dockerfile
│ └── app.conf
├── go.mod
└── go.sum
package main
import (
"fmt"
"log"
"net/http"
"github.com/google/uuid"
)
func handler(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(uuid.New().String()))
}
func main() {
http.HandleFunc("/", handler)
fmt.Println("Server is running ...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
version: "3.4"
services:
football_api_go:
build:
context: "../.."
dockerfile: "docker/dev/go/Dockerfile"
hostname: "football-api-go"
football_api_nginx:
build:
context: "../.."
dockerfile: "docker/dev/nginx/Dockerfile"
hostname: "football-api-nginx"
ports:
- "6000:80"
depends_on:
- "football_api_go"
#
# STAGE 1: prepare
#
FROM golang:1.13.1-alpine3.10 as prepare
WORKDIR /source
COPY go.mod .
COPY go.sum .
RUN go mod download
#
# STAGE 2: build
#
FROM prepare AS build
COPY . .
RUN CGO_ENABLED=0 go build -ldflags "-s -w" -o bin/server -v cmd/server.go
#
# STAGE 3: run
#
FROM scratch as run
COPY --from=build /source/bin/server /server
EXPOSE 8080
ENTRYPOINT ["/server"]
FROM nginx:1.15.8-alpine
COPY docker/dev/nginx/app.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
server {
listen 80 default_server;
location / {
proxy_set_header X-Request-Id $request_id;
proxy_set_header Host $host;
proxy_pass http://football_api_go:8080/;
}
}
module football-api
go 1.13
require github.com/google/uuid v1.1.1 // indirect
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
$ docker-compose -f docker/dev/docker-compose.yaml up --build
Building football_api_go
Successfully built 74e284b2da5c
Successfully tagged dev_football_api_go:latest
Building football_api_nginx
Successfully built 12eabebd6b5f
Successfully tagged dev_football_api_nginx:latest
Recreating dev_football_api_go_1 ... done
Recreating dev_football_api_nginx_1 ... done
Attaching to dev_football_api_go_1, dev_football_api_nginx_1
football_api_go_1 | Server is running ...
$ curl -i http://0.0.0.0:6000
HTTP/1.1 200 OK
Server: nginx/1.15.8
Date: Thu, 26 Dec 2019 15:13:12 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 36
Connection: keep-alive
96d16744-c128-48ce-b9ff-5ae2fc0e3f05
$ go build -ldflags "-s -w" -o bin/server -v cmd/server.go
command-line-arguments
$ bin/server
Server is running ...
$ curl -i http://0.0.0.0:8080
HTTP/1.1 200 OK
Date: Thu, 26 Dec 2019 15:16:07 GMT
Content-Length: 36
Content-Type: text/plain; charset=utf-8
cecaabd0-d224-45cc-91c2-5c77f9627611