10

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?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

2 Answers2

9

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 
Saeed
  • 3,255
  • 4
  • 17
  • 36
  • RUN npm install doesn't work for me npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json' I guess, it's because a volume is available after the container is built. – olek07 Feb 11 '22 at 17:22
  • Then add `COPY . /app` before `WORKDIR /app` in your `Dockerfile` to make sure files are copied – Saeed Feb 12 '22 at 06:00
  • I gave this answer a go but I am getting `ERROR: Duplicate mount points:`. This is what I have volumes: - ./build:/usr/src/app/ - ./api/database_changes:/usr/src/app/ – J.Do Oct 19 '22 at 09:31
  • 1
    @J.Do, you cannot have two same mount points (in your case `/usr/src/app/`. You should change one of them to another path. – Saeed Oct 19 '22 at 10:00
7

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/
vsergi
  • 705
  • 1
  • 6
  • 16