How to Install the Google Cloud SDK in a Docker Image

To install the Google Cloud SDK in a Docker image, you can create a Dockerfile that uses an appropriate base image and then adds the necessary steps to install the SDK.

Here’s an example of DockerFile using the ubuntu:20.04 base image:

# Use the official Ubuntu 20.04 image as the base
FROM ubuntu:20.04

# Set the environment variable for non-interactive installations
ENV DEBIAN_FRONTEND=noninteractive

# Install required dependencies
RUN apt-get update && \
 apt-get install -y curl apt-transport-https ca-certificates gnupg lsb-release

# Add the Google Cloud SDK repository
RUN echo "deb [signed-by=/usr/share/keyrings/] cloud-sdk main" | \
 tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
 curl | \
 gpg --dearmor -o /usr/share/keyrings/

# Install the Google Cloud SDK
RUN apt-get update && \
 apt-get install -y google-cloud-sdk

# Set the working directory

# Start a new shell to use the installed SDK
CMD ["/bin/bash"]

First, save the contents above in a file named Dockerfile. Then, you can build the Docker image using the following command:

docker build -t my-gcloud-sdk-image .

Once the image is built, you can run a container using the new image with this command:

docker run -it --rm my-gcloud-sdk-image

This command will start an interactive shell inside the container, where you can access the gcloud command.

Remember to replace my-gcloud-sdk-image with the desired name for your Docker image.

That’s it.

Leave a Comment