Docker COPY issue – “no such file or directory”

dockernode.js

In my Dockerfile I have the following 'COPY" statement:

# Copy app code
COPY /srv/visitor /srv/visitor

It should go without saying that in my host system, under the "/srv/visitor" directory, there is indeed my source code:

[root@V12 visitor]# ls /srv/visitor/
Dockerfile  package.json  visitor.js

Now, when I try to build an image using this Dockerfile it hangs at the step when the "COPY" is supposed to happen:

Step 10 : COPY /srv/visitor /srv/visitor
INFO[0155] srv/visitor: no such file or directory

It says that there is no such directory, but there clearly is.

Any ideas?

UPDATE 1:

It has been pointed to me that I was mistaken, in the way I understood build context. The suggestion amounted to changing the "COPY" statement to this:

COPY . /srv/visitor

The problem is that I had it this way, and the build process halted at the very next step:

RUN npm install

It said something along the lines of "no package.json file found", when there clearly is one.

UPDATE 2:

I tried running it with this change in the Dockerfile:

COPY source /srv/visitor/

It halted when trying to run npm:

Step 12 : RUN npm install
 ---> Running in ae5e2a993e11
npm ERR! install Couldn't read dependencies
npm ERR! Linux 3.18.5-1-ARCH
npm ERR! argv "/usr/bin/node" "/usr/sbin/npm" "install"
npm ERR! node v0.10.36
npm ERR! npm  v2.5.0
npm ERR! path /package.json
npm ERR! code ENOPACKAGEJSON
npm ERR! errno 34

npm ERR! package.json ENOENT, open '/package.json'
npm ERR! package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.

npm ERR! Please include the following file with any support request:
npm ERR!     /npm-debug.log
INFO[0171] The command [/bin/sh -c npm install] returned a non-zero code: 34

So, has the copy been performed? If yes, why is npm unable to find package.json?

Best Answer

From the documentation :

The <src> 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.

When you use /srv/visitor you are using an absolute path outside of the build context even if it's actually the current directory.

You better organize your build context like this :

├── /srv/visitor
│   ├── Dockerfile
│   └── resources
│       ├── visitor.json
│       ├── visitor.js

And use :

COPY resources /srv/visitor/

Note:

docker build - < Dockerfile does not have any context.

Hence use,

docker build .