Dockerfile build yielding lstat error on ADD

2019-07-25 13:09发布

问题:

I have a dead-simple project with the following structure:

docker/
|- src/
|  |- config.json
|
|- Dockerfile

The contents of the json file:

{
  "setting": "value"
}

The contents of the Dockerfile:

FROM python:3.5

ADD src/config.json /testapp/config.json  # Config

I run this build command:

docker build -t testapp:dev .

And get this result:

Sending build context to Docker daemon 4.608 kB
Step 1 : FROM python:3.5
 ---> 1d0326469b55
Step 2 : ADD src/config.json /testapp/config.json 
lstat testapp/config.json: no such file or directory

Why is this failing? Why is it that the not-found item is testapp/config.json? In the past I've used this exact construct to, for instance, copy requirements.txt files into temporary working directories. I don't understand what I'm missing about how ADD works that is causing this to fail. Can anyone explain?

回答1:

I fell victim to my IDE. This line will fail:

ADD src/config.json /testapp/config.json  # Config

But this will succeed:

ADD src/config.json /testapp/config.json

Reading the documentation for add, it appears it's trying to use Config as the destination directory, and as such /testapp/config.json is treated as a source file. The IDE greyed out the # Config, making me think it was being treated as a comment: Dockerfiles do not do inline comments.

This is covered in Docker documentation on comments:

Docker will treat lines that begin with # as a comment. A # marker anywhere else in the line will be treated as an argument.



标签: docker