1

I have a Dockerfile located at path: /Users/userx/Library/Documents/dockerProject

I want to copy a file into the container from: /Users/userx/Library/temp/otherfiles/myFile.txt

I would like to do this from with in the Dockerfile. I tried using COPY but it only works in the context of the project.

some people implied I can do it with the RUN command chaining bash commands but when I use:

RUN 'cd /Users/userx/Library/temp/otherfiles/;pwd'

I get the error returned a non-zero code

How can I copy a file using a Dockerfile from a system absolute path.

thanks

bellackn
  • 1,939
  • 13
  • 22
abe
  • 4,046
  • 6
  • 29
  • 33

1 Answers1

5

It is correct that you can only use files in your Dockerfile that are within the build context. Usually, this is ., so the Dockerfile's directory. You specify it within your docker build command. Here you can get a good overview.

I see two options here:

  1. You copy the file you need inside the Dockerfile's directory just before you build your image. Then you can use COPY file /path/in/container inside your Dockerfile.
  2. You mount the file you need inside the container on runtime by using a volume: docker run -v /path/to/file:/path/in/container yourimage.

Also, this: RUN 'cd /Users/userx/Library/temp/otherfiles/;pwd' does not make sense. The command will be run inside the image on build time, this path does not exist there. You cannot reference paths from the host machine inside the Dockerfile in RUN commands.

bellackn
  • 1,939
  • 13
  • 22
  • Are you saying get RUN command can't be used to navigate to the location of the file and copy it? – abe Jan 10 '20 at 09:25
  • Yes, I just enhanced my answer. – bellackn Jan 10 '20 at 09:27
  • @abe You have misunderstood something. When building an image, the only thing that is available, is the build context you have provided to docker daemon via `docker build` command. – leopal Jan 10 '20 at 09:27
  • 2
    @leopal it feels very limiting. I Just want to add one file that is not in the context. It really feels like i'm missing something. I can't not be the only person who does not have all there files in one location. – abe Jan 10 '20 at 09:34