COPY failed: forbidden path outside the build context docker compose

Viewed 14441

THis is the project structure

Project
 /deployment
   /Dockerfile
   /docker-compose.yml
 /services
   /ui
     /widget

Here is the docker file

FROM node:14

WORKDIR /app

USER root

# create new user (only root can do this) and assign owenership to newly created user
RUN echo "$(date '+%Y-%m-%d %H:%M:%S'): ======> Setup Appusr" \
    && groupadd -g 1001 appusr \
    && useradd -r -u 1001 -g appusr appusr \
    && mkdir /home/appusr/ \
    && chown -R appusr:appusr /home/appusr/\
    && chown -R appusr:appusr /app

# switch to new created user so that appuser will be responsible for all files and has access
USER appusr:appusr

COPY ../services/ui/widget/ /app/
COPY ../.env /app/

# installing deps
RUN npm install 

and docker-compose

version: "3.4"

x-env: &env
  HOST: 127.0.0.1

services:
  widget:
    build:
      dockerfile: Dockerfile
      context: .
    ports:
      - 3002:3002
    command:
      npm start
    environment:
      <<: *env
    restart: always

and from project/deplyment/docker-compose up it shows

Step 6/8 : COPY ../services/ui/widget/ /app/
ERROR: Service 'widget' failed to build : COPY failed: forbidden path outside the build context: ../services/ui/widget/ ()

am i setting the wrong context?

2 Answers

You cannot COPY or ADD files outside the current path where Dockerfile exists.

You should either move these two directories to where Dockerfile is and then change your Dockerfile to:

COPY ./services/ui/widget/ /app/
COPY ./.env /app/

Or use volumes in docker-compose, and remove the two COPY lines.

So, your docker-compose should look like this:

x-env: &env
  HOST: 127.0.0.1

services:
  widget:
    build:
      dockerfile: Dockerfile
      context: .
    ports:
      - 3002:3002
    command:
      npm start
    environment:
      <<: *env
    restart: always
    volumes:
      - /absolute/path/to/services/ui/widget/:/app/
      - /absolute/path/to/.env/:/app/

And this should be your Dockerfile if you use volumesindocker-compose`:

FROM node:14

WORKDIR /app

USER root

# create new user (only root can do this) and assign owenership to newly created user
RUN echo "$(date '+%Y-%m-%d %H:%M:%S'): ======> Setup Appusr" \
    && groupadd -g 1001 appusr \
    && useradd -r -u 1001 -g appusr appusr \
    && mkdir /home/appusr/ \
    && chown -R appusr:appusr /home/appusr/\
    && chown -R appusr:appusr /app

# switch to new created user so that appuser will be responsible for all files and has access
USER appusr:appusr

# installing deps
RUN npm install 

You problem is that you are referencing a file which is outside Dockerfile context. By default, is the location from where you execute the build command.

From docker documentation - Copy section:

The path must be inside the context of the build; you cannot COPY ../something /something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

However, you can use the parameter -f to specify the dockerfile independently of the folder you are running your build. So you could use the next line executing it from projects:

docker build -f ./deployment/Dockerfile .

You will need to modify your copy lines as well to point at the right location.

COPY ./services/ui/widget/ /app/
COPY ./.env /app/
Related