Puoi configurare le variabili di ambiente impostate durante la creazione dell'immagine container.
Puoi anche personalizzare l'immagine container estendendo le immagini di build ed esecuzione.
Build locali
Per le build locali, devi aver installato Pack CLI e Docker.
Prima di iniziare
- Installa Docker Community Edition (CE)
sulla tua workstation. Docker viene utilizzato da
packcome generatore di immagini OCI. - Installa Pack CLI.
- Installa lo strumento di controllo del codice sorgente Git per recuperare l'applicazione di esempio da GitHub.
Imposta le variabili di ambiente
Per impostare le variabili di ambiente per le build locali, aggiungi il flag --env al comando pack per ogni variabile di ambiente.
Puoi utilizzare una o più delle variabili di ambiente supportate dal runtime.
pack build SERVICE_NAME \
--env ENVIRONMENT_VARIABLE
Sostituisci:
SERVICE_NAMEcon il nome del servizio per la tua applicazione o funzione.ENVIRONMENT_VARIABLEcon la variabile di ambiente che vuoi impostare durante il tempo di compilazione.
- Esempio
Per impostare la variabile di ambiente
GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app"nell'immagine container, esegui questo comando:pack build my-app \ --builder gcr.io/buildpacks/builder:v1 \ --env GOOGLE_ENTRYPOINT="gunicorn -p :8080 main:app" --env MY-LOCAL-ENV-VARIABLE
Build remoti
Per impostare le variabili di ambiente per le build remote, utilizza il
descrittore del progetto project.toml.
Il descrittore del progetto project.toml viene utilizzato da Cloud Build durante la creazione dell'immagine container.
Prima di iniziare
- Accedi al tuo account Google Cloud . Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti senza costi per l'esecuzione, il test e il deployment dei carichi di lavoro.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build and Artifact Registry APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Build and Artifact Registry APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init - Assicurati che il tuo progetto Google Cloud abbia accesso a un repository di immagini container.
Per configurare l'accesso a un repository Docker in Artifact Registry:
- Crea un nuovo repository Docker nella stessa località del tuo progetto Google Cloud .
Sostituisci:gcloud artifacts repositories create REPO_NAME \ --repository-format=docker \ --location=REGION --description="DESCRIPTION"
REPO_NAMEcon il nome che scegli per il repository Docker.REGIONcon la posizione in o più vicina alla posizione del tuo progetto Google Cloud .DESCRIPTIONcon una descrizione a tua scelta.
Ad esempio, per creare un repository
dockerinus-west2con la descrizione "Docker repository", esegui:gcloud artifacts repositories create buildpacks-docker-repo --repository-format=docker \ --location=us-west2 --description="Docker repository"
- Verifica che il repository sia stato creato:
gcloud artifacts repositories list
Dovresti vedere il nome scelto per il repository Docker nell'elenco.
- Crea un nuovo repository Docker nella stessa località del tuo progetto Google Cloud .
(Facoltativo) Scaricare un'applicazione di esempio
- Clona il repository di esempio sulla tua macchina locale:
git clone https://github.com/GoogleCloudPlatform/buildpack-samples.git
- Passa alla directory che contiene il codice campione dell'applicazione:
Vai
cd buildpack-samples/sample-go
Java
cd buildpack-samples/sample-java-gradle
Node.js
cd buildpack-samples/sample-node
PHP
cd buildpack-samples/sample-php
Python
cd buildpack-samples/sample-python
Ruby
cd buildpack-samples/sample-ruby
.NET
cd buildpack-samples/sample-dotnet
Crea l'applicazione con le variabili di ambiente
- Nella directory radice del servizio, crea o aggiorna il descrittore del progetto
project.tomlin modo che includa la sezione[[build.env]]e una delle variabili di ambiente supportate:[[build.env]] name = "ENVIRONMENT_VARIABLE_NAME" value = "ENVIRONMENT_VARIABLE_VALUE"
Sostituisci:
ENVIRONMENT_VARIABLE_NAMEcon il nome di una variabile di ambiente supportata.ENVIRONMENT_VARIABLE_VALUEcon il valore corrispondente per la variabile di ambiente specificata.
Esempio
[[build.env]] name = "GOOGLE_ENTRYPOINT" value = "gunicorn -p :8080 main:app"
- Utilizza
gcloudper inviare il codice sorgente dell'applicazione a Cloud Build:gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
Sostituisci:
LOCATIONcon il nome della regione del tuo repository di container. Esempio:us-west2-docker.pkg.devPROJECT_ID: l'ID del tuo progetto Google Cloud .REPO_NAMEcon il nome del tuo repository Docker.IMAGE_NAMEcon il nome dell'immagine container.
Esempi: se hai scaricato un'applicazione di esempio, esegui il comando corrispondente:
Vai
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-go
Java
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-java-gradle
Node.js
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-node
PHP
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-php
Python
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-python
Ruby
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-ruby
.NET
gcloud builds submit --pack image=LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/sample-dotnet
-
Verifica che l'applicazione di esempio sia stata pubblicata correttamente su
REPO_NAME:gcloud artifacts docker images list LOCATION-docker.pkg.dev/project-id/REPO_NAME
Sostituisci:
LOCATIONcon il nome della regione del repository del container. Esempio:us-west2-docker.pkg.devPROJECT_ID: l'ID del tuo progetto Google Cloud .REPO_NAMEcon il nome del tuo repository Docker.