Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build with -f option cannot find Dockerfile

Tags:

docker

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.

like image 448
AnAmuser Avatar asked Aug 26 '15 20:08

AnAmuser


People also ask

Which command is used to build Dockerfile?

$ 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.

Do Dockerfiles have to be called Dockerfile?

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.

Where should I put Dockerfiles?

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.


2 Answers

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.

like image 89
Ahmad Abdelghany Avatar answered Nov 06 '22 03:11

Ahmad Abdelghany


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

like image 26
Rusty1 Avatar answered Nov 06 '22 03:11

Rusty1