无法在 Docker 中运行 gcloud auth activate-service-account。

huangapple go评论75阅读模式
英文:

Not able to run gcloud auth activate-service-account in Docker

问题

我想在 Docker 构建期间运行 gcloud auth activate-service-account 命令,以确保我的应用程序可以访问 GCP 密钥管理器。我的 Docker 文件内容如下:

FROM maven:3.8.3-openjdk-17 as maven_builder
WORKDIR /app
COPY up-user-service/pom.xml .
COPY up-user-service/src ./src
RUN mvn clean install
RUN mv target/*.jar target/application.jar

FROM openjdk:17-jdk-alpine as builder
COPY --from=maven_builder /app/target/*.jar up-user-service/target/application.jar
RUN java -Djarmode=layertools -jar up-user-service/target/application.jar extract
FROM openjdk:17-jdk-alpine

COPY --from=builder dependencies/ ./
RUN true
COPY --from=builder snapshot-dependencies/ ./
RUN true
COPY --from=builder spring-boot-loader/ ./
RUN true

FROM ubuntu:22.04

# 安装先决条件
RUN apt-get update && apt-get install -y \
curl
CMD /bin/bash

RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
RUN apt-get update && apt-get install -y python3 python3-pip
RUN mkdir -p /usr/local/gcloud
RUN tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz
RUN /usr/local/gcloud/google-cloud-sdk/install.sh
RUN gcloud auth activate-service-account --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json

COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

即使安装了 google-cloud-sdk,我仍然遇到以下错误:

=> ERROR [stage-3 8/9] RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json                            0.3s 
------
> [stage-3 8/9] RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json:                                       
#0 0.296 /bin/sh: 1: gcloud: not found
------
Dockerfile:40
--------------------
38 |     RUN tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz
39 |     RUN /usr/local/gcloud/google-cloud-sdk/install.sh
40 | >>> RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json
41 |     
42 |     COPY --from=builder application/ ./
--------------------
ERROR: failed to solve: process "/bin/sh -c gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json" did not complete successfully: exit code: 127

为什么 Docker 容器仍然无法运行 gcloud 命令?

英文:

I want to run gcloud auth activate-service-account command during docker build to make sure my application can access GCP secrets manager. My docker file contents are as following:-

FROM maven:3.8.3-openjdk-17 as maven_builder
WORKDIR /app
COPY up-user-service/pom.xml .
COPY up-user-service/src ./src
RUN mvn clean install
RUN mv target/*.jar target/application.jar
FROM openjdk:17-jdk-alpine as builder
COPY --from=maven_builder /app/target/*.jar up-user-service/target/application.jar
RUN java -Djarmode=layertools -jar up-user-service/target/application.jar extract
FROM openjdk:17-jdk-alpine
COPY --from=builder dependencies/ ./
RUN true
COPY --from=builder snapshot-dependencies/ ./
RUN true
COPY --from=builder spring-boot-loader/ ./
RUN true
FROM ubuntu:22.04
# Install prerequisites
RUN apt-get update && apt-get install -y \
curl
CMD /bin/bash
RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
RUN apt-get update && apt-get install -y python3 python3-pip
RUN mkdir -p /usr/local/gcloud
RUN tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz
RUN /usr/local/gcloud/google-cloud-sdk/install.sh
RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json
COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

Even after installing google-cloud-sdk, I am getting following error:-

 => ERROR [stage-3 8/9] RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json                            0.3s 
------                                                                                                                                                                                 
> [stage-3 8/9] RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json:                                       
#0 0.296 /bin/sh: 1: gcloud: not found                                                                                                                                                 
------                                                                                                                                                                                 
Dockerfile:40                                                                                                                                                                          
--------------------
38 |     RUN tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz
39 |     RUN /usr/local/gcloud/google-cloud-sdk/install.sh
40 | >>> RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json
41 |     
42 |     COPY --from=builder application/ ./
--------------------
ERROR: failed to solve: process "/bin/sh -c gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json" did not complete successfully: exit code: 127

Why is the docker container still not able to run gcloud command?

答案1

得分: 1

The gcloud command was not found in the $PATH. To fix this issue, you can update the environment variable before executing the gcloud command.

ENV PATH="${PATH}:/usr/local/gcloud/google-cloud-sdk/bin"
RUN gcloud auth activate-service-account --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json

A better option would be to install Google Cloud SDK via the package manager. For more information, please check this link.

英文:

The gcloud command was not found in the $PATH. To fix this issue, you can update the environment variable before executing the gcloud command.

ENV PATH="${PATH}:/usr/local/gcloud/google-cloud-sdk/bin"
RUN gcloud auth activate-service-account  --key-file=/home/sdesai/Uplight/code/up-ms-java/application_default_credentials.json

A better option would be to install Google Cloud SDK via the package manager. For more information, please check this https://cloud.google.com/sdk/docs/install#deb

huangapple
  • 本文由 发表于 2023年6月13日 16:20:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定