SSH into newly deployed Container on GCP Compute Engine

I want to simply SSH into a container deployed on GCP Compute Engine without jumping through hoops.

First, I create a compute instance with a container:

gcloud compute instances create-with-container instance_name --container-image gcr.io/my-container-path:latest

Then try to 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. Is there a better way?


I am not aware of an option to name containers at virtual machine instance creation.

As a solution, you could run a command to list the container images. This command would be run once to get the image name. Put it into a shell script.

gcloud compute ssh $INSTANCE_NAME \
--project $PROJECT_ID \
--zone $ZONE_NAME \
--ssh-key-file $SSH_PRIVATE_KEY \
--command "docker ps"

Note: you can set up the CLI to not require specifying the project and zone to simplify the command:

gcloud config set project PROJECT_ID
gcloud config set compute/zone ZONE_NAME

The command can be simplified more if the instance is using the default SSH private key, which is ~/.ssh/google_compute_engine.

That reduces the command to:

gcloud compute ssh $INSTANCE_NAME --command "docker ps"