27

I have a simple Dockerfile:

FROM php:7.1-apache
LABEL maintainer="rburton@agsource.com"
COPY C:/Users/rburton/code/MyAgsourceAPI /var/www

It is the last line that is giving me problems. I am copying from a Windows structure to a docker container (Linux I assume). When I build this image I get:

...
Step 3/3 : COPY C:/Users/rburton/code/MyAgsourceAPI /var/www
COPY failed: stat /var/lib/docker/tmp/dockerbuilder720374851/C:/Users/rburton/code/MyAgsourceAPI: no such file or directory

First, something is preventing the recognition that this is an absolute path and naturally if the path is pre-pended with /var/lib/docker/tmp/dockerbuilder720374851 then the file will not be found. Second, I have tried / and \ but all with the same result. Also the drive letter I suspect is confusing to docker. So the question is how do I copy files and folders (along with the contents) from a Windows folder to a docker container?

Vladyslav
  • 2,018
  • 4
  • 18
  • 44
Kevin Burton
  • 425
  • 2
  • 6
  • 11
  • 4
    The path you 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 [docker-forum](https://forums.docker.com/t/copy-files-from-windows-host-to-ubuntu-container/28757) – Deja Sep 19 '17 at 20:47
  • The error that I am getting is: Step 3/3 : COPY MyAgsourceAPI/ /var/www COPY failed: stat /var/lib/docker/tmp/docker-builder894919783/MyAgsourceAPI: no such file or directory – Kevin Burton Sep 19 '17 at 21:06

3 Answers3

11

First, change your Dockerfile to:

FROM php:7.1-apache
LABEL maintainer="rburton@agsource.com"
COPY MyAgsourceAPI /var/www

Then, to go your code directory: cd Users/rburton/code.

Within that directory, run: docker build -t <image_name> .

ItayB
  • 10,377
  • 9
  • 50
  • 77
  • 1
    Thank you. I originally moved the Dockerfile to C:\Temp because I was running up against a "could not stat" error that was supposedly solved by moving to folder that didn't have sub-folders. But now this seems to work. – Kevin Burton Sep 20 '17 at 15:27
  • 1
    Theres a joke, where a person only knows enough english, to tell someone to 'come here' - so if he wants someone to go somewhere, he goes to the place and then says, 'come here' -- jokes aside, how do you write absolute paths? – DMin Apr 12 '21 at 04:45
  • 1
    ok, the file referenced in the dockerfile has to be 1) Relative to the docker file 2) Has to be inside or lower than the docker-file folder - cannot be higher than that. – DMin Apr 12 '21 at 05:06
0

Another tip that might be helpful, I've seen same issue while running build from correct context, and issue remained until I've used all small caps on src folder that I wanted to copy from. eg:

COPY MyAgsourceAPI /var/www -> COPY myagsourceapi /var/www
0

The root of the path is relative to the Dockerfile and not your Windows filesystem.

If for example your filesystem is layed out like this:

+-+-MyProject
  |
  +---Dockerfile
  |
  +-+-build
    |
    +---MyAgsourceAPI

You can use:

COPY /build/MyAgsourceAPI /var/www

Note that "MyProject" (or anything above it) is excluded from the path.

Jasper Citi
  • 1,673
  • 1
  • 23
  • 31