私有宝石在docker中未安装

10

我正在尝试使用Docker运行Rails应用程序。以下是通过Github的ssh URL安装的几个gems:

Gemfile

gem 'swagger-docs', :git => 'git@github.com:xyz/swagger-docs.git', :branch => 'my_branch'

我已经在Dockerfile中添加了keys,这可以克隆所需的仓库并从Git安装gems。

Dockerfile

RUN mkdir -p /root/.ssh
COPY ./id_rsa /root/.ssh/id_rsa

RUN chmod 700 /root/.ssh/id_rsa

RUN ssh-keygen -f /root/.ssh/id_rsa -y > /root/.ssh/id_rsa.pub

RUN ssh-keyscan github.com >> /root/.ssh/known_hosts

我构建it时(包括bundle install),一切顺利,镜像也被成功构建。但是当我运行docker-compose up时,会出现以下错误:

/usr/local/bundle/gems/bundler-1.9.2/lib/bundler/source/git/git_proxy.rb:155:in `allowed_in_path': The git source git@github.com:xyz/swagger-docs.git is not yet checked out. Please run `bundle install` before trying to start your application (Bundler::GitError)

你的Dockerfile在复制密钥之后是否包含“RUN bundle install”? - Nabeel
@NabeelAmjad 当然可以。 - Ajeet Khan
在这个例子中,id_rsa 的位置在哪里? - Mayuresh Srivastava
3个回答

2

你尝试过使用 Docker 实验性功能吗?它们有一个是专门用于在构建中使用 SSH 访问私有数据的特点,这使我能够在我的 Docker 构建中使用托管在 Github 上的私有 gem。要启用它,需要完成以下 4 个步骤:

gem 'rack', git: 'https://github.com/rack/rack'
DOCKER_BUILDKIT
$ DOCKER_BUILDKIT=1 docker build --ssh default .

# syntax=docker/dockerfile:experimental

# Make ssh dir and download public key for github.com to add it to known_hosts
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

# Add your ssh key, update bundler, and use bundler to install all your gems
RUN --mount=type=ssh ssh-add -L && gem install bundler && bundle install

使用docker实验特性中的--mount=type=ssh选项可以让docker负责保护您的ssh密钥信息,这是过去对docker的抱怨之一,使用实验特性似乎是目前我所知道的最安全、最简单的将ssh私钥传递给构建过程的方法。
最终,您的Dockerfile应该像这样:(个人示例)
# syntax=docker/dockerfile:experimental
FROM ruby:3.0.0
    
WORKDIR /usr/src/app
    
COPY Gemfile Gemfile.lock /usr/src/app/
    
# Make ssh dir and download public key for github.com to add it to known_hosts
RUN mkdir -p -m 0600 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts

# Add your ssh key, update bundler, and use bundler to install all your gems
RUN --mount=type=ssh ssh-add -L && gem install bundler && bundle install
    
COPY . .
    
EXPOSE 8080
    
CMD ["bundle", "exec", "rackup", "-o", "0.0.0.0", "-p", "8080"]

0
尝试在Gemfile中将git替换为https,即:
  gem 'swagger-docs', git: 'https://github.com:xyz/swagger-docs.git', branch: 'my_branch'

...或者在运行bundle install之前将其添加到您的Dockerfile中:

  RUN git config --global url."https://".insteadOf git://

如果什么都不起作用,那就运行两次bundle install
  ...
  RUN gem install bundler && bundle install

  CMD bundle install && rails s -p 3000 -b 0.0.0.0

1
我遇到了相同的错误,使用 https:// 而不是 git:// 也没有帮助 :( - Nathan Wallace

0

我调试这个问题的一种方法是使用 Dockerfile

RUN mkdir -p /root/.ssh
COPY ./id_rsa /root/.ssh/id_rsa

RUN chmod 700 /root/.ssh/id_rsa

RUN ssh-keygen -f /root/.ssh/id_rsa -y > /root/.ssh/id_rsa.pub

RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
COPY #copy yout Gemfile and Gemfile.lock
CMD /bin/bash

然后构建它并进行简单的Docker运行

docker build . -t test
docker run --rm -it test

现在,你可以从内部运行bundle install并慢慢查看输出...我甚至建议使用极简的Gemfile


在这个例子中,id_rsa 的位置在哪里? - Mayuresh Srivastava

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接