Come Raczy
Come Raczy

Reputation: 1680

How do I get the correct shell with 'gcloud compute ssh <instance> --container <container>'

When using gcloud compute instances create-with-container, if I use an image that has bash as a CMD or ENTRYPOINT, and if I ssh into the VM, and finally attach to the container, I end up in a bash shell as expected:

> gcloud compute instances create-with-container test \
  --container-image debian:12-slim --container-stdin --container-tty
> gcloud compute ssh test
  ########################[ Welcome ]########################
  #  You have logged in to the guest OS.                    #
  #  To access your containers use 'docker attach' command  #
  ###########################################################

come@test ~ $ docker container ls
CONTAINER ID   IMAGE            COMMAND   CREATED       STATUS             PORTS     NAMES
2667f985b3f1   debian:12-slim   "bash"    2 hours ago   Up About an hour             klt-test-wwbe
come@test ~ $ docker inspect klt-test-wwbe | grep -A 1 Cmd
            "Cmd": [
                "bash"
come@test ~ $ docker attach klt-test-wwbe
root@test:/# echo $SHELL
/bin/bash
root@test:/#

I get the same effect when attaching the container using the --command option:

> gcloud compute ssh test --command "docker attach klt-test-wwbe" -- -t
root@test:/# echo $SHELL
/bin/bash

However, if I ssh into the container using gcloud and connect to the container using the --container option, I do not end up in a bash shell:

> gcloud compute ssh test --container klt-test-wwbe
# echo $SHELL

#

Why is that so? And how can I get the shell (or other environment) specified as the ENTRYPOINT or CMD of the docker image when using gcloud compute ssh test --container klt-test-wwbe, preferably without having to type it.

Of course, in this example, I can just type bash and be done with it. But if the ENTRYPOINT or CMD isn't something as trivial, I'd first need to inspect the container, which would somewhat defeat the purpose.

Upvotes: 1

Views: 24

Answers (1)

DazWilkin
DazWilkin

Reputation: 40061

gcloud compute ssh uses ssh and you can use -vvv to get more detailed logging to see what's going on:

gcloud compute ssh ${NAME} \
--container=${CONTAINER} \
--zone=${ZONE} \
--project=${PROJECT} \
--ssh-flag="-vvv"

If you grep through the output, you should see:

debug1: Sending command: sudo docker exec -it {CONTAINER} /bin/sh

NOTE It's using sh not bash

This should reflect the behavior of:

cloud compute ssh ${NAME} \
--zone=${ZONE} \
--project=${PROJECT} \
--command="docker exec -it {CONTAINER} /bin/sh"

Upvotes: 1

Related Questions