构建 docker 个图像而不向注册表公开秘密
Build docker images without exposing secrets to the registry
什么有效:
我确实有一个 php-fpm
docker 容器托管一个 PHP 应用程序,该应用程序使用 composer 来管理依赖项。 Jenkins 构建容器,运行s composer install
并将其推送到注册表。
什么应该有效:
我想包含一个来自 git 的私人包与作曲家,需要身份验证。因此,容器必须拥有不应该泄露给容器注册表的秘密。
如何从私有存储库安装 composer 包而不将秘密暴露给注册表?
什么不起作用:
- 让詹金斯运行
composer install
。开发环境需要在构建时安装依赖项。
- 在构建过程中复制和复制 ssh 密钥,因为这会将它保存到层中。
我还有哪些其他选择?
因为可能有更好的解决方案,我的是使用 docker multi stage builds to have the build process in an early layer that is not included in the final image. That way the container registry never sees the secrets. To verify that I used dive。
请看下面的Dockerfile
FROM php-fpm
COPY ./id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa
RUN wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - -q | php -- --quiet
COPY ./src /var/www/html
RUN composer install
FROM php-fpm
COPY --from=0 /var/www/html/vendor /var/www/html/vendor
什么有效:
我确实有一个 php-fpm
docker 容器托管一个 PHP 应用程序,该应用程序使用 composer 来管理依赖项。 Jenkins 构建容器,运行s composer install
并将其推送到注册表。
什么应该有效: 我想包含一个来自 git 的私人包与作曲家,需要身份验证。因此,容器必须拥有不应该泄露给容器注册表的秘密。
如何从私有存储库安装 composer 包而不将秘密暴露给注册表?
什么不起作用:
- 让詹金斯运行
composer install
。开发环境需要在构建时安装依赖项。 - 在构建过程中复制和复制 ssh 密钥,因为这会将它保存到层中。
我还有哪些其他选择?
因为可能有更好的解决方案,我的是使用 docker multi stage builds to have the build process in an early layer that is not included in the final image. That way the container registry never sees the secrets. To verify that I used dive。
请看下面的Dockerfile
FROM php-fpm
COPY ./id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa
RUN wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - -q | php -- --quiet
COPY ./src /var/www/html
RUN composer install
FROM php-fpm
COPY --from=0 /var/www/html/vendor /var/www/html/vendor