我目前正在通过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应用程序,如下图所示。 ruby-on-rails - Docker:COPY找不到Gemfile:没有这样的文件或目录-LMLPHP

但是,在生成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

最佳答案

问题是我将容器目录和应用程序目录命名为相同的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应用程序之外时,才需要这样做。

10-08 08:08
查看更多