Score:0

SSH into newly deployed Container on GCP Compute Engine

de flag

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?

Score:1
cn flag

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"
Score:0
cn flag

A single command version to connect to the running container:

gcloud compute ssh my-vm --project=my-project  --container=$(gcloud compute ssh my-vm --project=my-project --command 'sudo docker ps --format {{.ID}}')
Chad Brockman avatar
bh flag
Great answer - thank you
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.