"Forbidden path outside the build context"
I was recently attempting to Dockerize a Node project, so I added a docker
folder to the project and created a simple Dockerfile
to get started:
FROM node:alpine
WORKDIR /app
COPY ../ ./
RUN npm install
EXPOSE 80
ENTRYPOINT [ "npm", "start" ]
However, when I tried to build the image, I got the following output:
$ docker build -t node-app .
Sending build context to Docker daemon 53.76kB
Step 1/6 : FROM node:alpine
---> 04a3ba95f191
Step 2/6 : WORKDIR /app
---> 85fff9a66b84
Removing intermediate container e2c838595c90
Step 3/6 : COPY ../ ./
COPY failed: Forbidden path outside the build context: ../ ()
Note the error: COPY failed: Forbidden path outside the build context: ../ ()
According to Docker's COPY
documentation:
COPY
obeys the following rules:
- The
<src>
path must be inside the context of the build; you cannotCOPY ../something /something
, because the first step of adocker build
is to send the context directory (and subdirectories) to the docker daemon.
It turns out that you cannot include files outside Docker's build context. However, you can copy files from the Dockerfile's parent directory.
How to COPY
Files from the Dockerfile Parent Directory
I tried several combinations of command line arguments to include the parent directory in the context, and finally landed on the solution: start from the parent directory, and pass the Dockerfile as an argument.
To do this, first update the COPY
command to use the current (./
) directory:
FROM node:alpine
WORKDIR /app
COPY ./ ./
RUN npm install
EXPOSE 80
ENTRYPOINT [ "npm", "start" ]
Next, cd ..
into the parent directory and run docker build
:
~/Code/node-app/docker
$ cd ..
~/Code/node-app
$ docker build -t node-app -f docker/Dockerfile .
Sending build context to Docker daemon 263.7MB
...
Successfully built d296bf765369
Successfully tagged node-app:latest
Success!
Broader Topics Related to Include Files Outside Docker Build Context
Docker
A set of tools to build and run containers