Jeremy
Jeremy

Reputation: 2459

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

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.

Upvotes: 81

Views: 112669

Answers (19)

Ojasv singh
Ojasv singh

Reputation: 544

am getting following error with FROM google/cloud-sdk:latest docker base image

 > [3/6] RUN gcloud components update:
1.028 Beginning update. This process may take several minutes.
1.040 ERROR: (gcloud.components.update) 
1.040 You cannot perform this action because the Google Cloud CLI component manager 
1.040 is disabled for this installation. You can run the following command 
1.040 to achieve the same result for this installation: 
1.040 
1.040 sudo apt-get update && sudo apt-get --only-upgrade install google-cloud-cli-minikube google-cloud-cli-app-engine-java google-cloud-cli-app-engine-python-extras google-cloud-cli-datastore-emulator google-cloud-cli-harbourbridge google-cloud-cli-managed-flink-client google-cloud-cli-cloud-run-proxy google-cloud-cli-pubsub-emulator google-cloud-cli google-cloud-cli-kpt google-cloud-cli-kubectl-oidc google-cloud-cli-enterprise-certificate-proxy google-cloud-cli-app-engine-grpc kubectl google-cloud-cli-nomos google-cloud-cli-config-connector google-cloud-cli-istioctl google-cloud-cli-gke-gcloud-auth-plugin google-cloud-cli-terraform-tools google-cloud-cli-app-engine-go google-cloud-cli-docker-credential-gcr google-cloud-cli-anthos-auth google-cloud-cli-bigtable-emulator google-cloud-cli-spanner-migration-tool google-cloud-cli-app-engine-python google-cloud-cli-local-extract google-cloud-cli-spanner-emulator google-cloud-cli-skaffold google-cloud-cli-cloud-build-local google-cloud-cli-anthoscli google-cloud-cli-cbt google-cloud-cli-firestore-emulator google-cloud-cli-log-streaming google-cloud-cli-package-go-module
1.040 
1.040 

Tried with lucasamorim/gcloud docker base image and it is working fine.

dockerfile:

FROM lucasamorim/gcloud

EXPOSE 1000

ENV CLOUDSDK_CORE_DISABLE_PROMPTS 1
ENV HOST_PORT 1000

ENV APP_HOME /work
WORKDIR $APP_HOME

COPY . $APP_HOME

RUN gcloud components update
RUN gcloud components update beta
RUN gcloud components install -q bigtable beta

RUN echo '#!/bin/bash' > ./start-bigtable && \
    echo 'gcloud beta emulators bigtable start --host-port "0.0.0.0:${HOST_PORT}"' >> start-bigtable && \
    chmod +x start-bigtable

CMD ["/bigtable/start-bigtable"]

Upvotes: 0

Bruno Schaatsbergen
Bruno Schaatsbergen

Reputation: 406

If you're just interested in getting the gcloud CLI available, add this to your Dockerfile:

# Downloading gcloud package
RUN curl https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-409.0.0-linux-x86_64.tar.gz > /tmp/google-cloud-cli.tar.gz

# Installing the gcloud cli
RUN mkdir -p /usr/local/gcloud \
  && tar -xf /tmp/google-cloud-cli.tar.gz \
  && ./google-cloud-sdk/install.sh --quiet

Upvotes: 0

Luciano Marqueto
Luciano Marqueto

Reputation: 1198

In Google documentation you can see the best practice https://cloud.google.com/sdk/docs/install-sdk search on the page for "Docker Tip"

eg debian use:

RUN 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-cli -y
  

Upvotes: 1

John Slavick
John Slavick

Reputation: 10619

You can use multi-stage builds to make this simpler and more efficient than solutions using curl.

FROM bitnami/google-cloud-sdk:0.392.0 as gcloud

FROM base-image-for-production:tag

# Do what you need to configure your production image

COPY --from=gcloud /opt/bitnami/google-cloud-sdk/ /google-cloud-sdk

Upvotes: 4

Jiarui Xu
Jiarui Xu

Reputation: 86

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

and export env works for me

Upvotes: 4

Galuoises
Galuoises

Reputation: 3273

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.

Upvotes: 19

Chinmay Kulkarni
Chinmay Kulkarni

Reputation: 47

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

Upvotes: 3

KJoe
KJoe

Reputation: 339

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)

Upvotes: 30

Hasni Mehdi
Hasni Mehdi

Reputation: 656

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

Upvotes: 4

Steven
Steven

Reputation: 3813

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"

Upvotes: 2

Chris Stryczynski
Chris Stryczynski

Reputation: 33861

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

Upvotes: 1

Jeremy
Jeremy

Reputation: 2459

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

Upvotes: 93

adi biton
adi biton

Reputation: 517

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

Upvotes: 33

Jes&#250;s D&#237;az
Jes&#250;s D&#237;az

Reputation: 372

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

Upvotes: 2

pushya
pushya

Reputation: 4418

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

Upvotes: 0

kross
kross

Reputation: 3753

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

Upvotes: 1

fredrik
fredrik

Reputation: 10281

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.

Upvotes: 7

hirowatari
hirowatari

Reputation: 3302

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

Upvotes: 11

Radu Gabriel
Radu Gabriel

Reputation: 3201

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/

Upvotes: 52

Related Questions