Service Hello World
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Créez une application Hello World simple, empaquetez-la dans une image de conteneur, importez l'image de conteneur dans Artifact Registry, puis déployez l'image de conteneur dans Cloud Run.
En savoir plus
Pour obtenir une documentation détaillée incluant cet exemple de code, consultez les articles suivants :
Exemple de code
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],[],[],[],null,["# Hello World service\n\nCreate a simple Hello World application, package it into a container image, upload the container image to Artifact Registry, and then deploy the container image to Cloud Run.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Quickstart: Build and deploy a .NET web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-dotnet-service)\n- [Quickstart: Build and deploy a C++ web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-c-plus-plus-service)\n- [Quickstart: Build and deploy a Go web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-go-service)\n- [Quickstart: Build and deploy a Java Spring Boot web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-java-service)\n- [Quickstart: Build and deploy a Node.js web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-nodejs-service)\n- [Quickstart: Build and deploy a PHP web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-php-service)\n- [Quickstart: Build and deploy a Python (Flask) web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-python-service)\n- [Quickstart: Build and deploy a Ruby web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-ruby-service)\n\nCode sample\n-----------\n\n### C++\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n #include \u003cgoogle/cloud/functions/framework.h\u003e\n #include \u003ccstdlib\u003e\n\n namespace gcf = ::google::cloud::functions;\n\n auto hello_world_http() {\n return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {\n std::string greeting = \"Hello \";\n auto const* target = std::getenv(\"TARGET\");\n greeting += target == nullptr ? \"World\" : target;\n greeting += \"\\n\";\n\n return gcf::HttpResponse{}\n .set_header(\"Content-Type\", \"text/plain\")\n .set_payload(greeting);\n });\n }\n\n int main(int argc, char* argv[]) {\n return gcf::Run(argc, argv, hello_world_http());\n }\n\n### C#\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n var builder = WebApplication.CreateBuilder(args);\n\n var port = Environment.GetEnvironmentVariable(\"PORT\") ?? \"8080\";\n var url = $\"http://0.0.0.0:{port}\";\n var target = Environment.GetEnvironmentVariable(\"TARGET\") ?? \"World\";\n\n var app = builder.Build();\n\n app.MapGet(\"/\", () =\u003e $\"Hello {target}!\");\n\n app.Run(url);\n\n### Go\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Sample run-helloworld is a minimal Cloud Run service.\n package main\n\n import (\n \t\"fmt\"\n \t\"log\"\n \t\"net/http\"\n \t\"os\"\n )\n\n func main() {\n \tlog.Print(\"starting server...\")\n \thttp.HandleFunc(\"/\", handler)\n\n \t// Determine port for HTTP service.\n \tport := os.Getenv(\"PORT\")\n \tif port == \"\" {\n \t\tport = \"8080\"\n \t\tlog.Printf(\"defaulting to port %s\", port)\n \t}\n\n \t// Start HTTP server.\n \tlog.Printf(\"listening on port %s\", port)\n \tif err := http.ListenAndServe(\":\"+port, nil); err != nil {\n \t\tlog.Fatal(err)\n \t}\n }\n\n func handler(w http.ResponseWriter, r *http.Request) {\n \tname := os.Getenv(\"NAME\")\n \tif name == \"\" {\n \t\tname = \"World\"\n \t}\n \tfmt.Fprintf(w, \"Hello %s!\\n\", name)\n }\n\n### Java\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package com.example.helloworld;\n\n import org.springframework.beans.factory.annotation.Value;\n import org.springframework.boot.SpringApplication;\n import org.springframework.boot.autoconfigure.SpringBootApplication;\n import org.springframework.web.bind.annotation.GetMapping;\n import org.springframework.web.bind.annotation.RestController;\n\n @SpringBootApplication\n public class HelloworldApplication {\n\n @Value(\"${NAME:World}\")\n String name;\n\n @RestController\n class HelloworldController {\n @GetMapping(\"/\")\n String hello() {\n return \"Hello \" + name + \"!\";\n }\n }\n\n public static void main(String[] args) {\n SpringApplication.run(HelloworldApplication.class, args);\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import express from 'express';\n const app = express();\n\n app.get('/', (req, res) =\u003e {\n const name = process.env.NAME || 'World';\n res.send(`Hello ${name}!`);\n });\n\n const port = parseInt(process.env.PORT) || 8080;\n app.listen(port, () =\u003e {\n console.log(`helloworld: listening on port ${port}`);\n });\n\n### PHP\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n \u003c?php\n\n $name = getenv('NAME', true) ?: 'World';\n echo sprintf('Hello %s!', $name);\n\n### Python\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import os\n\n from flask import Flask\n\n app = Flask(__name__)\n\n\n @app.route(\"/\")\n def hello_world():\n \"\"\"Example Hello World route.\"\"\"\n name = os.environ.get(\"NAME\", \"World\")\n return f\"Hello {name}!\"\n\n\n if __name__ == \"__main__\":\n app.run(debug=True, host=\"0.0.0.0\", port=int(os.environ.get(\"PORT\", 8080)))\n\n### Ruby\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"sinatra\"\n\n set :bind, \"0.0.0.0\"\n port = ENV[\"PORT\"] || \"8080\"\n set :port, port\n\n get \"/\" do\n name = ENV[\"NAME\"] || \"World\"\n \"Hello #{name}!\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloudrun)."]]