4

I'm trying to set up a Docker development environment on Windows. I have a code directory structure like so:

project/
  node-app/
  react-app-1/
  react-app-2/
  shared/

node-app, react-app-1 and react-app-2 all depend upon the code within shared. So, I was thinking of having a Dockerfile in each of the apps, with something like this:

FROM node:10.0

WORKDIR /app
COPY ../ .

WORKDIR /app/node-app
RUN npm install

However, that doesn't work - Docker gives me an error saying that it's a Forbidden path outside the build context: ../.

What is the best way to resolve this problem? I'm looking at setting up some sort of Docker Compose after I get this sorted (once again for local development), so my ideal solution would keep that in mind.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Lucas
  • 16,930
  • 31
  • 110
  • 182
  • https://docs.docker.com/engine/reference/builder/ explains how the context works. If you `docker build .` from the `project/` directory you can get the right context, and you have to specify which Dockerfile to select with `-f`/`--file`. You can also set explicit context and file in the compose file `build` section: https://docs.docker.com/compose/compose-file/#build – jonrsharpe Mar 23 '19 at 12:40
  • 1
    Possible duplicate of [How to include files outside of Docker's build context?](https://stackoverflow.com/questions/27068596/how-to-include-files-outside-of-dockers-build-context) – jonrsharpe Mar 23 '19 at 12:42

1 Answers1

5

COPY ../ . is invalid because .. is outside the build context (by default this is the directory from where the docker build command is issued).

To fix that, you could build your application from the top directory ($PWD = project/), thus making all files/directories in that directory available from the build context, and specify the Dockerfile you want to use for the build :

cd project/ && docker build -t <your_image_name> -f node-app/Dockerfile .

or :

docker build -t <your_image_name> -f /path/to/project/node-app/Dockerfile /path/to/project/

And of course replace COPY ../ . by COPY . ., since you are now building from the top directory.

norbjd
  • 10,166
  • 4
  • 45
  • 80