我正在尝试使用 docker 在 golang 中部署一个应用程序。
我试过docker build -t go-k8s .
然后docker run -it {{image-id}}
# build stage
FROM golang:latest AS builder
RUN mkdir -p /go/src/app
RUN go get github.com/gorilla/mux
WORKDIR /go/src/app
COPY main.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# final stage
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /go/src/app .
CMD ["./main"]
去应用:
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Yay, it works!nWelcome to Leroy Merlin SRE / DevOps Engineer Test App :)")
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", IndexHandler).Methods("GET")
log.Fatal(http.ListenAndServe(":1337", router))
}
谁能告诉我我的错误在哪里,因为无论如何都会发生构建,但 docker run 没有做任何事情。
您没有在容器中发布 Web 服务器使用的端口。相反,做一些类似的事情
docker run -p 1337:1337 go-k8s
建成后。此时,您应该能够从主机访问您的服务:
$ curl localhost:1337
Yay, it works!
Welcome to Leroy Merlin SRE / DevOps Engineer Test App :)