Open In App

Setting Up Flask Applications in Google Cloud Run

Last Updated : 10 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Flask is a popular and easy to use Python web framework that is lightweight and flexible. It lets programmers quickly and with minimal code construct web applications. Flask is simple to modify to match the requirements of different project types since it allows you to choose the tools and libraries you want. Despite its simplicity, Flask is scalable and suitable for small projects as well as large-scale applications. The flexibility and modular design offer seamless integration with a broad variety of plugins and extensions. Flask's vibrant community provides developers with an abundance of tools and help, expediting development and debugging. For web development projects of all sizes, Flask is an excellent choice overall because of

Step-by-step to Deploy Flask Application in Google Cloud Run

Step 1: Create the repository. Here is my repository on my GitHub.

 Creating the repository

Step 2: We need to convert the code into the docker image and we need to push this into the repository. For your reference images is attached pushing the image into artifact repository.

Image Pushing
# Use an official Python runtime as a parent image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy the current directory contents into the container at /usr/src/app
COPY . .
# Install any needed dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 5000 available to the world outside this container
EXPOSE 5000
# Define environment variable
ENV FLASK_APP=app.py
# Run app.py when the container launches
CMD ["flask", "run", "--host=0.0.0.0"]
  • FROM python:3.9-slim: This line specifies the base image to use for the Docker image. In this case, it uses the official Python 3.9 slim image as the parent image. The slim variant is smaller in size compared to the full Python image, making it suitable for production environments.
  • WORKDIR /usr/src/app: This line sets the working directory inside the container to /usr/src/app. Subsequent commands will be executed from this directory.
  • COPY . .: This line copies the contents of the current directory (where the Dockerfile is located) into the /usr/src/app directory in the container. This includes the Flask application code and any other necessary files.
  • RUN pip install --no-cache-dir -r requirements.txt: This line installs the Python dependencies specified in the requirements.txt file using pip. The --no-cache-dir option ensures that no cache is used during the installation, reducing the image size.
  • EXPOSE 5000: This line exposes port 5000 on the container to allow communication with the outside world. This is the default port used by Flask applications.
  • ENV FLASK_APP=app.py: This line sets an environment variable FLASK_APP to app.py, indicating the entry point of the Flask application.
  • CMD ["flask", "run", "--host=0.0.0.0"]: This line specifies the command to run when the container is started. It runs the Flask application using the flask run command, listening on all available network interfaces (--host=0.0.0.0).

Step 3: Create the artifact repository into the google cloud here is the sample repository i have created.

Creating artifact repository

Step 4: Here is the my image under the artifact repository.

Image artifact repository

Step 5: Enter into the cloudrun and click on create service later select the our image and arrange the configuration as per our requirements.

Cloudrun Dashboard

Step 6: Here is my sample configuration.

Configuration

Step 7: After successful configuration click on deploy the image was successfully deployed on cloud run with the 5000 port. Refer the below image for logs.

Deploying  image

Step 8: Here is the revision successfully created on the cloud run and click on the url and access the application.

Revision successfully Created

Step 9: Here is the my sample flask application. Refer the below image.

Flask application

Step 10: Delete the cloud run by clicking the delete and you can authorizes.

Deleting cloud run

Conclusion

Creating a Docker image, publishing it to an artifact repository, and setting up a service on Cloud Run are the simple steps involved in deploying a Flask application on Google Cloud Run. Developers can leverage Cloud Run's scalability and flexibility to easily deploy Flask apps by following the above step-by-step guide. Developers can concentrate on creating and refining their apps while leaving the deployment and scaling responsibilities to Google Cloud's platform using Cloud Run's managed infrastructure. All things considered, hosting web apps in a serverless environment may be done easily and effectively by deploying Flask applications on Cloud Run.


Next Article
Article Tags :

Similar Reads