I have the following terraform script:
# Configure the Google Cloud Provider
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 6.0"
}
}
}
# Define Variables
variable "project_id" {
type = string
default = "myProject"
}
variable "zone" {
type = string
default = "europe-central2-a"
}
variable "machine_type" {
type = string
default = "e2-small"
}
variable "network" {
type = string
default = "default"
}
variable "image" {
type = string
default = "cos-cloud/cos-73-11647-217-0"
}
variable "docker_image" {
type = string
default = "myusername/myProject:master"
}
provider "google" {
project = "myProject"
}
module "startup-scripts" {
source = "terraform-google-modules/startup-scripts/google"
version = "~> 2.0"
enable_get_from_bucket = true
enable_setup_init_script = true
enable_setup_sudoers = true
}
resource "google_compute_instance" "default" {
name = "myProject-vm"
machine_type = var.machine_type
zone = var.zone
boot_disk {
initialize_params {
image = var.image
}
auto_delete = true
}
network_interface {
network = var.network
access_config {}
}
# Metadata Startup Script to Install Docker and Start a Container
metadata = {
startup-script = module.startup-scripts.content
startup-script-custom = file("${path.module}/startup-script.sh")
}
}
and this is my startup-script.sh
#!/bin/bash
docker pull myUsername/myProject:master
# Run the Docker container
docker run -d myUsername/myProject:master
But once the vm is running, I don’t see any image running.
Any idea what am I doing wrong?
Ensure Docker installation: Add the following to startup-script.sh:
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl start docker
docker pull myusername/myProject:master
docker run -d –name myProjectContainer myusername/myProject:master
Check logs: After the VM is running, view logs:
Startup logs: sudo journalctl -u google-startup-scripts.service
Docker logs: sudo docker logs myProjectContainer
Permissions: Ensure the script has execute permissions and the VM can access Docker registries.
Manual test: SSH into the VM and manually run the commands in the script to debug further