使用其他图像中安装在构建器图像中的APK软件包

发布于 2025-01-24 23:05:21 字数 2567 浏览 4 评论 0原文

我有一个(多阶段)高山建筑商图像,该图像安装了一些APK软件包(Postgres)和GO应用程序。然后,我有另一个图像,该图像从此图像延伸,需要使用其中安装的APK软件包。但是,似乎APK软件包不在最终图像中。我构建了构建器映像并运行它,并且APK软件包也没有安装在其中...

构建器:

FROM golang:1.18-alpine AS base
RUN apk add --no-cache --quiet musl-dev git gcc upx postgresql-client

RUN LDFLAGS="-s -w -linkmode external -extldflags \"-static\"" &&\
    go install --ldflags "$LDFLAGS" github.com/GoogleCloudPlatform/cloudsql-proxy/cmd/cloud_sql_proxy@latest &&\
    upx /go/bin/cloud_sql_proxy

# Build the dist image
FROM alpine
COPY --from=base /go/bin/cloud_sql_proxy /

最终图像:

# syntax=docker/dockerfile:experimental

# set default PSQLCONNECTOR_VERSION value
ARG PSQLCONNECTOR_VERSION=20220425

# pull from base image
FROM gcr.io/mycrepo/psql-cloudproxy-connector:$PSQLCONNECTOR_VERSION-alpine AS base

# copy script
COPY ./some_script.sh /

# make the script executable
RUN ["chmod", "+x", "/some_script.sh"]

some_script.sh包含:

PG_URL="postgresql://postgres:password@localhost:5432/some_db?sslmode=disable"
psql "$PG_URL" -c "SELECT * FROM ...;"

psql(来自Postgresql-client PKG)在第二张图像中不存在。运行脚本后,我们将获得/some_script.sh:行35:psql:找不到

输出

apk list
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.15/main: No such file or directory
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.15/community: No such file or directory
libretls-3.3.4-r3 x86_64 {libretls} (ISC AND (BSD-3-Clause OR MIT)) [installed]
musl-1.2.2-r7 x86_64 {musl} (MIT) [installed]
zlib-1.2.12-r0 x86_64 {zlib} (Zlib) [installed]
apk-tools-2.12.7-r3 x86_64 {apk-tools} (GPL-2.0-only) [installed]
musl-utils-1.2.2-r7 x86_64 {musl} (MIT BSD GPL2+) [installed]
libssl1.1-1.1.1n-r0 x86_64 {openssl} (OpenSSL) [installed]
alpine-baselayout-3.2.0-r18 x86_64 {alpine-baselayout} (GPL-2.0-only) [installed]
alpine-keys-2.4-r1 x86_64 {alpine-keys} (MIT) [installed]
busybox-1.34.1-r5 x86_64 {busybox} (GPL-2.0-only) [installed]
scanelf-1.3.3-r0 x86_64 {pax-utils} (GPL-2.0-only) [installed]
ca-certificates-bundle-20211220-r0 x86_64 {ca-certificates} (MPL-2.0 AND MIT) [installed]
libc-utils-0.7.2-r3 x86_64 {libc-dev} (BSD-2-Clause AND BSD-3-Clause) [installed]
ssl_client-1.34.1-r5 x86_64 {busybox} (GPL-2.0-only) [installed]
libcrypto1.1-1.1.1n-r0 x86_64 {openssl} (OpenSSL) [installed]

这是最终容器中的apk列表的 在图像B中。但是似乎在同一dockerfile的多个阶段之间甚至没有共享安装的软件包?

将这些软件包复制到最终图像a的正确方法是什么?

ps。这些图像需要分开,因为我需要在不同的时间构建它们,因此单个多阶段构建无用

I have a (multi-stage) alpine builder image that installs a few apk packages (postgres) and a Go application. I then have another image that extends from this image and needs to use the apk packages installed in it. However it seems the apk packages are not in the final image. I built the builder image and ran it and the apk packages are not installed in there either...

The builder:

FROM golang:1.18-alpine AS base
RUN apk add --no-cache --quiet musl-dev git gcc upx postgresql-client

RUN LDFLAGS="-s -w -linkmode external -extldflags \"-static\"" &&\
    go install --ldflags "$LDFLAGS" github.com/GoogleCloudPlatform/cloudsql-proxy/cmd/cloud_sql_proxy@latest &&\
    upx /go/bin/cloud_sql_proxy

# Build the dist image
FROM alpine
COPY --from=base /go/bin/cloud_sql_proxy /

final image:

# syntax=docker/dockerfile:experimental

# set default PSQLCONNECTOR_VERSION value
ARG PSQLCONNECTOR_VERSION=20220425

# pull from base image
FROM gcr.io/mycrepo/psql-cloudproxy-connector:$PSQLCONNECTOR_VERSION-alpine AS base

# copy script
COPY ./some_script.sh /

# make the script executable
RUN ["chmod", "+x", "/some_script.sh"]

some_script.sh contains:

PG_URL="postgresql://postgres:password@localhost:5432/some_db?sslmode=disable"
psql "$PG_URL" -c "SELECT * FROM ...;"

psql (from postgresql-client pkg) is not present in the second image. When the script is run, we get /some_script.sh: line 35: psql: not found.

here is the output of apk list from within the final container:

apk list
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.15/main: No such file or directory
WARNING: Ignoring https://dl-cdn.alpinelinux.org/alpine/v3.15/community: No such file or directory
libretls-3.3.4-r3 x86_64 {libretls} (ISC AND (BSD-3-Clause OR MIT)) [installed]
musl-1.2.2-r7 x86_64 {musl} (MIT) [installed]
zlib-1.2.12-r0 x86_64 {zlib} (Zlib) [installed]
apk-tools-2.12.7-r3 x86_64 {apk-tools} (GPL-2.0-only) [installed]
musl-utils-1.2.2-r7 x86_64 {musl} (MIT BSD GPL2+) [installed]
libssl1.1-1.1.1n-r0 x86_64 {openssl} (OpenSSL) [installed]
alpine-baselayout-3.2.0-r18 x86_64 {alpine-baselayout} (GPL-2.0-only) [installed]
alpine-keys-2.4-r1 x86_64 {alpine-keys} (MIT) [installed]
busybox-1.34.1-r5 x86_64 {busybox} (GPL-2.0-only) [installed]
scanelf-1.3.3-r0 x86_64 {pax-utils} (GPL-2.0-only) [installed]
ca-certificates-bundle-20211220-r0 x86_64 {ca-certificates} (MPL-2.0 AND MIT) [installed]
libc-utils-0.7.2-r3 x86_64 {libc-dev} (BSD-2-Clause AND BSD-3-Clause) [installed]
ssl_client-1.34.1-r5 x86_64 {busybox} (GPL-2.0-only) [installed]
libcrypto1.1-1.1.1n-r0 x86_64 {openssl} (OpenSSL) [installed]

I was under the impression that building an image A that is extending from an image B with installed apk packages would give an image A with the packages installed in image B. But it seems the installed packages are not even shared between multiple stages in the same Dockerfile?

What is the correct way to copy these packages to the final image A?

ps. The images need to be separate because I need to build them at different times, so a single multi-stage build is not useful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚风撩人 2025-01-31 23:05:22

来自DockerFile中的第二个语句的完全将图像重置为新的基本图像。什么都没携带。从较早阶段获得一些东西的唯一方法是复制-from = ...语句。

如果您的最终图像中需要一些软件包,则应将它们安装在最终图像中。从较早的阶段复制它们将是一个困难,因为您需要知道包装已安装的文件。

在您的情况下,您应该在Alpine图像中安装软件包。然后,它们也将在您制作的最终图像中可用,即您基于高山图像。

A second FROM statement in a Dockerfile completely resets the image to the new base image. Nothing is carried over. The only way to get something from an earlier stage is the COPY --from=... statement.

If you need some packages in your final image, you should install them in the final image. Copying them from an earlier stage will be a difficult, since you'll need to know what files the package installed.

In your case, you should install the packages in the alpine image. Then they'll also be available in the final image you make, that you base on the alpine image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文