Advertisement
Guest User

Untitled

a guest
Mar 20th, 2025
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. // At the bottom custom metadata of instance templates
  2. // Key: startup-script
  3.  
  4. Copy and paste the value below and update the docker value paths and any env variables
  5. // Value:
  6.  
  7. #!/bin/bash
  8.  
  9. # Ensure Python3 is installed
  10. if ! command -v python3 &> /dev/null; then
  11. echo "Installing Python3..."
  12. apt-get update
  13. apt-get install -y python3
  14. echo "Python3 installed."
  15. else
  16. echo "Python3 is already installed."
  17. fi
  18.  
  19. # Ensure curl is installed
  20. if ! command -v curl &> /dev/null; then
  21. echo "Installing curl..."
  22. apt-get update
  23. apt-get install -y curl
  24. echo "curl installed."
  25. else
  26. echo "curl is already installed."
  27. fi
  28.  
  29. # Install NVIDIA drivers using Google's recommended method
  30. if test -f /opt/google/cuda-installer; then
  31. echo "NVIDIA drivers are already installed."
  32. else
  33. echo "Installing NVIDIA drivers using Google's recommended installer..."
  34. mkdir -p /opt/google/cuda-installer
  35. cd /opt/google/cuda-installer/ || exit
  36. curl -fSsL -O https://github.com/GoogleCloudPlatform/compute-gpu-installation/releases/download/cuda-installer-v1.1.0/cuda_installer.pyz
  37. python3 cuda_installer.pyz install_cuda
  38. echo "NVIDIA drivers installed successfully."
  39. fi
  40.  
  41. # Check if Docker is installed
  42. if ! command -v docker &> /dev/null; then
  43. echo "Installing Docker..."
  44. apt-get update
  45. apt-get install -y docker.io
  46. systemctl enable docker
  47. systemctl start docker
  48. echo "Docker installed and started."
  49. else
  50. echo "Docker is already installed."
  51. fi
  52.  
  53. # Install NVIDIA Container Toolkit if not already installed
  54. if ! command -v nvidia-ctk &> /dev/null; then
  55. echo "Installing NVIDIA Container Toolkit..."
  56. curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg
  57. curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \
  58. sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
  59. tee /etc/apt/sources.list.d/nvidia-container-toolkit.list
  60. apt-get update
  61. apt-get install -y nvidia-container-toolkit
  62. nvidia-ctk runtime configure --runtime=docker
  63. systemctl restart docker
  64. echo "NVIDIA Container Toolkit installed and configured."
  65. else
  66. echo "NVIDIA Container Toolkit is already installed."
  67. fi
  68.  
  69. # Authenticate with Google Container Registry if necessary
  70. if ! docker images | grep -q 'us.gcr.io/your/path/here'; then
  71. echo "Pulling Docker image from Google Container Registry..."
  72. if ! command -v gcloud &> /dev/null; then
  73. echo "Installing Google Cloud SDK..."
  74. apt-get install -y google-cloud-sdk
  75. fi
  76. gcloud auth configure-docker us.gcr.io
  77. docker pull us.gcr.io/your/path/here:version-1.0-snapshot
  78. echo "Docker image pulled."
  79. fi
  80.  
  81. # Run the Docker container with environment variables
  82. echo "Starting Docker container..."
  83. docker run --gpus all -p 3000:3000 -d \
  84. -e EXAMPLE_ENV='test1' \
  85. -e EXAMPLE_ENV_2='server' \
  86. us.gcr.io/your/path/here:version-1.0-snapshot
  87. echo "Docker container started."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement