makeporngreatagain.pro
yeahporn.top
hd xxx

Getting Started with Cloud Run

197

Write the sample application

  1. In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.
  2. From Cloud Shell, enable the Cloud Run API :
    gcloud services enable run.googleapis.com
  3. In Cloud Shell create a new directory named helloworld-nodejs, then change into that directory:
    mkdir helloworld-nodejs
    cd helloworld-nodejs
  4. Next you’ll be creating and editing files. To edit files, use viemacnano or the Cloud Shell Code Editor by clicking on the Open editor icon in Cloud Shell.
  5. Create a package.json file, then add the following content to it:
    {
      "name": "cloudrun-helloworld",
      "version": "1.0.0",
      "description": "Simple hello world sample in Node",
      "main": "index.js",
      "scripts": {
        "start": "node index.js"
      },
      "author": "",
      "license": "Apache-2.0",
      "dependencies": {
        "express": "^4.16.4"
      }
    }
    

    Most importantly, the file above contains a start script command and a dependency on the Express web application framework.

     

  6. Next, in the same directory, create a index.js file, and copy the following lines into it:
    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
      console.log('Hello world received a request.');
    
      const target = process.env.TARGET || 'World';
      res.send(`Hello ${target}!`);
    });
    
    const port = process.env.PORT || 8080;
    app.listen(port, () => {
      console.log('Hello world listening on port', port);
    });
    

    This code creates a basic web server that listens on the port defined by the PORT environment variable. Your app is now finished and ready to be containerized and uploaded to Container Registry.

Containerize your app and upload it to Container Registry

  1. To containerize the sample app, create a new file named Dockerfile in the same directory as the source files, and add the following content:
    # Use the official Node.js 10 image.
    # https://hub.docker.com/_/node
    FROM node:10
    
    # Create and change to the app directory.
    WORKDIR /usr/src/app
    
    # Copy application dependency manifests to the container image.
    # A wildcard is used to ensure both package.json AND package-lock.json are copied.
    # Copying this separately prevents re-running npm install on every code change.
    COPY package*.json ./
    
    # Install production dependencies.
    RUN npm install --only=production
    
    # Copy local code to the container image.
    COPY . .
    
    # Run the web service on container startup.
    CMD [ "npm", "start" ]
    

     

  2. Get your Project ID by running the following, you’ll need it for the next step:
    gcloud config get-value project
    

     

  3. Now, build your container image using Cloud Build by running the following command from the directory containing the Dockerfile, adding your Project-ID from the last output:
    gcloud builds submit --tag gcr.io/[PROJECT-ID]/helloworld
    

    Cloud Build is a service that executes your builds on Google Cloud. It executes a series of build steps, where each build step is run in a Docker container to produce your application container (or other artifacts) and push it to Cloud Registry, all in one command.

    Once pushed to the registry, you will see a SUCCESS message containing the image name (gcr.io/[PROJECT-ID]/helloworld). The image is stored in Container Registry and can be re-used if desired.

     

  4. List all the container images associated with your current project using this command :
    gcloud container images list
    

     

  5. To run and test the application locally from Cloud Shell, start it using this standard docker command and make sure to replace your project id.
    docker run -d -p 8080:8080 gcr.io/[PROJECT-ID]/helloworld

     

  6. In the Cloud Shell window, click on Web preview and select “Preview on port 8080”.web_preview.png

    This should open a browser window showing the “Hello World!” message. You could also simply use curl localhost:8080.

Deploy to Cloud Run

  1. Deploying your containerized application to Cloud Run is done using the following command adding your Project-ID:
    gcloud beta run deploy --image gcr.io/[PROJECT-ID]/helloworld
    
  2. When prompted:
    • select Cloud Run (fully managed)
    • confirm the service name by pressing Enter
    • select your region ( us-central1)
    • respond y to allow unauthenticated invocations (that last step is important and can also be avoided by using the --allow-unauthenticated deploy option).

    Wait a few moments until the deployment is complete.

  3. On success, the command line displays the service URL:
    Service [helloworld] revision [helloworld-00001] has been deployed
    and is serving traffic at https://helloworld-wdl7fdwaaa-uc.a.run.app
    
  4. You can now visit your deployed container by opening the service URL in any browser window:
  5. From the Navigation menu, in the Compute section, click Cloud Run and you should see your helloworld service listed:

Comments are closed, but trackbacks and pingbacks are open.

baseofporn.com https://www.opoptube.com
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.