{"id":23404740,"url":"https://github.com/aryank-08/dockerflask-gcp","last_synced_at":"2025-04-08T22:47:45.512Z","repository":{"id":268764804,"uuid":"905377739","full_name":"ARYANK-08/dockerFlask-GCP","owner":"ARYANK-08","description":"Flask Deployment using Docker + GCP","archived":false,"fork":false,"pushed_at":"2024-12-18T21:56:21.000Z","size":44,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-08T22:47:39.413Z","etag":null,"topics":["cicd","docker","flask","googlecloud"],"latest_commit_sha":null,"homepage":"https://githubflask-862135488696.us-east1.run.app/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ARYANK-08.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-18T17:45:28.000Z","updated_at":"2025-03-28T14:32:45.000Z","dependencies_parsed_at":"2024-12-18T19:41:01.639Z","dependency_job_id":"4914ae6c-06d6-4d28-bd84-a3bd414d7120","html_url":"https://github.com/ARYANK-08/dockerFlask-GCP","commit_stats":null,"previous_names":["aryank-08/dockerflask-gcp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARYANK-08%2FdockerFlask-GCP","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARYANK-08%2FdockerFlask-GCP/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARYANK-08%2FdockerFlask-GCP/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ARYANK-08%2FdockerFlask-GCP/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ARYANK-08","download_url":"https://codeload.github.com/ARYANK-08/dockerFlask-GCP/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247941718,"owners_count":21022037,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cicd","docker","flask","googlecloud"],"created_at":"2024-12-22T13:15:31.979Z","updated_at":"2025-04-08T22:47:45.495Z","avatar_url":"https://github.com/ARYANK-08.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **Deploying a Flask App to GCP using Docker and CI/CD**\r\n\r\n| **Anime Docker** 🐳 | **Website Live** 🌐 |\r\n|:---:|:---:|\r\n| ![Anime Docker](https://github.com/user-attachments/assets/06088f3b-dd19-4798-b7be-2f5358f179eb) | ![Website Live](https://github.com/user-attachments/assets/5130e4fd-c189-4341-80c0-a0760ddec4d0) |\r\n\r\n### **Step 1: Create `requirements.txt`**\r\nFirst, list the dependencies for your Flask application in a `requirements.txt` file:\r\n\r\n```\r\nFlask==2.0.3\r\n```\r\n\r\n### **Step 2: Create `Dockerfile`**\r\nNext, create a `Dockerfile` to containerize your Flask application. Below is an example:\r\n\r\n```dockerfile\r\nFROM python:3.10-slim-buster\r\n\r\n# Set the working directory inside the container\r\nWORKDIR /todo-app\r\n\r\n# Copy requirements first for caching\r\nCOPY requirements.txt requirements.txt\r\n\r\n# Install dependencies\r\nRUN pip3 install -r requirements.txt\r\n\r\n# Copy all files into the working directory\r\nCOPY . .\r\n\r\n# Command to run the application\r\nCMD [\"python\", \"app.py\"]\r\n```\r\n\r\n![image](https://github.com/user-attachments/assets/c147248e-4421-49c8-a43b-24894bfb3ae9)\r\n\r\n\r\n### **Step 3: Update Flask App to Listen on Port 8080**\r\nIn your `app.py`, ensure your Flask app listens on port `8080`:\r\n\r\n```python\r\nif __name__ == '__main__':\r\n    app.run(host=\"0.0.0.0\", port=8080)  # Hardcoded port 8080\r\n```\r\n\r\n**Error Handling**: If the port is not configured correctly, you may encounter the following error when deploying:\r\n\r\n```\r\nRevision 'flaskimg-00001-nqv' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout.\r\n```\r\n\r\nEnsure your app is configured to listen on port 8080, as GCP expects it.\r\n\r\n### **Step 4: Create a New GCP Project**\r\nCreate a new project in GCP, for example, `flaskdeploydocker`. Obtain the project ID for further use.\r\n\r\n### **Step 5: Enable Cloud Run API and Cloud Build**\r\nYou need to enable Cloud Run and Cloud Build APIs in your project. You can do this in the GCP Console, and ensure billing is enabled.\r\n\r\n### **Step 6: Install Google Cloud CLI**\r\nInstall the `gcloud` CLI tool and initialize it:\r\n\r\n```bash\r\ngcloud init\r\ngcloud config set project flaskdockerdeploy\r\n```\r\n\r\n![cmdgcloud](https://github.com/user-attachments/assets/553c4137-fef8-485f-a7d7-d352ec43f93f)\r\n\r\n\r\n### **Step 7: Create Artifact Repository**\r\nCreate a Docker repository in Google Artifact Registry:\r\n\r\n```bash\r\ngcloud artifacts repositories create flaskdocker --repository-format=docker --location=europe-west4 --description=\"FlaskDockerAPP\" --immutable-tags --async\r\n```\r\n\r\n![Screenshot 2024-12-18 233800](https://github.com/user-attachments/assets/12b36b20-0b96-42fc-8c22-38ed5a45118d)\r\n\r\n\r\n### **Step 8: Authenticate Docker to GCP**\r\nAuthenticate Docker to GCP's Artifact Registry:\r\n\r\n```bash\r\ngcloud auth configure-docker europe-west4-docker.pkg.dev\r\n```\r\n\r\n### **Step 9: Configure IAM Permissions**\r\nIn IAM, assign the `Object Storage Viewer` permission to the service account to allow access to the Artifact Registry.\r\n\r\n### **Step 10: Build and Push Docker Image**\r\nBuild and push the Docker image to Artifact Registry:\r\n\r\n```bash\r\ngcloud builds submit --tag europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/flaskimg:flasktagnew\r\n```\r\n\r\n### **Step 11: Deploy to Cloud Run**\r\n1. Go to the **Cloud Run** console.\r\n2. Select **Deploy Container** -\u003e **Service**.\r\n3. Choose the container image from Artifact Registry.\r\n4. **Allow unauthenticated invocations** if you want your service to be publicly accessible.\r\n\r\n\r\n![Screenshot 2024-12-18 234639](https://github.com/user-attachments/assets/516cf816-c480-4d43-90be-ea05bfd9f7d0)\r\n\r\n![Screenshot 2024-12-18 234413](https://github.com/user-attachments/assets/fcf142e0-170f-4309-8a92-f12d76d295cd)\r\n\r\n### **Step 12: Set up CI/CD Pipeline with GitHub**\r\nTo automate deployments, connect your GitHub repository to GCP and set up a CI/CD pipeline using Cloud Build to push images to Artifact Registry and deploy them to Cloud Run.\r\n\r\n![Screenshot 2024-12-19 025528](https://github.com/user-attachments/assets/a8c26d30-a2fe-4add-9658-cee918947ecd)\r\n\r\n### Steps:\r\n1. **Create a Service Account in GCP (if not already):**\r\n   - Go to **IAM \u0026 Admin** \u003e **Service Accounts**.\r\n   - Create a new service account.\r\n   - Grant the following roles:\r\n     - `Artifact Registry Writer`\r\n     - `Artifact Registry Create-on-Push Writer`\r\n     - `Cloud Run Admin`\r\n     - `Editor`\r\n     - `Logs Writer`\r\n     - `Service Account User`\r\n     - `Storage Object Viewer`\r\n   \r\n2. **Create and Download Service Account Key:**\r\n   - Go to **IAM \u0026 Admin** \u003e **Service Accounts**.\r\n   - Create a key for your service account.\r\n   - Download the key as a JSON file.\r\n\r\n3. **Add Service Account Key to GitHub Secrets:**\r\n   - Go to your GitHub repository.\r\n   - Navigate to **Settings** \u003e **Secrets and Variables** \u003e **Actions**.\r\n   - Add a new secret called `GCP_SA_KEY` and paste the JSON key content there.\r\n\r\n4. **Create `cicd.yaml` for GitHub Actions:**\r\n   In your GitHub repository, create a file `.github/workflows/cicd.yaml` with the following content:\r\n\r\n![image](https://github.com/user-attachments/assets/74b31773-0e33-49c9-acff-0805306bed5a)\r\n\r\n\r\n### Key Points:\r\n- **Service Account Key (`GCP_SA_KEY`)** is stored as a secret in GitHub.\r\n- The GitHub Action triggers on every push to the `main` branch.\r\n- It builds the Docker image and pushes it to **Google Artifact Registry**.\r\n- Finally, it deploys the image to **Google Cloud Run**.\r\n\r\n### Don't Forget To:\r\n1. Replace `your-image-name`, `your-service-name`, and `your-region` with your actual values.\r\n2. Ensure the GCP project ID is stored in GitHub secrets (`GCP_PROJECT_ID`) for better security.\r\n3. This process will automatically deploy your updated Docker image to GCP whenever you push changes to the `main` branch.\r\n\r\nThis setup is ready to automate your CI/CD workflow with GitHub and GCP.\r\n\r\n![Screenshot 2024-12-19 025004](https://github.com/user-attachments/assets/daa47d13-2eae-4fe9-aee7-5e07c2556f23)\r\n\r\n---\r\n\r\n### **Step 13: Run Locally Using Docker**\r\nIf you want to run the application locally using Docker, build and run it using the following commands:\r\n\r\n```bash\r\ndocker build . --tag europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/otherimg:othertag\r\ndocker run -p 8080:8080 europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/otherimg:othertag\r\n```\r\n\r\nthanks maccha, hope it helps you! :)\r\n\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryank-08%2Fdockerflask-gcp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faryank-08%2Fdockerflask-gcp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faryank-08%2Fdockerflask-gcp/lists"}