Krishnadas PC
Krishnadas PC

Reputation: 6519

Docker compose ERROR: Service 'web' failed to build : COPY failed: forbidden path

I'm following the official docker tutorial to set up rails in docker the link of the same is given below

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

My 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

The error which I'm getting when run this command

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

Error:

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

My dir structure is

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

0 directories, 5 files

I'm relatively new to docker setups. I saw similar kind of errors in SO but their set up files are different so couldn't understood how to fix it.

Upvotes: 4

Views: 3564

Answers (1)

Eitank
Eitank

Reputation: 682

You are getting the error because you are trying to copy a file that is outside of the build context. the build context according to the documentation of docker-compose:

Either a path to a directory containing a Dockerfile, or a url to a git repository.

When the value supplied is a relative path, it is interpreted as relative to the location of the Compose file. This directory is also the build context that is sent to the Docker daemon.

try changing COPY ../compose /myapp to COPY . . and run the build command in a terminal within the directory

Upvotes: 3

Related Questions