DanBaba
DanBaba

Reputation: 11

How to attach Bucket to Google Compute Engine VM on Startup?

I would like to, on startup, copy contents of my bucket to the VM with the Container Optimized OS. When the server shuts down I'd like to save the changes back to the bucket.

I've tried making a startup script

#!/bin/bash
toolbox
gsutil cp -r gs://my-bucket/

However, this causes the VM to fail on startup despite this script working if I run it manually.

Upvotes: 0

Views: 258

Answers (2)

llompalles
llompalles

Reputation: 3166

I just tried:

#! /bin/bash
gsutil cp -r gs://my-bucket /

And it worked for me. What is the toolbox command that you are executing previously?

Anyway you can see what is failing in the Serial Port Output.

EDIT: In the Container Optimized OS this does not work as this OS does not have the gsutil package preinstalled. Refer to @DanBaba answer.

Upvotes: 0

DanBaba
DanBaba

Reputation: 11

I think I found a reasonable solution. My script has changed to

#! /bin/bash
toolbox --bind=/home/username/bucket-folder:/my-bucket <<< "gsutil cp -r /my-bucket/* gs://my-bucket"

So what happens is we need to call toolbox --bind to bind a folder from the server to the toolbox container. Then we use <<< to pass the whole command to the container when it starts up so we copy to the newly bound directory so it goes back to the server.

Now when I bound the directory in my docker container, everything is there!

Upvotes: 1

Related Questions