I’m having an issue copying files into a new image that I can’t find here. I’ve looked at other posts but they don’t seem to work such as this one. I am using Linux containers on a Windows host. I have copied the files I need into a folder on the root context of the Dockerfile. I even took one file and put it on the root context. I can copy the files into a container using docker cp but I want these files in the image. I tried ADD with the same results. I don’t have a .dockerignore file. The image builds fine without the COPY commandlet.
I get this error:
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref |sha redacted|::|sha redacted|: "/mytext.txt": not found
My structure in the context is as follows (how I would like to use it)
DockerFile + -- /mydirectory + -- mytext.txt
I have tried the following commandlets:
This works
WORKDIR /mydirectory
These don’t work below the WORKDIR commandlet
COPY . .
<- pull everything in the workdir from host to image (I want this behavior)
Another way 1
COPY mytext.txt /mydirectory
Another way 2
COPY mytext.txt .
<- I copied it to the root context instead of the subdir
Another way 3
COPY mytext.txt ./
Another way 4
COPY c:/project/mydirectory/mytext.txt /mydirectory
Another way 5
COPY c:/project/mydirectory/mytext.txt .
2
Answers
Assuming you’re host’s current folder contains:
With a
.dockerignore
containingDockerfile
Dockerfile
:Then e.g.:
Then:
Which is what you want.
The left-hand side of
COPY
is always relative to the top-level build-context directory (and must be within that filesystem tree, not a parent or sibling directory).WORKDIR
changes the current directory in the image, which can include the destination ofCOPY
, but it has no effect on the source.That is, with the build-context layout you show, you always must write
If the right-hand side of
COPY
is a relative path, that is affected byWORKDIR
, but not the left-hand side.There’s no way within a Dockerfile to "narrow" the build context to some specific directory. There are some patterns with trying to build an image out of multiple source trees that require the build-context directory to be a parent directory of the actual application you’re trying to package, and you always need to use a path relative to that parent directory on the left-hand side of
COPY
.