首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GCP Quickstart:构建和部署(云运行教程)

GCP Quickstart:构建和部署(云运行教程)
EN

Stack Overflow用户
提问于 2020-07-21 12:12:35
回答 1查看 186关注 0票数 1

从下面的链接:1;,我正在学习如何在Cloud上部署应用程序的教程,而且我一直有错误。详情见下文:

Quickstart:构建和部署

目录:helloworld-shell

目录中的文件:

  • script.sh
  • invoke.go
  • Dockerfile

每个文件的代码

脚本代码

代码语言:javascript
复制
script.sh
#!/bin/sh
echo Hello ${TARGET:=World}!

援引守则:

代码语言:javascript
复制
invoke.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

代码语言:javascript
复制
# 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"]

在云外壳上运行build之后,如下所示:

代码语言:javascript
复制
gcloud builds submit --tag gcr.io/ultra-complex-282611/helloworld

我的产出如下所示:

代码语言:javascript
复制
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"

否则我应该如何指定源文件?

EN

回答 1

Stack Overflow用户

发布于 2020-07-21 18:41:07

我也有过同样的问题。我通过在文档的go标签中显示的helloworld目录中创建go标签文件来解决这个问题,这使得构建得以成功。

代码语言:javascript
复制
module github.com/knative/docs/docs/serving/samples/hello-world/helloworld-go

go 1.13

您应该有以下文件:

代码语言:javascript
复制
Dockerfile  go.mod  invoke.go  script.sh
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63014312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档