Buildpacks are another approach for building container images and provide an alternate approach to turn your source code into a container image. Buildpacks are distributed and executed in images called builders. Each builder can have one or more buildpacks. A builder turns your source code into a container image. The buildpacks do the actual work to build and package the container image that you can deploy to Cloud Run or run with Docker locally.
You can create your own buildpacks, or use those provided by multiple vendors. Google Cloud's buildpacks allow developers to create and deploy containerized applications without the need to install Docker locally, or create a Dockerfile. Buildpacks are also built into Cloud Run to enable a source-based deployment workflow.
Objectives
In this lab, you:
Build an application with pack, a command-line tool that is used with builders to create container images from source code.
Use the Google Cloud's buildpacks builder to build a container image.
Run and test the container locally with Docker.
Build and redeploy the container to Cloud Run.
Setup
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
Activate Google Cloud Shell
Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.
Google Cloud Shell provides command-line access to your Google Cloud resources.
In Cloud console, on the top right toolbar, click the Open Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
You can list the active account name with this command:
View the sample application files and source code:
ls sample-py-app
cat sample-py-app/main.py
Because the Python buildpack does not generate a default container entry-point for the application, we use a Procfile to configure the application's start command.
The application is written in Python and returns a sample welcome message in response to a request made to the application.
...
...
[exporter] Setting default process type 'web'
[exporter] Saving sample-py-app...
[exporter] *** Images (9f9f9a48fd46):
[exporter] sample-py-app
[exporter] Adding cache layer 'google.python.pip:pip'
[exporter] Adding cache layer 'google.python.pip:pipcache'
Successfully built image sample-py-app
Note: With pack, you did not need to write and provide a Dockerfile to build the container image.
To view the images downloaded and built in your Cloud Shell host, run:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gcr.io/buildpacks/builder latest 514fb6f1bbfe 29 hours ago 804MB
gcr.io/buildpacks/gcp/run v1 22db1b5e48e3 29 hours ago 177MB
buildpacksio/lifecycle 0.16.0 67e021546a3f 43 years ago 30.5MB
sample-py-app latest 9f9f9a48fd46 43 years ago 571MB
Run the container locally in Docker by passing in the PORT environment variable to the application and binding the host's port 8080 to the container port:
docker run -it -e PORT=8080 -p 8080:8080 -d sample-py-app
The application code listens on the port that is provided in the environment variable, in this case, port 8080.
Test the containerized application with the curl command:
curl http://localhost:8080/
You should see the following message as a response:
Welcome to this sample app, built with Buildpacks.
Task 3. Build and run an application on Cloud Run
Typically, as a next step in your development and deployment lifecycle, you should push the container image that you built in the previous task to Artifact Registry, and then deploy the image to a container-based environment like Google Kubernetes Engine or Cloud Run.
In this task, you modify the sample application code, then build and deploy the containerized application directly from source with Cloud Run.
Modify the application code
You modify the sample application code to use the Google Translation API that translates a piece of text from English to Spanish.
Edit the main.py file with an editor of your choice, for example, vi or nano. You can also click Open Editor from the Cloud Shell menu to edit the file.
Replace the entire contents of the main.py file with the code below:
from flask import Flask, request
import google.auth
from google.cloud import translate
app = Flask(__name__)
_, PROJECT_ID = google.auth.default()
TRANSLATE = translate.TranslationServiceClient()
PARENT = 'projects/{}'.format(PROJECT_ID)
SOURCE, TARGET = ('en', 'English'), ('es', 'Spanish')
@app.route('/', methods=['GET', 'POST'])
def index():
# reset all variables
text = translated = None
if request.method == 'POST':
text = request.get_json().get('text').strip()
if text:
data = {
'contents': [text],
'parent': PARENT,
'target_language_code': TARGET[0],
}
# handle older call for backwards-compatibility
try:
rsp = TRANSLATE.translate_text(request=data)
except TypeError:
rsp = TRANSLATE.translate_text(**data)
translated = rsp.translations[0].translated_text
# create context
context = {
'trtext': translated
}
return context
if __name__ == "__main__":
# Dev only: run "python main.py" and open http://localhost:8080
import os
app.run(host="localhost", port=int(os.environ.get('PORT', 8080)), debug=True)
The application code uses the Google Translate API to translate a piece of text passed in a JSON request from English to Spanish.
Build and deploy the container
To build and deploy the container on Cloud Run, execute the following command:
gcloud run deploy sample-py-app --source . --region=${REGION} --allow-unauthenticated
The allow-unauthenticated option enables access to the service without requiring any authentication.
When prompted, type Y to accept the default repository that is created in Artifact Registry to store the container image.
When the command completes, a Cloud Run service named sample-py-app is created.
The command output is similar to:
Building using Buildpacks and deploying container to Cloud Run service [sample-py-app] in project [qwiklabs-gcp-00-0d56d42aca1a] region [asia-east1]
OK Building and deploying new service... Done.
OK Creating Container Repository...
OK Uploading sources...
OK Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds/8bea2ded-4745-41f9-a82d-128e409daa20?project=34240880885].
OK Creating Revision...
OK Routing traffic...
OK Setting IAM Policy...
Done.
Service [sample-py-app] revision [sample-py-app-00001-nec] has been deployed and is serving 100 percent of traffic.
Service URL: https://sample-py-app-ulvp7xw3bq-de.a.run.app
Test the Cloud Run service
Set an environment variable for the Cloud Run service that was created in the previous step:
SERVICE_URL=[SERVICE URL]
Replace the [SERVICE URL] with the value returned from Cloud Run in the output of the command in the previous step.
To test the service, , and execute the curl command:
curl $SERVICE_URL -H 'Content-Type: application/json' -d '{"text" : "Welcome to this sample app, built with Google Cloud buildpacks."}'
{"trtext":"Bienvenido a esta aplicaci\u00f3n de muestra, creada con paquetes de compilaci\u00f3n de Google Cloud."}
Click Check my progress to verify the objective.
Deploy an application on Cloud Run
Congratulations!
In this lab, you built an application with the pack command, and the Google Cloud's buildpacks builder to build a container image for a sample python application. You first ran and tested the container locally with Docker before rebuilding and deploying the container to Cloud Run.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
Les ateliers créent un projet Google Cloud et des ressources pour une durée déterminée.
Les ateliers doivent être effectués dans le délai imparti et ne peuvent pas être mis en pause. Si vous quittez l'atelier, vous devrez le recommencer depuis le début.
En haut à gauche de l'écran, cliquez sur Démarrer l'atelier pour commencer.
Utilisez la navigation privée
Copiez le nom d'utilisateur et le mot de passe fournis pour l'atelier
Cliquez sur Ouvrir la console en navigation privée
Connectez-vous à la console
Connectez-vous à l'aide des identifiants qui vous ont été attribués pour l'atelier. L'utilisation d'autres identifiants peut entraîner des erreurs ou des frais.
Acceptez les conditions d'utilisation et ignorez la page concernant les ressources de récupération des données.
Ne cliquez pas sur Terminer l'atelier, à moins que vous n'ayez terminé l'atelier ou que vous ne vouliez le recommencer, car cela effacera votre travail et supprimera le projet.
Ce contenu n'est pas disponible pour le moment
Nous vous préviendrons par e-mail lorsqu'il sera disponible
Parfait !
Nous vous contacterons par e-mail s'il devient disponible
Un atelier à la fois
Confirmez pour mettre fin à tous les ateliers existants et démarrer celui-ci
Utilisez la navigation privée pour effectuer l'atelier
Ouvrez une fenêtre de navigateur en mode navigation privée pour effectuer cet atelier. Vous éviterez ainsi les conflits entre votre compte personnel et le compte temporaire de participant, qui pourraient entraîner des frais supplémentaires facturés sur votre compte personnel.
In this lab, you build a containerized application with the CLI tool pack, and the Google Cloud Buildpacks builder.
Durée :
0 min de configuration
·
Accessible pendant 60 min
·
Terminé après 60 min