skip to Main Content

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


  1. Assuming you’re host’s current folder contains:

    tree
    
    .
    ├── Dockerfile
    └── my_app
        ├── index.html
        └── scripts
            └── index.js
    

    With a .dockerignore containing Dockerfile

    Dockerfile:

    FROM docker.io/alpine:latest
    
    WORKDIR mydirectory
    
    COPY . .
    
    RUN apk add tree && 
        pwd && 
        tree
    

    Then e.g.:

    docker build 
    --tag=foo 
    --file=${PWD}/Dockerfile 
    ${PWD}
    

    NOTE I’m unsure what the Windows equivalent of ${PWD} is but it’s the reference to the current folder.

    Then:

    STEP 1/4: FROM docker.io/alpine:latest
    STEP 2/4: WORKDIR mydirectory
    --> 7606566a09d
    STEP 3/4: COPY . .
    --> cf9433292cd
    STEP 4/4: RUN apk add tree && pwd && tree
    (1/1) Installing tree (2.1.1-r0)
    Executing busybox-1.36.1-r29.trigger
    OK: 8 MiB in 15 packages
    /mydirectory
    .
    └── my_app
        ├── index.html
        └── scripts
            └── index.js
    
    3 directories, 2 files
    COMMIT stackoverflow:79190089
    --> 2186f71dd44
    Successfully tagged localhost/foo
    

    Which is what you want.

    Login or Signup to reply.
  2. 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 of COPY, but it has no effect on the source.

    That is, with the build-context layout you show, you always must write

    COPY mydirectory/mytext.txt /some/container/path/
    

    If the right-hand side of COPY is a relative path, that is affected by WORKDIR, but not the left-hand side.

    # Using absolute paths:
    COPY mydirectory/mytext.txt /app/mydirectory/
    
    # Equivalently:
    WORKDIR /app/mydirectory
    COPY mydirectory/mytext.txt ./
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search