【问题标题】:Issue with docker and bundlerdocker 和 bundler 的问题
【发布时间】:2015-09-29 22:36:24
【问题描述】:

我在使用 docker/docker-compose 和 bundler 时遇到了一些问题。构建映像后,我可以正确运行rails server,没有任何问题,但是,当我尝试使用rails console 运行控制台时,我不断得到:

Could not find i18n-0.7.0 in any of the sources
Run `bundle install` to install missing gems.

如果我尝试在容器中运行bundle install 没有问题,一切似乎都已正确安装。

docker-compose run web bundle install

...
Using spring 1.3.6
Using therubyracer 0.12.2
Using turbolinks 2.5.3
Using uglifier 2.7.2
Using web-console 2.2.1
Updating files in vendor/cache
Bundle complete! 24 Gemfile dependencies, 119 gems now installed.
Bundled gems are installed into /usr/local/bundle.

我的docker-compose.yml 看起来像这样:

db:
  image: postgres
web:
  build: .
  command: rails s -p 3000 -b 0.0.0.0
  ports:
    - "3000:3000"
  volumes:
    - .:/app
    - ./github_rsa:/root/.ssh/id_rsa
  links:
    - db

我需要使用 ssh 密钥挂载一个卷,因为有一些 gem 需要从私有存储库中提取。

我的Dockerfile 看起来像这样:

FROM ruby:2.2.0

RUN apt-get update -qq && apt-get install -y build-essential \
    libpq-dev \
    libxml2-dev libxslt1-dev \
    nodejs

ENV HOME /root
ENV APP_HOME /app

RUN mkdir $APP_HOME
RUN mkdir $APP_HOME/tmp
RUN mkdir $APP_HOME/log

# Copy the Gemfile and Gemfile.lock into the image.
# Temporarily set the working directory to where they are.
WORKDIR /tmp
ADD Gemfile Gemfile
ADD Gemfile.lock Gemfile.lock

# Copy the github key for pulling gems
COPY github_rsa /root/.ssh/id_rsa

RUN \
    chown -R root:root /root/.ssh && \
    chmod 700 $HOME/.ssh && \
    chmod 600 $HOME/.ssh/id_rsa

RUN echo "Host github.com\n\tStrictHostKeyChecking no\n" >> /root/.ssh/config

# Start ssh agent and add keys
RUN eval "$(ssh-agent)" && \
    ssh-add && \
    ssh-add -l

# Install bundler
RUN gem install bundler -v '~> 1.10'

# Install ruby dependencies
RUN bundle install

# Remove the ssh key now, we don't want to ship secrets on our images
RUN rm /root/.ssh/id_rsa

# Add app to container
ADD . $APP_HOME

# Add container working directory
WORKDIR $APP_HOME

# Expose puma port
EXPOSE 3000

【问题讨论】:

    标签: ruby-on-rails docker bundler docker-compose


    【解决方案1】:

    您需要立即删除此图片。以下方法不起作用:

    RUN rm /root/.ssh/id_rsa
    

    您不能像这样删除文件;它们仍将存在于先前的层中,并且任何拥有该图像的人都可以访问它们。目前,您正在传送您的秘密。

    关于实际问题,我怀疑这只是与工作目录和路径有关。尝试将 RUN bundle install 移动到 WORKDIR 指令之后。

    【讨论】:

    • Adrian,感谢您指出 ssh 密钥的问题!你知道如何改进它吗?我需要 ssh 密钥来拉动!我会尝试您的建议,但我认为宝石缓存会随着更改而丢失。
    • 老实说,最好的办法是把代码拉到 Dockerfile 之外,然后COPY 把它拉进去。
    • 关于bundle install,我的建议只是为了弄清楚它是否是目录/路径问题,不一定是最终解决方案。
    • 捆绑安装建议不起作用 :(。我真的很沮丧不知道发生了什么!rails server 有效而 rails console 无效是没有意义的。
    • 找出问题所在。这是我的.bundle/config 文件的问题。不知道那是存在的。它在vendor/cache 中安装了我的gem,我也在安装. 卷,所以发生了奇怪的事情。现在唯一的问题是,每次我必须更新 Gemfile 时,我都需要用所有 gem 重建图像! :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    相关资源
    最近更新 更多