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
- Ensure you have the Pulumi CLI installed. If not, you can install it from here.
- Set up a new Pulumi project using TypeScript:
pulumi new typescript
- Install the necessary Pulumi packages for GCP:
npm install @pulumi/pulumi @pulumi/gcp
Step 2: Configure GCP Provider
- Set up your GCP credentials. You can follow the guide here to configure your GCP provider.
- 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
- 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
- Run
pulumi up
to create the GCE instance and deploy the Docker container. - 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 upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.