Adam
Adam

Reputation: 6132

Copy folder from Windows host OS into Docker image using COPY in Docker file

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.

Upvotes: 2

Views: 3064

Answers (3)

Noam Yizraeli
Noam Yizraeli

Reputation: 5404

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

Upvotes: 1

usuario
usuario

Reputation: 2467

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

Upvotes: 0

clarj
clarj

Reputation: 1043

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.

Upvotes: 0

Related Questions