GCP - 快速入门:构建和部署问题

GCP - 快速入门:构建和部署问题

来自以下链接: https://cloud.google.com/run/docs/quickstarts/build-and-deploy#shell_1; 我正在学习有关如何在 Cloud Run 上部署应用程序的教程,但一直出现错误。请参阅以下详细信息:

快速入门:构建和部署

目录:helloworld-shell

目录中的文件:

  • 脚本
  • 调用.go
  • Dockerfile

脚本

#!/bin/sh
echo Hello ${TARGET:=World}!

调用.go

    package main

import (
        "fmt"
        "log"
        "net/http"
        "os"
        "os/exec"
)

func handler(w http.ResponseWriter, r *http.Request) {
        log.Print("helloworld: received a request")

        cmd := exec.CommandContext(r.Context(), "/bin/sh", "script.sh")
        cmd.Stderr = os.Stderr
        out, err := cmd.Output()
        if err != nil {
                w.WriteHeader(500)
        }
        w.Write(out)
}

func main() {
        log.Print("helloworld: starting server...")

        http.HandleFunc("/", handler)

        port := os.Getenv("PORT")
        if port == "" {
                port = "8080"
        }

        log.Printf("helloworld: listening on %s", port)
        log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}

Dockerfile

# Use the official Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.13 as builder

# Create and change to the app directory.
WORKDIR /app

# Retrieve application dependencies using go modules.
# Allows container builds to reuse downloaded dependencies.
COPY go.* ./
RUN go mod download

# Copy local code to the container image.
COPY invoke.go ./

# Build the binary.
# -mod=readonly ensures immutable go.mod and go.sum in container builds.
RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o server

# Use the official Alpine image for a lean production container.
# https://hub.docker.com/_/alpine
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3
RUN apk add --no-cache ca-certificates

# Copy the binary to the production image from the builder stage.
COPY --from=builder /app/server /server
COPY script.sh ./

# Run the web service on container startup.
CMD ["/server"]

在cloud shell上运行build cmd后,如下:

gcloud builds submit --tag gcr.io/ultra-complex-282611/helloworld

我不断收到如下输出:

sunny@cloudshell:~/helloworld-shell (ultra-complex-282611)$ gcloud builds submit --tag gcr.io/ultra-complex-282611/helloworld-shell/script.sh
Creating temporary tarball archive of 3 file(s) totalling 1.8 KiB before compression.
Uploading tarball of [.] to [gs://ultra-complex-282611_cloudbuild/source/1595280001.548751-6f55216d642d438a82392a7ae1688fbe.tgz]
Created [https://cloudbuild.googleapis.com/v1/projects/ultra-complex-282611/builds/ec154f13-cc1e-4082-bcc0-e47804d201cb].
Logs are available at [https://console.cloud.google.com/cloud-build/builds/ec154f13-cc1e-4082-bcc0-e47804d201cb?project=413771885505].
---------------------------------------------------------------------------- REMOTE BUILD OUTPUT ----------------------------------------------------------------------------
starting build "ec154f13-cc1e-4082-bcc0-e47804d201cb"
FETCHSOURCE
Fetching storage object: gs://ultra-complex-282611_cloudbuild/source/1595280001.548751-6f55216d642d438a82392a7ae1688fbe.tgz#1595280009304068
Copying gs://ultra-complex-282611_cloudbuild/source/1595280001.548751-6f55216d642d438a82392a7ae1688fbe.tgz#1595280009304068...
/ [1 files][  1.1 KiB/  1.1 KiB]
Operation completed over 1 objects/1.1 KiB.
BUILD
Already have image (with digest): gcr.io/cloud-builders/docker
                   ***** NOTICE *****
Alternative official `docker` images, including multiple versions across
multiple platforms, are maintained by the Docker Team. For details, please
visit https://hub.docker.com/_/docker.
                ***** END OF NOTICE *****

Sending build context to Docker daemon  5.632kB
Step 1/11 : FROM golang:1.13 as builder
1.13: Pulling from library/golang
e9afc4f90ab0: Already exists
989e6b19a265: Already exists
af14b6c2f878: Already exists
5573c4b30949: Already exists
d4020e2aa747: Already exists
78b4a3dfc225: Pulling fs layer
2ade102f7410: Pulling fs layer
2ade102f7410: Verifying Checksum
2ade102f7410: Download complete
78b4a3dfc225: Verifying Checksum
2ade102f7410: Download complete
78b4a3dfc225: Verifying Checksum
78b4a3dfc225: Download complete
78b4a3dfc225: Pull complete
2ade102f7410: Pull complete
Digest: sha256:ffb07735793859dc30a06503eb4cbc5c9523b1477ac55155c61a2285abd4c89d
Status: Downloaded newer image for golang:1.13
 ---> afae231e0b45
Step 2/11 : WORKDIR /app
 ---> Running in 5f7bae1883f6
Removing intermediate container 5f7bae1883f6
 ---> 3405fccd5cd0
Step 3/11 : COPY go.* ./

COPY failed: no source files were specified
ERROR
ERROR: build step 0 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ERROR: (gcloud.builds.submit) build ec154f13-cc1e-4082-bcc0-e47804d201cb completed with status "FAILURE"

我还应该怎样指定源文件?

答案1

你缺少了步骤 #2教程:创建go.mod文件。

如果您注意到,错误会提到找不到任何go.*文件,这意味着您缺少该go.mod文件。在您的文件结构中,您也没有提到任何go.mod文件。

此外,dockerfile 中的下一步是RUN go mod download,如果没有文件,它也将失败go.mod

有关更多背景信息go.mod,请参阅 golang 博客系列帖子解释概念和用法。

相关内容