Container to Production in less than 5 minutes | Google Cloud Run | Google Cloud Platform
What are Containers?
A container packages up code and all its dependencies so the application can run on any computing environment
What is Cloud Run?
Cloud Run is a managed compute platform that enables you to run stateless containers that can be invoked by HTTP requests.
Steps to your container to Cloud Run:
- Write a sample application
- Containerize the application
- Upload the app to Container Registry
- Deploy to Cloud Run
1. Write a sample application
app.py
import os
from flask import Flask
app = Flask(__name__)
@app.route(‘/’)
def hello_world():
return ‘Hello Google Cloud!\n’
if __name__ == “__main__”:
app.run(debug=True,host=’0.0.0.0',port=8080)))
2. Containerize the application
DockerfileFROM python:3.7
# Copy local code to the container image.
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . .
# Install production dependencies.
RUN pip install Flask gunicorn
# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD exec gunicorn — bind :$PORT — workers 1 — threads 8 app:app
3. Upload the app to Container Registry
Open Cloud shell and run the below given command. (change ‘PROJECT-ID’ with the project id of your GCP Project)
The last part of command i.e ‘helloworld’ is the container name, you can name it as per your wish.
gcloud builds submit — tag gcr.io/[PROJECT-ID]/helloworld
4. Deploy to Cloud Run
The below command pushes the container to production.
gcloud beta run deploy — image gcr.io/[PROJECT-ID]/helloworld
Now you have a deployed your container on a completely managed, Auto scaled server with https encryption and a domain name for your application.
Benefits of Cloud Run:
Simple developer experience
Fast Auto-scaling
Fully Managed
Any language, Any library
Integrated logging and monitoring
Reach out to me on twitter: @IVaibhavMalpani