Docker 撰写错误:服务“web”无法构建:复制失败:禁止路径

时间:2021-05-22 13:28:03

标签: docker docker-compose

我正在按照官方的 docker 教程在 docker 中设置 rails,下面给出了相同的链接

https://docs.docker.com/samples/rails/

我的 Dockerfile

# syntax=docker/dockerfile:1
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY ../compose /myapp

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

version: "3.9"
services:
  db:
    image: postgres
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: password
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/myapp
    ports:
      - "3000:3000"
    depends_on:
      - db

运行此命令时出现的错误

docker-compose run --no-deps web rails new . --force --database=postgresql

错误:

Step 7/12 : COPY ../compose /myapp
ERROR: Service 'web' failed to build : COPY failed: forbidden path outside the build context: ../compose ()

我的目录结构是

docker-rails tree.
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── docker-compose.yml
└── entrypoint.sh

0 directories, 5 files

我对 docker 设置比较陌生。我在 SO 中看到了类似的错误,但它们的设置文件不同,因此无法理解如何修复它。

1 个答案:

答案 0 :(得分:2)

您收到错误是因为您试图复制构建上下文之外的文件。根据 docker-compose 文档的构建上下文:

<块引用>

包含 Dockerfile 的目录的路径,或指向 git 仓库。

当提供的值是相对路径时,它被解释为 相对于 Compose 文件的位置。这个目录也是 发送到 Docker 守护进程的构建上下文。

尝试将 COPY ../compose /myapp 更改为 COPY . . 并在目录中的终端中运行构建命令