简体   繁体   English

Docker:构建 Debian 11 镜像,其中包含 Python 和 Google Cloud SDK

[英]Docker: Build Debian 11 image, with Python and Google Cloud SDK in it

I want ot build a Docker image with the following features:我想要构建具有以下功能的 Docker 图像:

  • Must have a Debian 11 base image ( Mandatory )必须有一个 Debian 11 基本图像(强制
  • On top of it, I also need installed:最重要的是,我还需要安装:
    • Python 3. Python 3.
      • spaCy package (There could be others of my choice). spaCy package(可能还有其他我选择的)。
    • Google Cloud SDK (I will need this to run commands using gcloud and gsutil ). Google Cloud SDK(我需要它来使用gcloudgsutil运行命令)。
      • Most likely, I will download some files stored in some Cloud Storage bucket.最有可能的是,我会下载一些存储在某个 Cloud Storage 存储桶中的文件。

I decided to use gcr.io/cloud-marketplace/google/debian11:latest because as far as I understood, that base image should have both Python and Google Cloud SDK already installed on top of it, but it did not, so I started adding what I needed on my own.我决定使用gcr.io/cloud-marketplace/google/debian11:latest因为据我所知,该基本映像应该已经安装了 Python 和 Google Cloud SDK,但它没有,所以我开始添加我自己需要的东西。

My first Dockerfile attempt (defaults) looks as follows:我的第一个 Dockerfile 尝试(默认)如下所示:

FROM gcr.io/cloud-marketplace/google/debian11:latest   
RUN apt-get update -y

# Suggested by @JohnHanley, in the comments section
RUN apt-get install apt-transport-https ca-certificates gnupg -y
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

# Back to my original implementation
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
RUN pip install spacy==3.2.1
CMD ["gsutil","--version"]

The error triggered:错误触发:

------
 > [4/6] 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:
#6 0.403 deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main
#6 0.403 /bin/sh: 1: curl: not found
#6 0.442 Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
#6 0.453 gpg: no valid OpenPGP data found.
------
executor failed running [/bin/sh -c 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]: exit code: 2

My second Dockerfile attempt ( "If apt-key command is not supported" ) looks as follows:我的第二次 Dockerfile 尝试( “如果不支持apt-key命令” )如下所示:

FROM gcr.io/cloud-marketplace/google/debian11:latest   
RUN apt-get update -y

# Suggested by @JohnHanley, in the comments section
RUN apt-get install apt-transport-https ca-certificates gnupg -y
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 | tee /usr/share/keyrings/cloud.google.gpg && apt-get update -y && \
    apt-get install google-cloud-sdk -y

# Back to my original implementation
RUN apt-get install -y python3
RUN apt-get install -y python3-pip
RUN pip install spacy==3.2.1
CMD ["gsutil","--version"]

The error triggered:错误触发:

------
 > [4/6] 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 | tee /usr/share/keyrings/cloud.google.gpg && apt-get update -y &&     apt-get install google-cloud-sdk -y:
#7 0.423 deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main
#7 0.423 /bin/sh: 1: curl: not found
#7 0.974 Get:1 http://deb.debian.org/debian bullseye InRelease [116 kB]
#7 1.130 Get:2 http://deb.debian.org/debian bullseye-updates InRelease [44.1 kB]
#7 1.215 Get:3 http://deb.debian.org/debian-security bullseye-security InRelease [48.4 kB]
#7 1.373 Get:4 http://packages.cloud.google.com/apt cloud-sdk InRelease [6751 B]
#7 1.420 Err:4 http://packages.cloud.google.com/apt cloud-sdk InRelease
#7 1.420   The following signatures couldn't be verified because the public key is not available: NO_PUBKEY B53DC80D13EDEF05 NO_PUBKEY FEEA9169307EA071
#7 1.431 Get:5 http://deb.debian.org/debian-security bullseye-security/main amd64 Packages [262 kB]
#7 1.590 Reading package lists...
#7 1.985 W: GPG error: http://packages.cloud.google.com/apt cloud-sdk InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY B53DC80D13EDEF05 NO_PUBKEY FEEA9169307EA071
#7 1.985 E: The repository 'http://packages.cloud.google.com/apt cloud-sdk InRelease' is not signed.
------
executor failed running [/bin/sh -c 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 | tee /usr/share/keyrings/cloud.google.gpg && apt-get update -y &&     apt-get install google-cloud-sdk -y]: exit code: 100

If the section where Google Cloud SDK is installed gets commented, the rest works OK.如果安装 Google Cloud SDK 的部分得到评论,则 rest 工作正常。 Another important info:另一个重要信息:

  • The base OS:基本操作系统:
Edition Windows 10 Pro
Version 22H2
Installed on    ‎10/‎27/‎2022
OS build    19045.2251
Experience  Windows Feature Experience Pack 120.2212.4180.0
  • Docker version: Docker version 20.10.21, build baeda1f Docker版本: Docker version 20.10.21, build baeda1f

QUESTIONS:问题:

  1. What am I doing wrong?我究竟做错了什么?
  2. Is there a better way to do what I want?有没有更好的方法来做我想做的事? (Image size can have up to 10GB in size, so optimization might be optional for this case). (图像大小最多可达 10GB,因此对于这种情况,优化可能是可选的)。

UPDATES: This section will include lengthy answers to questions raised in the comment sections.更新:本部分将包括对评论部分中提出的问题的冗长回答。

  • 2022-12-02, @JohnHanley suggested in thew comments, to follow Google official documentation (section "Docker Tip" ) to write the Dockerfiles, which I was already doing, but did not include in the original post; 2022-12-02,@JohnHanley 在评论中建议,按照 Google 官方文档“Docker 提示”部分)编写 Dockerfile,我已经在做,但没有包含在原始帖子中; I am including that now.我现在包括在内。 OS and Dockerfile versions are included also.操作系统和 Dockerfile 版本也包括在内。

You can install gcloud cli in your container with the following way:您可以通过以下方式在您的容器中安装gcloud cli:

FROM gcr.io/cloud-marketplace/google/debian11:latest   

RUN apt-get update -y
RUN apt install curl -y

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

WORKDIR /
RUN export CLOUD_SDK_VERSION="410.0.0" && \
    curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
    tar xzf google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
    rm google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \
    ln -s /lib /lib64

RUN gcloud config set core/disable_usage_reporting true && \
    gcloud config set component_manager/disable_update_check true && \
    gcloud config set metrics/environment github_docker_images && \
    gcloud -q components install beta kubectl

ENTRYPOINT [ "sh", "-c", "gcloud --version" ]

This part is not mandatory:这部分不是强制性的:

RUN gcloud config set core/disable_usage_reporting true && \
    gcloud config set component_manager/disable_update_check true && \
    gcloud config set metrics/environment github_docker_images && \
    gcloud -q components install beta kubectl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM