在 dockerfile (w composer.json) 中以“正确的方式”添加私有 GitHub 存储库

在 dockerfile (w composer.json) 中以“正确的方式”添加私有 GitHub 存储库

我正在尝试通过 composer.json 文件添加我的私有 GitHub 存储库,同时构建 docker 镜像。但无论我怎么尝试,都无法让它工作。

我想要尽可能简单的方法,它不必是最安全的,但至少是可以接受的。我希望可以使用“个人访问令牌”来实现。

这是我的尝试;

FROM php:8-fpm

# Set working directory
WORKDIR /var/www

# Set args
ARG GIT_ACCESS_TOKEN
ARG GIT_PRIVATE_KEY
ARG GIT_HASH
ENV GIT_HASH=$GIT_HASH

# add credentials on build
#RUN touch ~/.composer/auth.json
RUN mkdir ~/.composer
RUN echo '{"github-oauth":{"github.com": "${GIT_ACCESS_TOKEN}"}}' > ~/.composer/auth.json

# Install dependencies
RUN apt-get update && apt-get install -y \
    nano \
    build-essential \
    default-mysql-client \
    locales \
    zip \
    libzip-dev \
    unzip \
    git \
    curl \
    libssl-dev \
    libonig-dev

# Install extensions
RUN docker-php-ext-install opcache pdo_mysql mbstring zip ftp mysqli bcmath

# GitHub access to LCMS
RUN git config --global url."https://${GIT_ACCESS_TOKEN}@github.com".insteadOf "ssh://[email protected]"

RUN mkdir -p ~/.ssh/ && \
    echo ${GIT_ACCESS_TOKEN} > ~/.ssh/id_rsa && \
    chmod -R 600 ~/.ssh/ && \
    ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

# Install composer
# Copy composer.lock and composer.json
COPY ./web/composer.json /var/www/
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install vendor dependencies through composer
RUN composer install

# Install opache settings for php
COPY ./web/nginx/php.ini $PHP_INI_DIR/conf.d/opcache.ini

# Copy existing application directory contents
COPY ./web /var/www

# Clean up
RUN apt-get remove -y git && apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Expose port 9000 and start php-fpm server
EXPOSE 9000

我总是遇到 GitHub 的错误。如果我运行上面的代码,就会出现此错误;

> [11/14] RUN composer install:                                                                                    
#15 0.196 Do not run Composer as root/super user! See https://getcomposer.org/root for details                      
#15 0.226 No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
#15 0.226 Loading composer repositories with package information
#15 0.877 
#15 0.883                                                                                                                                           
#15 0.883   [RuntimeException]                                                                                                                      
#15 0.883   Failed to execute git clone --mirror -- '[email protected]/xxxxx' '/root/.composer/cache/vcs/git-github.com-xxxxxxx/'  
#15 0.883                                                                                                                                           
#15 0.883   Cloning into bare repository '/root/.composer/cache/vcs/git-github.com-xxxxxxx'...                                             
#15 0.883   Warning: Permanently added the RSA host key for IP address '140.82.121.3' to the list of known hosts.                                   
#15 0.883   Load key "/root/.ssh/id_rsa": invalid format                                                                                            
#15 0.883   [email protected]: Permission denied (publickey).                                                                                          
#15 0.883   fatal: Could not read from remote repository.                                                                                           
#15 0.883                                                                                                                                           
#15 0.883   Please make sure you have the correct access rights                                                                                     
#15 0.883   and the repository exists.

有人有建议吗?

答案1

您的个人访问令牌不能用作 SSH 密钥,而是您的个人 GitHub 密码的替代品,并且只能用于 HTTPS 连接。

一个可以工作的最小 Dockerfile 如下:

FROM php:8-fpm

ARG GIT_ACCESS_TOKEN

RUN apt-get update && apt-get install -y git

RUN git clone https://yourusername:${GIT_ACCESS_TOKEN}@github.com/yourusername/yourrepo.git

然后您可以在构建命令行上使用 ARG:

docker build --build-arg GIT_ACCESS_TOKEN="YOURLONGACCESSTOKEN" .

但:

所有有权访问您图像的人都可以看到您的访问令牌。

这在Dockerfile 文档

警告:

不建议使用构建时变量来传递诸如 github 密钥、用户凭据等机密信息。使用该docker history命令的任何图像用户都可以看到构建时变量值。

你应该使用多阶段构建或使用较新的构建机密以防止这种情况发生。

相关内容