I want to do something that I would think is very simple/standard - but seems impossible.
First, create a compute instance with a container:
gcloud compute instances create-with-container instance_name --container-image gcr.io/my-container-path:latest
Then ssh into it:
gcloud compute ssh instance_name --container CONTAINER_NAME
The problem is that the CONTAINER_NAME is unknown. It seems there is no way to set the container name when you create the instance in a deterministic fashion. So, after you create the instance, you need to run "docker ps" on it to determine the random name before you can use the SSH functionality.
This seems like such a standard usage pattern that I must be missing something. Any pointers are appreciated.
Solved! Go to Solution.
The container name also shows in the serial log, so you can isolate the line with this once the container is up (about 40 seconds from what I can see):
gcloud compute instances get-serial-port-output instance_name | grep "Created a container with name"
You should see something like this:
[ 34.862318] konlet-startup[608]: 2021/09/27 13:19:04 Created a container with name 'klt-containertest-kanp' and ID: 4416865e6328a967a2315325ce169356f302b4f6534acf66dbbf982403c9ee5e
It is a very interesting question. There is no direct way that I'm aware of, but I was thinking you could pull it dynamically from logging with something like this (in bash):
gcloud logging read "logName=projects/<PROJECTNAME>/logs/cos_containers AND resource.labels.instance_id=`gcloud compute instances describe <INSTANCENAME> --format='value(id)'`" --format="value(jsonPayload.'cos.googlea
pis.com/container_name')" --limit=1
That should be all on one line, substituting : <PROJECTNAME> and <INSTANCENAME> for your own values.
Essentially this is doing two things, it's looking up the instance ID for the instance you deployed, then cross referencing that into the cos_containers logs and pulling out the container_name. Then from there you could add that into your `gcloud compute ssh` command.
Give that a try, would be interested to hear how you get on.
The container name also shows in the serial log, so you can isolate the line with this once the container is up (about 40 seconds from what I can see):
gcloud compute instances get-serial-port-output instance_name | grep "Created a container with name"
You should see something like this:
[ 34.862318] konlet-startup[608]: 2021/09/27 13:19:04 Created a container with name 'klt-containertest-kanp' and ID: 4416865e6328a967a2315325ce169356f302b4f6534acf66dbbf982403c9ee5e