Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COPY failed: forbidden path outside the build context docker compose [duplicate]

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?

like image 957
Sunil Garg Avatar asked Mar 24 '26 18:03

Sunil Garg


1 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 
like image 187
Saeed Avatar answered Mar 26 '26 11:03

Saeed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!