Startup scripts are not behaving the way that I expected them to.
I have a .sh file in a storage bucket and have included a startup-script-url meta tag with the value gs://bucket-name/start-script.sh
[[0;32m OK [0m] Started Google Compute Engine Accounts Daemon.
Starting Google Compute Engine Startup Scripts...
[[0;32m OK [0m] Started Google Compute Engine Shutdown Scripts.
[[0;32m OK [0m] Started Google Compute Engine Startup Scripts.
[[0;32m OK [0m] Started Docker Application Container Engine.
[[0;32m OK [0m] Started Wait for Google Container Registry (GCR) to be accessible.
[[0;32m OK [0m] Reached target Google Container Registry (GCR) is Online.
[[0;32m OK [0m] Started Containers on GCE Setup.
[ 8.001248] konlet-startup[1018]: 2018/03/08 20:23:56 Starting Konlet container startup agent
The below script is not executed as expected. I tried using the startup-script metadata tag and using something simple like echo "hello" but that's not working either. I have full Cloud API access scopes enabled.
If I copy the contents of the below file and paste it into the ssh terminal it works perfectly.
Could really use some help =D
start-script.sh
#! /bin/bash
image_name=gcr.io/some-image:version-2
docker_images=$(docker inspect --type=image $image_name)
array_count=${#docker_images[0]}
# Check if docker image is available
while ((array_count = 2));
do
docker_images=$(docker inspect --type=image ${image_name})
array_count=${#docker_images[0]}
if (($array_count > 2)); then
break
fi
done
################################
#
# Docker image now injected
# by google compute engine
#
################################
echo "docker image ${image_name} available"
container_ids=($(docker container ls | grep ${image_name} | awk '{ print $1}'))
for (( i=0; i < ${#container_ids[@]}; i++ ));
do
# run command for each container
container_id=${container_ids[i]}
echo "running commands for container: ${container_ids[i]}"
# start cloud sql proxy
docker container run -d -p $HOST_PORT:$APPLICATION_PORT \
${container_ids[i]} \
./cloud_sql_proxy \
-instances=$PG_INSTANCE_CONNECTION_NAME=tcp:$PG_PORT \
-credential_file=$PG_CREDENTIAL_FILE_LOCATION
# HTTP - Start unicorn webserver
docker exec -d \
${container_ids[i]} \
bundle exec unicorn -c config/unicorn.rb
done
startup-script always run when use the "Deploy a container image to this VM instance" option.

You can use cmd sudo journalctl -u google-startup-scripts.service -f to check script run result.

You can use cmd sudo google_metadata_script_runner -d --script-type startup to debug script.
2021.11.09 Updated: sudo google_metadata_script_runner startup.
doc: https://cloud.google.com/compute/docs/instances/startup-scripts
Okay... after some scenario testing... found out that startup scripts do not run if you use the "Deploy a container image to this VM instance" option. Hope this saves you from tearing your hair out haha.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With