52

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?

dlyk1988
  • 1,644
  • 4
  • 24
  • 36
  • For those looking for an issue in 2017 - this may be your issue https://github.com/docker/for-mac/issues/1922. it recommends deleting you .dockerignore file and retesting. If that works you can fiddle with your settings in .dockerignore to resolve the issue. – Undefined Oct 03 '17 at 13:48

12 Answers12

50

For me the directory was in the correct context, only it was included in the (hidden) .dockerignore file in the root of the project. This leads to the error message:

lstat mydir/myfile.ext: no such file or directory
DHFW
  • 3
  • 1
AlcaDotS
  • 601
  • 5
  • 4
49

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 .

viru
  • 105
  • 3
Xavier Lucas
  • 12,815
  • 2
  • 44
  • 50
  • I am already inside the "/srv/visitor" directory of my host system, and all my source code, plus the Dockerfile, is here. How should I write my "COPY" statement so that all this source gets copied inside the container's "/srv/visitor" directory? – dlyk1988 Feb 09 '15 at 14:00
  • 1
    @dsljanus The source directory or file must be **relative** to the build context i.e. `/srv/visitor` directory. – Xavier Lucas Feb 09 '15 at 14:07
  • So, should it be "."? Because I had it that way, and the build process halted at the next step, "RUN npm install". It said something along the lines of "no package.json file found". Please see my update also. – dlyk1988 Feb 09 '15 at 14:10
  • 2
    @dsljanus And so where are you running npm from ? Post your whole dockerfile ... Btw don't amend multiple updates like this in questions, that's really annoying to jump from one problem to a completely different one. The purpose of SF is to post clear questions in order to get clear answers. – Xavier Lucas Feb 09 '15 at 14:26
  • In my Dockerfile I have these statements, in this exact order: COPY source /srv/visitor - RUN cd /srv/visitor - RUN npm install. So, npm is run inside the folder where the package.json file is, along with visitor.js – dlyk1988 Feb 09 '15 at 14:29
  • 1
    @dsljanus Ok so that's the issue, don't use `RUN cd` but use `WORKDIR` so the current directory is remembered between each step. A dockerfile is no more than a wrapper for docker run + docker commit so each step is run independently over the previous layer. This means pwd equals `/` in each step if you don't use this directive. – Xavier Lucas Feb 09 '15 at 14:31
  • Made the change. Now I am getting another error? Shall I make an update? – dlyk1988 Feb 09 '15 at 14:44
  • @dsljanus Post it in another question. – Xavier Lucas Feb 09 '15 at 14:46
  • btw - check .dockerignore file. You can "ignore" whole frontend folder (to prevent copying stuff like node_modules. But this prevents ./frontend/package.json also. !frontend/package.json in ignore files helps – NilColor Oct 18 '15 at 11:52
  • 2
    Special thanks for docker build - < Dockerfile does not have any context. – Winster Mar 06 '20 at 15:16
9

For me the issue was that I was using docker build - < Dockerfile

From the documentation Note: If you build using STDIN (docker build - < somefile), there is no build context, so COPY can’t be used.

Adam
  • 91
  • 1
  • 1
3

I was running into this issue and found out that I was able to add a context to the build variable in order to load my Dockerfile(s) from other directories. This allowed me to change my default Docker file structure a little more to my liking. Here is a snippet from my docker-compose.yml:

version: '3'
services:
  webserver:
    build:
      context: .
      dockerfile: ./server/Dockerfile
    ...

By adding the context I was able to define where the files should be referenced. You can reference the Docker docs here: https://docs.docker.com/compose/compose-file/#context

Hope this helps!

Ron
  • 131
  • 1
2

As Xavier Lucas [extremely helpful] answer has stated, you cannot use COPY or ADD from a directory outside of your build context (the folder you run "docker build" from, should be the same directory as your .Dockerfile). Even if you try to use a symlink, it will not work.

Note: This is specific to POSIX (Linux, Unix, Mac, Possibly Linux Subsystem for Windows). You may be able to do similar in Windows using JUNCTION.

cd ~/your_docker_project/
cp -al /subfolder/src_directory ./
echo "COPY src_directory /subfolder/" >> Dockerfile

Danger: Using this will make your docker project specific to the host. You almost never want to do this! Handle with care.

Application: Learning, Experimenting in a Development Environment

This did the trick for me. cp -al copies the directory structure and makes hard links for all the files. When you are done run "rm -rf ./src_directory" to remove it.

TamusJRoyce
  • 131
  • 4
  • My purpose: Copy cached packages on my local filesystem to my docker image. Install the tools I need (it will either use the cache or download it new). Then I delete that cache in the image and delete the hard-links on the host. If the host does not have those files, no worries. But I have limited bandwidth and limited drive space. Is this an acceptable use? – TamusJRoyce Mar 20 '18 at 18:11
  • Docker Bind Mount is the preferred way of doing things like this now. #DockerConf_2022 – TamusJRoyce May 09 '22 at 15:52
1

For following error,

COPY failed: stat

I got it around by restarting docker service.

Vineeth
  • 111
  • 3
0

For anyone who is interested, .gitignore can also cause the problem. In my case, yarn.lock was ignored. And it threw similar error.

dubu
  • 1
0

For me the problem was that the filename I was adding had a trailing space. A rename fixed it.

Hawkeye
  • 2,669
  • 9
  • 30
  • 34
0

I finally solved this issue in my case was Dockerfile that executes copy was in a deeper level of the project. So I realized that the host's build path is expressed relative to the Dockerfile's file location.

0

This happened to me when trying to run the docker file from a different directory.

I had the COPY failed: stat /var/lib/docker/tmp/docker-builder929708051/XXXX: no such file or directory and managed to resolve this by specifying the docker file.

Running docker build . -f docker/development/Dockerfile worked.

But running Runningdocker build docker/development/Dockerfile` caused this issue.

-f or --file to specify the name and location of the Dockerfile.

It found it strange at first because when i had the Dockerfile in the apps root directory it worked fine. This will help if you want to manage your environment docker files a little better.

A Star
  • 101
0

Not only must the file be in a directory in the current build context, but the file cannot be a soft link to a file outside the build context either.

I had a link to a file in my home directory, and the link was in the project directory. After I deleted the link and moved the linked file into the project (rm mylink ; mv ~/myrealfile ./), then it worked.

Loduwijk
  • 515
  • 3
  • 5
  • 14
-1

For me it was an issue with the Google Cloud SDK:

https://code.google.com/p/google-cloud-sdk/issues/detail?id=1431

Korneel
  • 99
  • 1