AnAmuser
AnAmuser

Reputation: 1895

Docker build with -f option cannot find Dockerfile

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.

Upvotes: 16

Views: 29078

Answers (6)

Ahmad Abdelghany
Ahmad Abdelghany

Reputation: 13218

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.

Upvotes: 7

memozac
memozac

Reputation: 91

Coming in a little bit late here but I ran into this issue yesterday. Basically, when using the dot (.) you are setting up your current directory as the working directory - if you are using the -f flag then most likely that's not right. So you need to do the following:

docker build -f /path/to/Dockerfile /path/to/

Notice the /path/to/ replacing the .. This places the docker script in the right context. You can use any other flag (like -t) if needed.

docker build -t foo/bar -f /path/to/Dockerfile /path/to/

This is working for me like a charm.

Upvotes: 2

user2915097
user2915097

Reputation: 32176

if you do

docker build --help

you will notice, among other things

-f, --file=                     Name of the Dockerfile (Default is 'PATH/Dockerfile')

you can do either

docker build -t mytag .

or

docker build -t mytag -f another_dockerfile

but you can't use . and the -f together.

It is ., or the Dockerfile with -f, not both

Upvotes: -1

Ibrohim Ermatov
Ibrohim Ermatov

Reputation: 2259

If your Dockerfile is not in the project root (current directory), show it after the -f.

Here is an example I used:

docker build -t test_service -f deployment/dev/Dockerfile .

Upvotes: 2

Erick T
Erick T

Reputation: 7449

I hit this issue, and it turns out that it was caused by my dockerfile being on a disk mounted as a folder in NTFS. Once I moved it to a regular folder, it worked fine.

Upvotes: 1

Rusty1
Rusty1

Reputation: 381

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

Upvotes: 7

Related Questions