I have tried a lot of combinations with the docker -f option but I have never got it to work
I can do this:
docker build -t foo/bar .
But I cannot do this:
docker build -t foo/bar -f Dockerfile .
or this:
docker build -t foo/bar -f ./Dockerfile .
This gives me the following error:
unable to prepare context: The Dockerfile (c:\path\Dockerfile) must be within the build context (.)
I am using docker through the default vm on Windows 7. Docker version is 1.8.1, build d12ea79
I cannot see the difference. It could be very nice to have different Dockerfiles for different tasks on a project, but without the -f option that is really not possible.
$ docker build -f Dockerfile.debug . This will use a file called Dockerfile.debug for the build instructions instead of Dockerfile . $ curl example.com/remote/Dockerfile | docker build -f - . The above command will use the current directory as the build context and read a Dockerfile from stdin.
The default filename to use for a Dockerfile is Dockerfile , without a file extension. Using the default name allows you to run the docker build command without having to specify additional command flags. Some projects may need distinct Dockerfiles for specific purposes.
The best way is to put the Dockerfile inside the empty directory and then add only the application and configuration files required for building the docker image. To increase the build's performance, you can exclude files and directories by adding a . dockerignore file to that directory as well.
Well, as the error message is suggesting, your Dockerfile
is not within the context
, which is the current directory (.
) in your case.
The docker file, specified with -f
, must always be within the context directory specified as an argument.
So, normally this should work fine:
docker build -f /path/to/context/dir/Dockerfile /path/to/context/dir
And this too:
cd /some/dir
docker build -f /some/dir/customDir/Custom-Dockerfile-name .
While, this would fail:
docker build -f /path/to/diff/dir/Dockerfile /path/to/context/dir
From the Docs:
docker build [OPTIONS] PATH | URL | -
The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or URL
And:
By default the docker build command will look for a Dockerfile at the root of the build context. The -f, --file, option lets you specify the path to an alternative file to use instead. This is useful in cases where the same set of files are used for multiple builds. The path must be to a file within the build context. If a relative path is specified then it is interpreted as relative to the root of the context.
Here is an example of how I used the docker build to avoid this error
docker build -f my.dockerfile ./
Note the trailing forward slash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With