【问题标题】:Docker: COPY failed to find Gemfile: no such file or directoryDocker:COPY 找不到 Gemfile:没有这样的文件或目录
【发布时间】:2018-07-05 21:16:02
【问题描述】:

我目前正在通过 docker 设置 Rails 样板。当我尝试运行 docker build . 时,出现以下错误:

Step 5/8 : COPY Gemfile /app/
COPY failed: stat /var/lib/docker/tmp/docker-builder090950451/Gemfile: no such file or directory

我正在按照documentation 中的说明进行操作,但我仍然无法构建映像。

这是我的 Dockerfile

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

WORKDIR /app

# Copy the existing Gemfile
COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock

# Install Gems
RUN bundle install
COPY . /app

我还有以下docker-compose.yml 文件:

version: '3'
services:
  db:
    image: postgres # To Edit - Default is postgresql.
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db

我通过运行docker-compose run web rails new app --force --database=postgresql 生成了 Rails 应用程序,它确实生成了带有 Gemfile 的 rails 应用程序,如下图所示。

但是在生成 rails 应用程序并运行 docker-compose build 后,我收到以下错误:

db uses an image, skipping
Building web
Step 1/7 : FROM ruby:2.5
 ---> 55fb4a37704e
Step 2/7 : RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
 ---> Using cache
 ---> afb6f347904c
Step 3/7 : WORKDIR /app
 ---> Using cache
 ---> 1fdbd260685d
Step 4/7 : COPY Gemfile /app/Gemfile
ERROR: Service 'web' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder885442968/Gemfile: no such file or directory

【问题讨论】:

  • 您的 Dockerfile 在您的应用目录中吗?在 VSCode 目录结构中无法正常查看。
  • @johnharris85 它在应用程序目录中
  • 这就是问题所在,默认情况下,构建上下文是您的 Dockerfile 所在的位置,而 Gemfile 不存在其中。我去了你正在关注的文档,它们在同一个目录中。
  • 但是如果子文件夹app没有Gemfile怎么办?在app 文件夹中是controllershelpersmodels 所在的位置。
  • 你说得对,它奏效了。我不明白如何

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


【解决方案1】:

问题是我将容器目录和应用程序目录命名为相同的app,导致找不到Gemfile。

我将Dockerfile 更新为:

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

WORKDIR /webapp

COPY ./app/Gemfile /webapp/Gemfile
COPY ./app/Gemfile.lock /webapp/Gemfile.lock

RUN cd /webapp && bundle install
ADD . /webapp

在 docker-compose.yml 中,我将 volumes 更改为:

volumes:
      - .:/webapp

这仅适用于您希望 Rails 应用程序独立并且Docker 文件位于 rails 应用程序之外。

【讨论】:

  • 这很奇怪,但我完全复制了你的 Dockerfile 并且仍然出现错误:Step 4/7 : COPY ./app/Gemfile /webapp/Gemfile COPY failed: stat /var/lib/docker/tmp/docker-builder085947770/app/Gemfile: no such file or directory
【解决方案2】:

你需要删除 ./dir/

COPY ./Gemfile /webapp/Gemfile 
COPY ./Gemfile.lock /webapp/Gemfile.lock

【讨论】:

    【解决方案3】:

    这里的问题是,如果你有这条线: WORKDIR /app您已经在 polder 应用程序中。因此,当您将某些内容复制到 docker 容器中时,您不需要编写 ./app 或 /app 因为 .意味着这已经是一个应用程序。如果你写了这样的东西,它将被复制到你的 WORKDIR 中的新 app 目录中,路径将是 /app/app。 在您的情况下,Dockerfile 应如下所示:

    FROM ruby:2.5
    RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
    
    WORKDIR /app
    
    # Copy the existing Gemfile
    COPY Gemfile Gemfile.lock .
    
    # Install Gems
    RUN bundle install
    COPY . .
    

    【讨论】:

      【解决方案4】:

      我在 Ubuntu 18.04 中处理 Rails 应用程序时遇到了同样的问题。

      我收到了这个错误:

      错误:服务“应用程序”构建失败:复制失败:stat /var/lib/docker/tmp/docker-builder805666212/Gemfile:没有这样的文件或目录

      问题是我有多个 docker-compose.yml 文件,并且我将文件更改为错误的文件,该文件位于目录而不是根目录中。

      我所要做的就是删除 docker-compose.yml 并在根目录中的 docker-compose.yml 上更改文件。

      然后,我为根目录中的docker-compose.yml 文件运行以下命令:

      docker-compose build
      

      一切都恢复正常了。

      就是这样。

      我希望这会有所帮助

      【讨论】:

        【解决方案5】:

        我有一个名为 bundle 的早期卷,它被不同的 docker 映像使用。

        因此,每当我运行另一个具有不同工作目录并且还需要 bundle 卷的 docker 映像时,它都会抛出与上述类似的错误消息。

        要修复它,我只需要修剪旧卷,其中包括旧的 bundle 卷,然后重新运行构建

        docker volume prune
        
        docker build .
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-10-22
          • 2014-09-13
          • 2017-03-07
          • 1970-01-01
          • 2021-08-26
          • 2020-10-13
          • 1970-01-01
          相关资源
          最近更新 更多