How to install the Google Cloud SDK in a Docker Image?

Google Cloud-Platform

Google Cloud-Platform Problem Overview


How can I build a Docker container with Google's Cloud Command Line Tool/SDK?

The script at the url https://sdk.cloud.google.com appears to require user input so doesn't work in a docker file.

Google Cloud-Platform Solutions


Solution 1 - Google Cloud-Platform

Adding the following to my Docker file appears to work.

# Downloading gcloud package
RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz

# Installing the package
RUN mkdir -p /usr/local/gcloud \
  && tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz \
  && /usr/local/gcloud/google-cloud-sdk/install.sh

# Adding the package path to local
ENV PATH $PATH:/usr/local/gcloud/google-cloud-sdk/bin

Solution 2 - Google Cloud-Platform

Use this one-liner in your Dockerfile:

RUN curl -sSL https://sdk.cloud.google.com | bash

source: https://docs.docker.com/v1.8/installation/google/

Solution 3 - Google Cloud-Platform

Doing it with alpine:

 FROM alpine:3.6

 RUN apk add --update \
 python \
 curl \
 which \
 bash

 RUN curl -sSL https://sdk.cloud.google.com | bash

 ENV PATH $PATH:/root/google-cloud-sdk/bin

Solution 4 - Google Cloud-Platform

RUN curl -sSL https://sdk.cloud.google.com > /tmp/gcl && bash /tmp/gcl --install-dir=~/gcloud --disable-prompts

This will download the google cloud sdk installer into /tmp/gcl, and run it with the parameters as follows:

  • --install-dir=~/gcloud: Extract the binaries into folder gcloud in home folder. Change this to wherever you want, for example /usr/local/bin
  • --disable-prompts: Don't show any prompts while installing (headless)

Solution 5 - Google Cloud-Platform

As an alternative, you could use the docker image provided by google namely google/cloud-sdk. https://hub.docker.com/r/google/cloud-sdk/

Solution 6 - Google Cloud-Platform

To install gcloud inside a docker container please follow the instructions here.

Basically you need to run

RUN apt-get update && \
    apt-get install -y curl gnupg && \
    echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
    curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg  add - && \
    apt-get update -y && \
    apt-get install google-cloud-sdk -y
      

inside your dockerfile. It's important you are user ROOT when you run this command, so it may necessary to add USER root before the previous command.

Solution 7 - Google Cloud-Platform

Dockerfile:

FROM centos:7

RUN yum update -y && yum install -y \
                    curl \
                    which && \
    yum clean all

RUN curl -sSL https://sdk.cloud.google.com | bash

ENV PATH $PATH:/root/google-cloud-sdk/bin

Build:

docker build . -t google-cloud-sdk

Then run gcloud:

docker run --rm \
--volume $(pwd)/assets/root/.config:/root/.config \
google-cloud-sdk gcloud

...or run gsutil:

docker run --rm \
--volume $(pwd)/assets/root/.config:/root/.config \
google-cloud-sdk gsutil

The local assets folder will contain the configuration.

Solution 8 - Google Cloud-Platform

apk upgrade --update-cache --available && \
apk add openssl && \
apk add curl python3 py-crcmod bash libc6-compat && \
rm -rf /var/cache/apk/*
curl https://sdk.cloud.google.com | bash > /dev/null
export PATH=$PATH:/root/google-cloud-sdk/bin
gcloud components update kubectl

Solution 9 - Google Cloud-Platform

I got this working with Ubuntu 18.04 using:

RUN apt-get install -y curl && curl -sSL https://sdk.cloud.google.com | bash
ENV PATH="$PATH:/root/google-cloud-sdk/bin"

Solution 10 - Google Cloud-Platform

This work for me.

FROM php:7.2-fpm

RUN apt-get update -y

RUN apt-get install -y python && \
    curl -sSL https://sdk.cloud.google.com | bash

ENV PATH $PATH:/root/google-cloud-sdk/bin

Solution 11 - Google Cloud-Platform

An example using debian as the base image:

FROM debian:stretch

RUN apt-get update && apt-get install -y apt-transport-https gnupg curl lsb-release

RUN export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)" && \
  echo "cloud SDK repo: $CLOUD_SDK_REPO" && \
  echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
  curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \
  apt-get update -y && apt-get install google-cloud-sdk -y

Solution 12 - Google Cloud-Platform

I was using Python Alpine image python:3.8.6-alpine3.12 as base and this worked for me:

RUN apk add --no-cache bash

RUN wget https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-327.0.0-linux-x86_64.tar.gz \
    -O /tmp/google-cloud-sdk.tar.gz | bash

RUN mkdir -p /usr/local/gcloud \
    && tar -C /usr/local/gcloud -xvzf /tmp/google-cloud-sdk.tar.gz \
    && /usr/local/gcloud/google-cloud-sdk/install.sh -q

ENV PATH $PATH:/usr/local/gcloud/google-cloud-sdk/bin
  • After building and running the image, you can check if google-cloud-sdk is installed by running docker exec -i -t <container_id> /bin/bash and running this:
bash-5.0# gcloud --version
Google Cloud SDK 327.0.0
bq 2.0.64
core 2021.02.05
gsutil 4.58
bash-5.0# gsutil --version
gsutil version: 4.58

If you want a specific version of google-cloud-sdk, you can visit https://storage.cloud.google.com/cloud-sdk-release

Solution 13 - Google Cloud-Platform

I used most of these examples in some form (thanks @KJoe), but I had to do several other things to setup everything so gcloud would work in the environment. Note that it is preferable to limit the number of lines (it limits layers needed to pull)

Here's a more complete example of Dockerfile with gcloud setup and extending a CircleCI image:

FROM circleci/ruby:2.4.1-jessie-node-browsers

# user is circleci in the FROM image, switch to root for system lib installation
USER root

ENV CCI /home/circleci
ENV GTMP /tmp/gcloud-install
ENV GSDK $CCI/google-cloud-sdk
ENV PATH="${GSDK}/bin:${PATH}"

# do all system lib installation in one-line to optimize layers
RUN curl -sSL https://sdk.cloud.google.com > $GTMP && bash $GTMP --install-dir=$CCI --disable-prompts \
  && rm -rf $GTMP \
  && chmod +x $GSDK/bin/* \
  \
  && chown -Rf circleci:circleci $CCI

# change back to the user in the FROM image
USER circleci

# setup gcloud specifics to your liking
RUN gcloud config set core/disable_usage_reporting true \
  && gcloud config set component_manager/disable_update_check true \
  && gcloud components install alpha beta kubectl --quiet

Solution 14 - Google Cloud-Platform

My use case was to generate a google bearer token using the service account, so I wanted the docker container to install gcloud this is how my docker file looks like

FROM google/cloud-sdk
# Setting the default directory in container
WORKDIR /usr/src/app
# copies the app source code to the directory in container
COPY . /usr/src/app
CMD ["/bin/bash","/usr/src/app/token.sh"]

If you need to examine a container after it is built but that isn't running use docker run --rm -it <container-build-id> bash -il and type in gcloud --version if installed correctly or not

Solution 15 - Google Cloud-Platform

curl https://sdk.cloud.google.com | bash -s -- --disable-prompts

and export env works for me

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeremyView Question on Stackoverflow
Solution 1 - Google Cloud-PlatformJeremyView Answer on Stackoverflow
Solution 2 - Google Cloud-PlatformRadu GabrielView Answer on Stackoverflow
Solution 3 - Google Cloud-Platformadi bitonView Answer on Stackoverflow
Solution 4 - Google Cloud-PlatformKJoeView Answer on Stackoverflow
Solution 5 - Google Cloud-PlatformhirowatariView Answer on Stackoverflow
Solution 6 - Google Cloud-PlatformGaluoisesView Answer on Stackoverflow
Solution 7 - Google Cloud-PlatformfredrikView Answer on Stackoverflow
Solution 8 - Google Cloud-PlatformHasni MehdiView Answer on Stackoverflow
Solution 9 - Google Cloud-PlatformStevenView Answer on Stackoverflow
Solution 10 - Google Cloud-PlatformJesús DíazView Answer on Stackoverflow
Solution 11 - Google Cloud-PlatformChris StryczynskiView Answer on Stackoverflow
Solution 12 - Google Cloud-PlatformChinmay KulkarniView Answer on Stackoverflow
Solution 13 - Google Cloud-PlatformkrossView Answer on Stackoverflow
Solution 14 - Google Cloud-PlatformpushyaView Answer on Stackoverflow
Solution 15 - Google Cloud-PlatformJiarui XuView Answer on Stackoverflow