John Balvin Arias
John Balvin Arias

Reputation: 2886

How to build a docker image using cloud build with sdk, in local machine without dying trying it

I'm using cloud build to build a docker image Guiding myself from examples provide at github:

------bin

------pkg

------src

     --cloud.google.com
     --contrib.go.opencensus.io
     --github.com
     --go.opencensus.io
     --golang.org
     --google.golang.org
     --me
        --backend

------cloudbuild.yaml

------Dockerfile

Where all my code is in src -> me -> backend

Cloud build steps .yaml file content is:

    steps:
- name: 'gcr.io/cloud-builders/go'
  args: ['install', 'me/backend']
  env: ['GOPATH=.']
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '--tag=gcr.io/superpack-213022/me/backend', '.']
images: ['gcr.io/superpack-213022/me/backend']

Docker File:

FROM scratch

COPY bin/backend /me/backend

ENTRYPOINT ["/me/backend"]

Gives me this error:

can not find a package golang/x/sys/unix in any of ...

me

Guiding myself from examples provide at documentation:

------bin

------pkg

------src

     --cloud.google.com
     --contrib.go.opencensus.io
     --github.com
     --go.opencensus.io
     --golang.org
     --google.golang.org
     --me
        --backend
          cloudbuild.yaml
          Dockerfile

Where all my code is in src -> me -> backend

Cloud build steps .yaml file content is:

    steps:
- name: 'gcr.io/cloud-builders/go'
  args: ['install', '.']
  env: ['GOPATH=backend']
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '--tag=gcr.io/superpack-213022/backend', '.']
images: ['gcr.io/superpack-213022/backend']

Docker File:

FROM scratch

COPY bin/backend /backend

ENTRYPOINT ["backend"]

give me this error: "can not find package me/backend in any of . and" and a buch of error with the same, it is not able to find my package me2 me3

So anybody knows what is wrong with the configuration? :(

Upvotes: 1

Views: 818

Answers (1)

John Balvin Arias
John Balvin Arias

Reputation: 2886

For users with the same trouble, the big problem is go dependecies args: ['install', 'me/backend'] "install" was the bottleneck that stoped me to acomplish the build, for some reason, "install" does not fetch all dependencies, you need to fetch all dependencies first with this: args: ['get','-d','me/backend/...'], obviusly you change "me/backend" for your repositorie you want to build.

How is my local repositorie setup:

-----bin

------pkg

------src

 --cloud.google.com #dependency
 --contrib.go.opencensus.io #dependency
 --github.com #dependency
 --go.opencensus.io #dependency
 --golang.org #dependency
 --google.golang.org #dependency
 --me #my code
    --backend
       .
       .
    --deploy
      cloudbuild.yaml
      Dockerfile

Also I moved all my code at "src/me" to google cloud repositories

cloudbuild.yaml:
    steps:
    - name: 'gcr.io/cloud-builders/gcloud-slim'
      args: ['source','repos','clone', '[repositorie name]','src/me','--project=[project name]']  #change [repositorie name] and [project name] for your repositorie name and project name respectively
    - name: 'gcr.io/cloud-builders/go'
      args: ['get','-d','me/backend/...']
    - name: 'gcr.io/cloud-builders/go'
      args: ['install', 'me/backend']
      env: ['GOPATH=.']
    - name: 'gcr.io/cloud-builders/docker'
      args: ['build', '--tag=gcr.io/[project name]/me/backend', '.'] #change [project name] with your project name
    images: ['gcr.io/[project name]/me/backend'] #change [project name] with your project name 
    artifacts:
      objects:
        location: 'gs://[your bucket name]/backend/' #change [your bucket name] for your bucket name
        paths: ['./bin/backend']

Dockerfile:

FROM alpine
COPY bin/backend /backend
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
CMD ["/backend"]
RUN chmod 755 /backend

In command line you should(taking my local repositorie example):

cd src/me/deploy
gcloud builds submit .

Upvotes: 1

Related Questions