skip to Main Content

I have my project wrapped up as docker containers. Please see the Dockerfile:

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt /app/

RUN pip install --upgrade pip setuptools wheel 
    && pip install -r requirements.txt

COPY . .

CMD ["python", "main.py"]

And below is my ci.yml file

name: CI
on:
  push:
    branches:
      - main
  pull_request:
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  dependencies:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          cache: pip
          python-version: "3.12"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

  deploy:
    needs: [dependencies]
    runs-on: ubuntu-latest
    if: |
      github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && github.ref == 'refs/heads/main')
    env:
      DOCKER_BUILDKIT: 1
    steps:
      - uses: actions/checkout@v4

      - name: Set up Google Cloud SDK
        uses: google-github-actions/setup-gcloud@v1
        with:
          project_id: service-a-123456
          service_account_key: ${{ secrets.GOOGLE_AUTHENTICATION_CREDENTIALS_JSON }}
          export_default_credentials: true  

      - name: Authenticate with Google Cloud
        uses: google-github-actions/auth@v1
        with:
          credentials_json: ${{ secrets.GOOGLE_AUTHENTICATION_CREDENTIALS_JSON }}    

      - name: Authorize Docker push
        run: gcloud auth configure-docker us-west1-docker.pkg.dev
        

      # Docker runtime image
      - name: Build Runtime with Cache
        id: build-runtime-with-cache
        continue-on-error: true
        run: >-
          docker build
          --build-arg COLLECT_STATIC=1
          --build-arg BUILDKIT_INLINE_CACHE=1
          -f Dockerfile
          --cache-from ${{ secrets.RUNTIME_DOCKERIMAGE_URL }}:latest
          -t ${{ secrets.RUNTIME_DOCKERIMAGE_URL }}:${{ github.sha }}
          -t ${{ secrets.RUNTIME_DOCKERIMAGE_URL }}:latest
          .

      - name: Build Runtime with Cache failed -> Build Runtime without Cache
        if: ${{ steps.build-runtime-with-cache.outcome == 'failure' }}
        run: >-
          docker build
          --build-arg COLLECT_STATIC=1
          -f Dockerfile
          -t ${{ secrets.RUNTIME_DOCKERIMAGE_URL }}:${{ github.sha }}
          -t ${{ secrets.RUNTIME_DOCKERIMAGE_URL }}:latest
          .

      - name: Push runtime image to Artifact Registry
        run: docker push --all-tags ${{ secrets.RUNTIME_DOCKERIMAGE_URL }}

      - name: Deploy to Cloud Run
        uses: google-github-actions/deploy-cloudrun@v1
        with:
          service: ${{ secrets.CLOUD_RUN_NAME }}
          image: ${{ secrets.RUNTIME_DOCKERIMAGE_URL }}:${{ github.sha }}
          region: us-west1

Now, when I tried to deploy the container to GCP cloud run. First step push to artifact succeeded. However, when it was deployed to the cloud run, I got errors in the GCP logs:

{
  httpRequest: null
  insertId: "78475d0e111149z00b7b87cc"
  jsonPayload: null
  labels: {3}
  logName: "projects/service-a-123456/logs/run.googleapis.com%2Fstderr"
  operation: null
  payload: "textPayload"
  protoPayload: null
  receiveLocation: "us-west1"
  receiveTimestamp: "2024-11-14T19:25:19.018800003Z"
  resource: {2}
  severity: "DEFAULT"
  sourceLocation: null
  split: null
  textPayload: "python: can't open file '/app/main.py': [Errno 2] No such file or directory"
  timestamp: "2024-11-14T19:25:19.014505Z"
  traceSampled: false
}

As you can see, it clearly said python: can't open file '/app/main.py': [Errno 2] No such file or directory.

I then checked on my local docker by booting the containers with the docker desktop and in the service-a container, I can see the /app/main.py there.

/app/main.py existed in the docker container (local)

Please help. Thanks.

UPDATE: I downloaded the uploaded image and run on my local. And the current folder is "/", under it there is a app folder, which has Dockerfile and main.py. Still don’t know why it’s failed.

2

Answers


  1. You have to start the path / to correspond with the root level and locate it in the folder app where you got the script for main.py. Change the config file as follows:

    CMD ["python",  "app/main.py"]
    

    OR

    CMD ["python", "./app/main.py"]
    

    Hope it helps!

    Login or Signup to reply.
  2. Thank you for the information you’ve provided. I’m glad to share some thoughts to address your problem, and I hope this solution works this time.

    1. If you’re using a .dockerignore file, double-check that it’s not excluding main.py or the directory containing it.

      Common .dockerignore mistakes:

      *.pyc
      __pycache__
      .git
      main.py  # Ensure this is not excluded
      
    2. Install dependencies with no caching (to reduce image sizes and avoids stale dependencies) and update copy application files to Dockerfile.

      FROM python:3.12-slim
      
      WORKDIR /app
      
      COPY requirements.txt /app/
      
      # Install dependencies with no caching
      RUN pip install --upgrade pip setuptools wheel 
          && pip install --no-cache-dir -r requirements.txt
      
      # Copy application files
      COPY . /app
      
      
      CMD ["python", "/app/main.py"] # Explicitly run the main script
      

    By doing this, verify the location of main.py. It must be present in the build context and not excluded by .dockerignore.

    Thank you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search