1

I tried to deploy application to cloud run in GCP which succesfuly got executed using docker file.Now,I am setting up CI/CD by using cloudbuild.yaml .I mirrored repo to CSR and created a cloudbuild service and placed cloudbuild.yaml in my repository .When executed from cloudbuild,it throws the following error.

    Status: Downloaded newer image for gcr.io/google.com/cloudsdktool/cloud-sdk:latest
gcr.io/google.com/cloudsdktool/cloud-sdk:latest

Deploying...
Creating Revision...failed
Deployment failedERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. 

Docker file is attached below:

#pulls python 3.7’s image from the docker hub
FROM python:alpine3.7
#copies the flask app into the container
COPY . /app
#sets the working directory
WORKDIR /app
#install each library written in requirements.txt
RUN pip install -r requirements.txt
#exposes port 8080
EXPOSE 8080
#Entrypoint and CMD together just execute the command
#python app.py which runs this file
ENTRYPOINT [ "python" ]
CMD [ "app.py" ]

cloudbuild.yaml:

 steps:
 # Build the container image
 - name: 'gcr.io/cloud-builders/docker'
   args: ['build', '-t', 'gcr.io/projectid/servicename', '.']
 # Push the container image to Container Registry
 - name: 'gcr.io/cloud-builders/docker'
   args: ['push', 'gcr.io/projectid/servicename']
 # Deploy container image to Cloud Run
 - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
   entrypoint: gcloud
   args:
   - 'run'
   - 'deploy'
   - 'phase-2'
   - '--image'
   - 'gcr.io/projectid/servicename'
   - '--region'
   - 'us-central1'
   - '--platform'
   - 'managed'
 images:
 - 'gcr.io/projectid/servicename'.  


 
lakshmi
  • 201
  • 2
  • 13
  • 1
    Your code looks good. What happens if you use Cloud Build only to build your container like that (in your app root directory) `gcloud builds submit --tag gcr.io/projectid/servicename`? and then deploy it to Cloud Run. Does it still work? – guillaume blaquiere Jul 10 '21 at 18:36
  • Yes.it runs and it gets deployed to cloud run .....But when I try to use cloudbuild.yaml and run manual invocation in cloud build(I am trying to setup CI/CD setup via cloudbuild),it fails.it throws the error in cloudbuild history. – lakshmi Jul 10 '21 at 18:55
  • Hmmm, got it. Remove the COMMIT_SHA when you test manually, and have another try. I think I got the issue!! – guillaume blaquiere Jul 10 '21 at 18:56
  • I removed COMMIT_SHA and tried...Still facing the same error.I have updated the code above – lakshmi Jul 10 '21 at 19:09
  • You build the container locally with Docker, you push it on GCR and you can deploy on Cloud Run and it works. But when you use Cloud Build it fails. That's correct? – guillaume blaquiere Jul 10 '21 at 20:30
  • Yes.So what I did till now was - I cloned the gitlab repo directly in cloud shell using few commands, created a docker file ,deployed the app to cloud run using few commands.It got successfully deployed.After that I wanted to set up a CI/CD pipeline.So I wrote cloudbuild.yaml in my local repo and placed my dockerfile also in repo ,mirrored it to GSR ,created cloud build and configured the remote repo and .yaml file .So when I checked the cloud build ,it fails. – lakshmi Jul 10 '21 at 20:48
  • -@guillaumeblaquiere do you think it might be because of any permission issues?I was able to run helloworld app in my personal project account and setup a CI/CD in my personal account with the same cloudbuild.yaml by changing the project id and service name. – lakshmi Jul 10 '21 at 21:09
  • 1
    No, it's not a permission problem. If it was, you will get a 401 or 403 error. Here it's because your container don't start correctly and Cloud Run can't bind the 8080 port of the container with the underlying infrastructure. So, did you have tried to run the container, built with Cloud Build, locally and to check if it works on your workstation? (because, even if the deployment fails on Cloud Run, the container has been built and pushed to GCR!) – guillaume blaquiere Jul 11 '21 at 11:27
  • @guillaumeblaquiere -Got the issue resolved.It was because of the python compatibility issue.I should use pip3 and python3 in the docker file.I think gcr.io image is compatible with python3. – lakshmi Jul 12 '21 at 14:33
  • @guillaumeblaquiere-Also ,I tried verifying the build locally using cloud-build-local --config=[BUILD_CONFIG] [SOURCE_CODE] in my gcloud shell in my directory where I have the code from gitlab.It throws me the error"Could not find evaluate symlinks/Could not find docker file" . Is it possible to use this command in gcloud shell?Or should I install docker locally and try with this command? – lakshmi Jul 12 '21 at 14:37
  • @lakshmi Ey, to make it easier for the community to see how you solved your error, could you post the solution you found as an answer? – Anton L Jul 13 '21 at 07:40

1 Answers1

1

OP got the issue resolved as seen in the comments:

Got the issue resolved.It was because of the python compatibility issue.I should use pip3 and python3 in the docker file.I think gcr.io image is compatible with python3.

Anton L
  • 430
  • 3
  • 11