Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Während der Entwicklung haben Sie die Möglichkeit, das Container-Image lokal auszuführen und zu testen, bevor Sie es bereitstellen. Sie können Cloud Code oder lokal installierte Docker zur lokalen Ausführung und für lokale Tests verwenden, einschließlich einer lokalen Ausführung mit Zugriff auf Google Cloud-Dienste.
In einem Knative-Bereitstellungsemulator ausführen
Mit dem Cloud Code-Plug-in für VS Code und mit JetBrains-IDEs können Sie Ihr Container-Image lokal in einem Knative Serving in Ihrer IDE ausführen und dabei Fehler beheben. Mit dem Emulator können Sie eine Umgebung konfigurieren, die für Ihren Dienst in Knative-Serving repräsentativ ist.
Sie können Eigenschaften wie CPU- und Arbeitsspeicherzuweisung konfigurieren, Umgebungsvariablen angeben und Cloud SQL-Datenbankverbindungen festlegen.
Ersetzen Sie IMAGE_URL durch einen Verweis auf das Container-Image, z. B. gcr.io/cloudrun/hello.
Die Umgebungsvariable PORT gibt den Port an, über den die Anwendung HTTP- oder HTTPS-Anfragen empfängt. Dies ist eine Anforderung des Containerlaufzeitvertrags.
In diesem Beispiel verwenden wir Port 8080.
Wenn Sie zum ersten Mal mit Containern arbeiten, sollten Sie die Einführung in Docker lesen. Weitere Informationen zu Docker-Befehlen finden Sie in der Docker-Dokumentation.
Lokal mit Docker und Zugriff auf Google Cloud-Dienste ausführen
Wenn Sie die Google Cloud-Clientbibliotheken verwenden, um Ihre Anwendung in Google Cloud-Dienste zu integrieren, und diese Dienste noch nicht zur Steuerung des externen Zugriffs gesichert haben, können Sie Ihren lokalen Container für die Authentifizierung bei Google Cloud-Diensten mit Standardanmeldedaten für Anwendungen einrichten.
Mit den folgenden Docker-Ausführungsflags werden die Anmeldedaten und die Konfiguration des lokalen Systems in den lokalen Container eingefügt:
Verwenden Sie das Flag --volume (-v), um die Datei mit den Anmeldedaten in den Container einzufügen. Dabei wird vorausgesetzt, dass Sie auf Ihrer Maschine bereits die Umgebungsvariable GOOGLE_APPLICATION_CREDENTIALS festgelegt haben:
Beachten Sie, dass der im obigen Beispiel aufgeführte Pfad
/tmp/keys/FILE_NAME.json
gut geeignet ist, um Ihre Anmeldedaten im Container zu platzieren.
Sie können aber auch ein anderes Verzeichnis verwenden. Wichtig ist, dass die Umgebungsvariable GOOGLE_APPLICATION_CREDENTIALS mit dem gebundenen Bereitstellungsspeicherort im Container übereinstimmt.
Beachten Sie auch, dass Sie bei einigen Google Cloud-Diensten möglicherweise eine alternative Konfiguration verwenden sollten, um die lokale Fehlerbehebung von der Produktionsleistung und den Daten zu isolieren.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2024-11-21 (UTC)."],[],[],null,["# Local testing\n\nDuring development, you can run and test your container image locally, prior\nto deploying. You can use [Cloud Code](/code) or\n[Docker installed locally](https://docs.docker.com/install/) to run and test\nlocally, including running locally with access to Google Cloud services.\n\nRunning in a Knative serving emulator\n-------------------------------------\n\nThe [Cloud Code](https://cloud.google.com/code) plugin for\n[VS Code](https://code.visualstudio.com/) and [JetBrains IDEs](https://www.jetbrains.com/)\nlets you locally run and debug your container image in a Knative serving\nemulator within your IDE. The emulator allows you configure an environment that is\nrepresentative of your service running on Knative serving.\n\nYou can configure properties like CPU and memory allocation,\nspecify environment variables, and set Cloud SQL\ndatabase connections.\n\n1. Install Cloud Code for [VS Code](/code/docs/vscode/install) or a [JetBrains IDE](/code/docs/intellij/install).\n2. Follow the instructions for locally developing and debugging within your IDE.\n - **VS Code** : Locally [developing](/code/docs/vscode/developing-a-cloud-run-app) and [debugging](/code/docs/vscode/debugging-a-cloud-run-app)\n - **IntelliJ** : Locally [developing](/code/docs/intellij/developing-a-cloud-run-app) and [debugging](/code/docs/intellij/debugging-a-cloud-run-app)\n\nRunning locally using Docker\n----------------------------\n\nTo test your container image locally using Docker:\n\n1. Use the Docker command:\n\n ```bash\n PORT=8080 && docker run -p 9090:${PORT} -e PORT=${PORT} IMAGE_URL\n ```\n\n Replace \u003cvar translate=\"no\"\u003eIMAGE_URL\u003c/var\u003e with a reference to the container image, for\n example, `gcr.io/cloudrun/hello`.\n\n The `PORT` environment variable specifies the port your application will\n use to listen for HTTP or HTTPS requests. This is a requirement from\n the [Container Runtime Contract](/kubernetes-engine/enterprise/knative-serving/docs/reference/container-contract).\n In this example, we use port 8080.\n2. Open \u003chttp://localhost:9090\u003e in your browser.\n\nIf you are new to working with containers, you may want to review the\n[Docker Getting Started](https://docs.docker.com/get-started/) guide. To learn\nmore about Docker commands, refer to the\n[Docker documentation](https://docs.docker.com/engine/reference/run/).\n\nRunning locally using Docker with access to Google Cloud services\n-----------------------------------------------------------------\n\nIf you are using Google Cloud client libraries to integrate your application\nwith Google Cloud services, and have not yet secured those services to control external\naccess, you can set up your local container to authenticate with Google Cloud\nservices using a service account key.\n| **Note:** Service account keys are a security risk if not managed correctly. You should [choose a more secure alternative to service account keys](/docs/authentication#auth-decision-tree) whenever possible. If you must authenticate with a service account key, you are responsible for the security of the private key and for other operations described by [Best practices for managing service account keys](/iam/docs/best-practices-for-managing-service-account-keys). If you are prevented from creating a service account key, service account key creation might be disabled for your organization. For more information, see [Managing secure-by-default organization resources](/resource-manager/docs/secure-by-default-organizations).\n|\n|\n| If you acquired the service account key from an external source, you must validate it before use.\n| For more information, see [Security requirements for externally sourced credentials](/docs/authentication/external/externally-sourced-credentials).\n\nTo run locally:\n\n1. Refer to [Service account key](/docs/authentication/set-up-adc-on-premises#wlif-key)\n for help with setting up your credentials.\n\n2. The following Docker run flags inject the credentials and configuration from\n your local system into the local container:\n\n 1. Use the `--volume` (`-v`) flag to inject the credential file into the container (assumes you have already set your `GOOGLE_APPLICATION_CREDENTIALS` environment variable on your machine): \n\n ```bash\n -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/FILE_NAME.json:ro\n ```\n 2. Use the `--environment` (`-e`) flag to set the `GOOGLE_APPLICATION_CREDENTIALS` variable inside the container: \n\n ```bash\n -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/FILE_NAME.json\n ```\n3. Optionally, use this fully configured Docker `run` command:\n\n ```bash\n PORT=8080 && docker run \\\n -p 9090:${PORT} \\\n -e PORT=${PORT} \\\n -e K_SERVICE=dev \\\n -e K_CONFIGURATION=dev \\\n -e K_REVISION=dev-00001 \\\n -e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/FILE_NAME.json \\\n -v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/FILE_NAME.json:ro \\\n IMAGE_URL\n ```\n\n Note that the path \n\n ```\n /tmp/keys/FILE_NAME.json\n ```\n shown in the example above is a reasonable location to place your credentials inside the container.\n\n \u003cbr /\u003e\n\n However, other directory locations will also work. The crucial requirement\n is that the `GOOGLE_APPLICATION_CREDENTIALS` environment variable must\n match the bind mount location inside the container.\n\n Note also, that with some Google Cloud services, you may want to use an alternate\n configuration to isolate local troubleshooting from production performance\n and data.\n\nWhat's next\n-----------\n\nTo learn how to deploy your built containers, follow [Deploying Services](/kubernetes-engine/enterprise/knative-serving/docs/deploying)."]]