Anime Docker ๐ณ | Website Live ๐ |
---|---|
![]() |
![]() |
First, list the dependencies for your Flask application in a requirements.txt
file:
Flask==2.0.3
Next, create a Dockerfile
to containerize your Flask application. Below is an example:
FROM python:3.10-slim-buster
# Set the working directory inside the container
WORKDIR /todo-app
# Copy requirements first for caching
COPY requirements.txt requirements.txt
# Install dependencies
RUN pip3 install -r requirements.txt
# Copy all files into the working directory
COPY . .
# Command to run the application
CMD ["python", "app.py"]
In your app.py
, ensure your Flask app listens on port 8080
:
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8080) # Hardcoded port 8080
Error Handling: If the port is not configured correctly, you may encounter the following error when deploying:
Revision '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.
Ensure your app is configured to listen on port 8080, as GCP expects it.
Create a new project in GCP, for example, flaskdeploydocker
. Obtain the project ID for further use.
You 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.
Install the gcloud
CLI tool and initialize it:
gcloud init
gcloud config set project flaskdockerdeploy
Create a Docker repository in Google Artifact Registry:
gcloud artifacts repositories create flaskdocker --repository-format=docker --location=europe-west4 --description="FlaskDockerAPP" --immutable-tags --async
Authenticate Docker to GCP's Artifact Registry:
gcloud auth configure-docker europe-west4-docker.pkg.dev
In IAM, assign the Object Storage Viewer
permission to the service account to allow access to the Artifact Registry.
Build and push the Docker image to Artifact Registry:
gcloud builds submit --tag europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/flaskimg:flasktagnew
- Go to the Cloud Run console.
- Select Deploy Container -> Service.
- Choose the container image from Artifact Registry.
- Allow unauthenticated invocations if you want your service to be publicly accessible.
To 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.
-
Create a Service Account in GCP (if not already):
- Go to IAM & Admin > Service Accounts.
- Create a new service account.
- Grant the following roles:
Artifact Registry Writer
Artifact Registry Create-on-Push Writer
Cloud Run Admin
Editor
Logs Writer
Service Account User
Storage Object Viewer
-
Create and Download Service Account Key:
- Go to IAM & Admin > Service Accounts.
- Create a key for your service account.
- Download the key as a JSON file.
-
Add Service Account Key to GitHub Secrets:
- Go to your GitHub repository.
- Navigate to Settings > Secrets and Variables > Actions.
- Add a new secret called
GCP_SA_KEY
and paste the JSON key content there.
-
Create
cicd.yaml
for GitHub Actions: In your GitHub repository, create a file.github/workflows/cicd.yaml
with the following content:
- Service Account Key (
GCP_SA_KEY
) is stored as a secret in GitHub. - The GitHub Action triggers on every push to the
main
branch. - It builds the Docker image and pushes it to Google Artifact Registry.
- Finally, it deploys the image to Google Cloud Run.
- Replace
your-image-name
,your-service-name
, andyour-region
with your actual values. - Ensure the GCP project ID is stored in GitHub secrets (
GCP_PROJECT_ID
) for better security. - This process will automatically deploy your updated Docker image to GCP whenever you push changes to the
main
branch.
This setup is ready to automate your CI/CD workflow with GitHub and GCP.
If you want to run the application locally using Docker, build and run it using the following commands:
docker build . --tag europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/otherimg:othertag
docker run -p 8080:8080 europe-west4-docker.pkg.dev/flaskdockerdeploy/flaskdocker/otherimg:othertag
thanks maccha, hope it helps you! :)