Setting Up Flask Applications in Google Cloud Run
Last Updated :
10 Oct, 2024
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.
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.
# 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.
Step 4: Here is the my image under the 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.
Step 6: Here is my sample 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.
Step 8: Here is the revision successfully created on the cloud run and click on the url and access the application.
Step 9: Here is the my sample flask application. Refer the below image.
Step 10: Delete the cloud run by clicking the delete and you can authorizes.
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.
Similar Reads
How to Run a Flask Application After successfully creating a Flask app, we can run it on the development server using the Flask CLI or by running the Python script. Simply execute one of the following commands in the terminal:flask --app app_name runpython app_nameFile StructureHere, we are using the following folder and file.Dem
4 min read
Deploying FastAPI Applications Using Render Deploying FastAPI applications involves making your FastAPI-based web application accessible over the internet or an intranet so that users can interact with it. FastAPI is a modern, fast (hence the name), web framework for building APIs with Python, known for its simplicity and performance. This ar
4 min read
How To Use Web Forms in a Flask Application A web framework called Flask provides modules for making straightforward web applications in Python. It was created using the WSGI tools and the Jinja2 template engine. An example of a micro-framework is Flask. Python web application development follows the WSGI standard, also referred to as web ser
5 min read
Load Balancing Flask Application using Nginx and Docker Load balancing means efficiently distributing the incoming traffic to different server instances. Nginx is open-source software that can be used to apply load balancing to backend systems. Nginx also can be serve services such as reverse proxy, caching, web server, etc.Docker is a tool that gives a
4 min read
How to Deploy a Django Application in Kubernetes In this article, we will study how we can deploy Django web applications to Kubernetes. We will also see how to dockerize and build an image of the Django application using Dockerfile. For this article, you should know about setting up a VM in Azure. We will see how to deploy applications on Linux S
5 min read