Docker:COPY 未能找到 Gemfile:没有这样的文件或目录

Ste*_*lar 3 ruby-on-rails docker docker-compose

我目前正在通过 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
Run Code Online (Sandbox Code Playgroud)

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

这是我的 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
Run Code Online (Sandbox Code Playgroud)

我还有以下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
Run Code Online (Sandbox Code Playgroud)

我通过运行生成了 Rails 应用docker-compose run web rails new app --force --database=postgresql程序,它使用 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
Run Code Online (Sandbox Code Playgroud)

Ste*_*lar 6

问题是我将容器目录和应用程序目录命名为相同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
Run Code Online (Sandbox Code Playgroud)

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

volumes:
      - .:/webapp
Run Code Online (Sandbox Code Playgroud)

只有当您想让 Rails 应用程序独立并且Docker文件存在于 Rails 应用程序之外时,才会这样做。