1. Answers
  2. Deploy The Registry Docker Image On Google Compute Engine (GCE) With TypeScript.

Deploy the Registry Docker Image on Google Compute Engine (GCE) With TypeScript.

Introduction

In this guide, we will deploy a Docker image from a registry onto Google Compute Engine (GCE) using Pulumi with TypeScript. This involves creating a GCE instance and configuring it to run the Docker container.

Step-by-Step Explanation

Step 1: Set Up Pulumi and Install Dependencies

  1. Ensure you have the Pulumi CLI installed. If not, you can install it from here.
  2. Set up a new Pulumi project using TypeScript:
    pulumi new typescript
    
  3. Install the necessary Pulumi packages for GCP:
    npm install @pulumi/pulumi @pulumi/gcp
    

Step 2: Configure GCP Provider

  1. Set up your GCP credentials. You can follow the guide here to configure your GCP provider.
  2. Add the GCP provider to your Pulumi program:
    import * as pulumi from "@pulumi/pulumi";
    import * as gcp from "@pulumi/gcp";
    
    const config = new pulumi.Config();
    const project = config.require("gcp:project");
    const region = config.require("gcp:region");
    
    const provider = new gcp.Provider("gcp", {
        project: project,
        region: region,
    });
    

Step 3: Create a GCE Instance

  1. Define the GCE instance and configure it to run the Docker container:
    const instance = new gcp.compute.Instance("docker-instance", {
        machineType: "f1-micro",
        zone: "us-central1-a",
        bootDisk: {
            initializeParams: {
                image: "debian-cloud/debian-9",
            },
        },
        networkInterfaces: [{
            network: "default",
            accessConfigs: [{
                // Ephemeral IP
            }],
        }],
        metadataStartupScript: `#!/bin/bash
        sudo apt-get update
        sudo apt-get install -y docker.io
        sudo systemctl start docker
        sudo docker run -d --name my-container <your-docker-image>
        `,
    }, { provider });
    

Step 4: Deploy the Pulumi Stack

  1. Run pulumi up to create the GCE instance and deploy the Docker container.
  2. Confirm the deployment and check the instance on the GCP console.

Conclusion

By following these steps, you have successfully deployed a Docker image on a GCE instance using Pulumi with TypeScript. This setup ensures that your containerized application runs on a scalable and reliable infrastructure provided by Google Cloud Platform.

Full Code Example

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const instance = new gcp.compute.Instance("docker-instance", {
    machineType: "f1-micro",
    zone: "us-central1-a",
    bootDisk: {
        initializeParams: {
            image: "debian-cloud/debian-9",
        },
    },
    networkInterfaces: [{
        network: "default",
        accessConfigs: [{
            // Ephemeral IP
        }],
    }],
    metadataStartupScript: \`#!/bin/bash
    sudo apt-get update
    sudo apt-get install -y docker.io
    sudo systemctl start docker
    sudo docker run -d --name my-container <your-docker-image>
    \`,
});

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up