Boilerplate Flask + GCP Cloud Run + Docker + Gitlab: A Comprehensive Guide
This article was originally published on GitLab and we have brought it to you as a comprehensive guide to help you get started with building your own Flask application on GCP Cloud Run with Docker and Gitlab.
Flask is a popular Python web framework that is known for its simplicity and flexibility. It is widely used for building web applications and APIs. GCP Cloud Run is a fully managed serverless platform that allows you to run your containerized applications with ease. Docker is a containerization platform that allows you to package your application with all its dependencies into a single container. Gitlab is a web-based Git repository manager that provides continuous integration and continuous deployment (CI/CD) capabilities.
In this guide, we will walk you through the process of setting up a Flask application on GCP Cloud Run with Docker and Gitlab. We will cover the following topics:
- Setting up a Flask application
- Creating a Dockerfile for your Flask application
- Deploying your Flask application to GCP Cloud Run
- Setting up CI/CD with Gitlab
Setting up a Flask application
First, you need to create a Flask application. You can create a new Flask application by running the following command:
$ pip install flask
$ mkdir myapp
$ cd myapp
$ touch app.py
Next, open the app.py file and add the following code:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
This code creates a simple Flask application that returns "Hello, World!" when you visit the root URL.
Creating a Dockerfile for your Flask application
Next, you need to create a Dockerfile for your Flask application. The Dockerfile is a text file that contains instructions for building a Docker image. Here's an example Dockerfile for a Flask application:
# Use an official Python runtime as a parent image
FROM python:3.8-slim-buster
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install the required packages
RUN pip install --no-cache-dir -r requirements.txt
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
This Dockerfile starts with an official Python 3.8 image, sets the working directory to /app, copies the contents of the current directory into the container, installs the required packages, exposes port 8080, sets an environment variable, and runs the app.py file when the container launches.
Deploying your Flask application to GCP Cloud Run
Now that you have created a Dockerfile for your Flask application, you can deploy it to GCP Cloud Run. Here are the steps:
- Install the Google Cloud SDK
- Build the Docker image
- Push the Docker image to Google Container Registry
- Deploy the Docker image to GCP Cloud Run
Here's an example command to build the Docker image:
$ docker build -t myapp .
This command builds a Docker image with the tag "myapp".
Next, you need to push the Docker image to Google Container Registry. Here's an example command:
$ docker tag myapp gcr.io/myproject/myapp
$ docker push gcr.io/myproject/myapp
This command tags the Docker image with the registry name and pushes it to Google Container Registry.
Finally, you can deploy the Docker image to GCP Cloud Run. Here's an example command:
$ gcloud run deploy --image gcr.io/myproject/myapp --platform managed
This command deploys the Docker image to GCP Cloud Run on the managed platform.
Setting up CI/CD with Gitlab
Now that you have deployed your Flask application to GCP Cloud Run, you can set up CI/CD with Gitlab. Here are the steps:
- Create a Gitlab repository for your Flask application
- Create a .gitlab-ci.yml file in the root directory of your repository
- Define the stages and jobs for your CI/CD pipeline
- Add the CI/CD variables to your Gitlab project
Here's an example .gitlab-ci.yml file:
image: docker:latest
services:
- docker:dind
stages:
- build
- test
- deploy
build:
stage: build
script:
- docker build -t myapp .
- docker tag myapp gcr.io/myproject/myapp
- echo "$GOOGLE_SERVICE_ACCOUNT" > /tmp/key.json
- gcloud auth activate-service-account --key-file /tmp/key.json
- gcloud config set project myproject
- gcloud auth configure-docker
- docker push gcr.io/myproject/myapp
test:
stage: test
script:
- echo "Running tests"
deploy:
stage: deploy
script:
- gcloud run deploy --image gcr.io/myproject/myapp --platform managed
This .gitlab-ci.yml file defines three stages: build, test, and deploy. The build stage builds the Docker image, tags it, and pushes it to Google Container Registry. The test stage runs any tests that you have defined. The deploy stage deploys the Docker image to GCP Cloud Run.
Finally, you need to add the CI/CD variables to your Gitlab project. Here are the variables that you need to add:
- GOOGLE_SERVICE_ACCOUNT: The JSON key for your GCP service account
- GOOGLE_PROJECT_ID: The ID of your GCP project
Conclusion
In this guide, we have shown you how to set up a Flask application on GCP Cloud Run with Docker and Gitlab. We have covered the basics of Flask, Docker, and GCP Cloud Run, and we have shown you how to deploy your application to GCP Cloud Run and set up CI/CD with Gitlab. We hope that this guide has been helpful to you and that you are now ready to build your own Flask application on GCP Cloud Run with Docker and Gitlab.