This below start up script doesn't result in Kafka containers running when I check using "docker ps -a".
#!/bin/bash
sudo apt update
sudo apt install -y docker.io
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo git clone https://github.com/XXXXX/Nodejs_Kafka.git /home/XXXX/app
sudo mv /home/XXXXX/app/docker-compose.yml /home/XXXXXXX/docker-compose.yml
sudo cd /home/XXXXXX
sudo docker-compose up -d
I tried using a nodejs client library and then using it to create a GCP VM giving all the necessary details and in the metadata section I gave this script. I cloned a repo to get a docker-compose file and moved to home directory. But whenever I login to the vm by ssh and then try to see the containers by giving "docker ps -a" , no containers are ever created , which means the docker compose never ran.
As @Puteri mentioned, if executed on bash one by one after ssh, it 100 % works.
1.As @puteri mentioned, check if the script is running when you manually run on the VM and before that check for any errors in the logs.
2.For better lifetime management by writing out a systemd unit (together with the bash script) with restart=true and just enable/start the unit during the boot process.
3.As @guillaume mentioned, try to run without sudo or switch to a non-root user in the script.
4.You can also try running a docker container in the detached process as suggested by the community member Sim in the SO post.
For more troubleshooting you can follow this document.