2

I want to copy folder D:\test from my host OS (Windows) to my docker image.

My Docker file is D:\Programs>

Docker file

FROM mcr.microsoft.com/windows/servercore:ltsc2019
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir test
COPY D:/test to /root/test
    #USING 'COPY' here instead of 'ADD' because of https://stackoverflow.com/questions/24958140/what-is-the-difference-between-the-copy-and-add-commands-in-a-dockerfile

From folder D:\Programs> I run command docker build . -t test

But I get error:

COPY failed: file not found in build context or excluded by .dockerignore: stat test: file does not exist

I tried commands COPY D:/test to /root/test and COPY ./test to /root/test

I know this error occurs because the path I copy from has to be relative from the build context (the . in docker build .). It cannot be an arbitrary path on the system outside of the build context.

I thought by using . I'd be in context D:\ from my build context D:\Programs>, but I guess not. What should my COPY command look like?

I checked here already:

UPDATE 1

I placed the test folder in D:\Programs where my Dockerfile also resides, so I now have structure:

D:\Programs
    \Test
    Dockerfile

I then ran the build command again where I tried COPY test to /root/test and COPY ./test to /root/test, but both fail with the same aforementioned error.

Adam
  • 6,041
  • 36
  • 120
  • 208

3 Answers3

1

I assume you've only added to in COPY D:/test to /root/test for abbreviation but I recommend checking that anyway.

If you really need what's in D:/Programs and can't move it to a folder inside the current build context there are two general options as I see it:

  1. changing the build context - I'd suggest setting the build context as D:/ and adding every folder in the D:/ directory other than Programs to the .dockerignore file, then you can reference the Dockerfile from some subdirectory somewhere in D:/ and copy your files at build time as you wanted

  2. creating a bind mount in runtime - you can map the D:/Programs directory to a folder inside the container (/my-programs) at runtime then copy the files from that folder to /root/test or just use the bind mount folder (/my-programs)

If in the future it will be available to use bind mounts in build time you'd be able to use the second option while creating the docker image

Noam Yizraeli
  • 4,446
  • 18
  • 35
  • See my update 1, I already tried moving what I need into my build context but still it does not work – Adam Sep 07 '21 at 18:34
  • as I understood it from update 1 you ran the `docker build` command from `D:\Programs\test` (with that folder as build context), was I wrong? – Noam Yizraeli Sep 07 '21 at 19:13
  • I see how my update was confusing. But yes, you were wrong, see updated update 1 – Adam Sep 07 '21 at 19:46
  • ok, but the structure was clear, what folder did you set as your build context when running `docker build`? – Noam Yizraeli Sep 07 '21 at 19:51
  • From my post: "From folder D:\Programs> I run command `docker build . -t test`", so with the `.` parameter my build context is `D:\Programs` then, right? – Adam Sep 07 '21 at 20:08
  • and what was your `COPY` command that time? I'd presume `COPY . /root/test` to copy all the program files from the `D:\Programs` dir (which is the build context this time) but making sure nonetheless – Noam Yizraeli Sep 07 '21 at 20:51
  • 1
    Wow...It was a typo! I had `to` in my `COPY` command the whole time. Instead of `COPY test to /root/test` I had to use `COPY test /root/test`. Your sample COPY command pointed me to it. Just use that as your answer and I'll award the bounty – Adam Sep 08 '21 at 08:30
0

The only way I know how to do this is to change the dockerfile location during build, keeping the copied folder in context.

As below:

  • 'test' folder located at D:/test

  • Set dockerfile COPY command as such:

    COPY test /root/test

  • Say dockerfile is at D:/Programs/dockerfile

  • Navigate to D:/ in CLI

  • docker build -f ./Programs/dockerfile .

The . keeps the "test" folder in the build context. As far as I know, you cannot go to a parent directory etc. with docker build, so instead you want to stay in the parent folder and go down to the dockerfile.

clarj
  • 1,001
  • 4
  • 14
  • Have tested again with your same folder structure from the update which definitely works on my machine. Running ```docker build .``` inside ```D:\Programs``` should copy "test" in providing: - folder "test" is inside D:\Programs - ```COPY test /root/test``` is in the dockerfile Just to check, is your folder capitalisation the same? You've referenced "Test" and "test" in a couple of places, so worth asking. Also, you've added "to" in your COPY commands above, I assume that is not in your dockerfile COPY command and just for this post? – clarj Sep 07 '21 at 21:11
0

If you run the docker build ... from D:\Programs that folder is the docker build context. All files used in the Dockerfile must be there.

d:\files is outside d:\programs, so it will be never be found.

You need to copy files content to the places where you run the command and you can use this copy line

COPY samplefile1.txt /root/test
usuario
  • 2,132
  • 1
  • 10
  • 26
  • Thanks. Please see my update 1, I copied the folder to the build context but still get the same error – Adam Aug 27 '21 at 13:19