GCP, Container Built with Google Cloud Built and Dockerfile isn't listening on port 8080

64 Views Asked by At

I have a cloudbuild job that failed because the container isn't listening on port 8080. But, when I run the container locally, I have no problem accessing it on port 8080. Here are the error, cloudbuild.yaml, and Dockerfile. I need help figuring out where is the problem.

Errors: Filter to find the logs:

resource.type = "cloud_run_revision"
resource.labels.service_name = "icognition-api-scv"
resource.labels.location = "us-central1"
 severity>=DEFAULT

To see the logs in JSON: Cloud Run: https://drive.google.com/file/d/1l_AV4lk0CYYLY49DQnSEtBOMqUPGzr-F/view?usp=sharing

Cloud Build: https://drive.google.com/file/d/1T1o0f8kLjM4oOSxlfHJPiKr43WODYGcY/view?usp=sharing

cloudbuild:

steps:
- id: "Build Docker Image"
  name: 'gcr.io/cloud-builders/docker:latest'
  dir: '.'
  args: ['build', 
          '-t', 'us-central1-docker.pkg.dev/${PROJECT_ID}/icognition-api-artifact-registry/icognition-api-img',
          '.']

- id: "Push Docker Image To Artifact Registry"
  name: 'gcr.io/cloud-builders/docker:latest'
  args: ['push', '--all-tags', 'us-central1-docker.pkg.dev/${PROJECT_ID}/icognition-api-artifact-registry/icognition-api-img']

- id: "Deploy Cloud Run Service"
  name: 'gcr.io/cloud-builders/gcloud:latest'
  args: [
    'run', 'deploy', 'icognition-api-scv', 
    '--service-account' ,'sa-icognition-api-svc@${PROJECT_ID}.iam.gserviceaccount.com',
    '--allow-unauthenticated',
    '--memory', '512Mi',
    '--platform', 'managed',
    '--image', 'us-central1-docker.pkg.dev/${PROJECT_ID}/icognition-api-artifact-registry/icognition-api-img',
    '--port', '8080',
    '--timeout', '30',
    '--min-instances', '0',
    '--max-instances', '1',
    '--ingress', 'all',
    '--region', 'us-central1',
    '--set-secrets', 'DATABASE_URL=icog-db-stg-secret:latest',
    '--network' ,'default',
    '--subnet', 'default',
    '--vpc-egress', 'private-ranges-only'
    ]
timeout: 600s
options:
  logging: CLOUD_LOGGING_ONLY
  dynamic_substitutions: true
  

Dockerfile:

# 
FROM python:3.12-slim

WORKDIR /app

# Install libraries
COPY requirements.txt ./
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

# Copy the app to the container
COPY ./app ./app

ENV HOST 0.0.0.0
EXPOSE 8080

# Run the app
CMD uvicorn app.main:app --host 0.0.0.0 --port 8080
3

There are 3 best solutions below

0
razimbres On

Maybe this solves your problem:

FROM python:3.10

EXPOSE 8080
ENV PORT 8080
... and so on
2
eboraks On

I am answering my question. There were two issues that confused me.

  1. The logs were filtered to Cloud Build, and as a result, I didn't see the errors in the Cloud Run. Lesson: Remove the default filter. In the end the root cause was typo in environment variable name. This error was in the Cloud Run error, not the Cloud Build.
  2. Rebuilding from the history tab, doesn't refetch the cloudbuild.yaml. As result, I had many builds that kept failing because the changes to Cloudbuild were not applied. I needed to trigger the build from the
0
lsalazar On

You need to configure your application to listen on the PORT environment variable. While the EXPOSE command in the Dockerfile signals the ports that a container listens to, it doesn't by itself make those ports accessible externally. Instead, you'll have to ensure that the service hosting your container, such as Cloud Run, routes traffic correctly to the port specified by the PORT environment variable your application uses.

You may also review this documentation - Configure the container port