Copy files outside of build context

I have a multi-stage Dockerfile that begins like:

FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app

RUN dotnet restore

The problem is that my project has a local NuGet repository. The obvious solution would be to do something like
COPY <source of NuGet repository> /root/.nuget/packages
But this gives me an error that COPY failed: Forbidden path outside the build context.
What is the solution to make a local Nuget repository available to a multi-stage build like this?

The COPY command require that the source is a located within the current directory, or some sub-directory. Would it be possible to copy the NuGet repo to a subfolder of say /path/to/my/docker/folder, before running the build step? Example: Copy the NuGet repo to /home/myuser/docker/nuget-repo, and place your Dockerfile in /home/myuser/docker?

Disclaimer: I don’t know your application and setup, and I’m not a docker expert.

Thank you. If this seems to be the only solution I can accept that. But it involves copying a potentially large NuGet repository twice (once before the build and once during the build). I would be interested in hearing other solutions.

Is it necessary to add the repo the container? When you destroy the container, you’ll loose the data as well. Would it be possible to mount it as a volume instead?

Did you look at this? https://stackoverflow.com/a/48681644
I’m not sure if it will do the trick, but it might be helpful.

Thank you. I will try it and see.

I agree with the idea of mounting it as a volume. Two problems with the copy. One, you’re copying stuff you might never use during the actual build. Two, any changes to the packages within the repo would require rebuilding the image.

I have solved a similar problem, where I want to copy data from a git repo, that might be quite large to a container image through the usage of a bind mount.

At the end it is quite easy to trick the Dockerfile.

  • go to you build path
  • mkdir -p some_name
  • sudo mount --bind src_dir ./some_name

I hope this solve your problem.

Hi Stephan,

I have similar problem, Could you please help

Thanks in Advance