161

I have a Dockerfile that is supposed to build an Ubuntu image. But whenever I run

docker build -t ubuntu-test:latest ./Dockerfile

it shows the following error on the console

unable to prepare context: context must be a directory: /Users/tempUser/git/docker/Dockerfile

I'm on Mac OsX. I tried to sudo as well. Nothing works.

Jeroen Steenbeeke
  • 3,884
  • 5
  • 17
  • 26
Damien-Amen
  • 7,232
  • 12
  • 46
  • 75
  • I find it interesting that none of the answers explain *why* it doesn't work, especially when the [`docker build` documentation](https://docs.docker.com/engine/reference/commandline/build/#text-files) specifically says it will: "Instead of specifying a context, you can pass a single Dockerfile in the URL" and "If you use `STDIN` or specify a `URL` pointing to a plain text file, the system places the contents into a file called `Dockerfile`, and any `-f`, `--file` option is ignored. In this scenario, there is no context." – Michael Burr May 17 '22 at 15:15

7 Answers7

241

You need to point to the directory instead. You must not specify the dockerfile.

docker build -t ubuntu-test:latest . does work.

docker build -t ubuntu-test:latest ./Dockerfile does not work.

gorgabal
  • 145
  • 10
Damien-Amen
  • 7,232
  • 12
  • 46
  • 75
  • 3
    why is it like this? it seems strange to need to point to a dir when you could easily have it point to the file itself – Monica Heddneck Sep 13 '20 at 20:29
  • 3
    Per a below answer from @JakubKnejzlik, you can point to a file using the `-f` flag (then still refer to a directory, usually using `.`, so docker still has the context it wants) – Vincent Buscarello Apr 12 '21 at 18:45
  • 1
    Still get error: `unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/kes/work/projects/sidnet/chimera/Chimera-DevEnv/Chimera/Dockerfile: no such file or directory` – Eugen Konkov Dec 02 '21 at 10:48
124

You can also run docker build with -f option

docker build -t ubuntu-test:latest -f Dockerfile.custom .

JakubKnejzlik
  • 6,363
  • 3
  • 40
  • 41
  • 6
    This worked for me. Dockerfile name in my case was **Dockerfile-abcd** so I had to fire: `docker build -t my-custom-name -f /path_to_my_docker_file/Dockerfile-abcd .` – niranjan_harpale Mar 25 '20 at 13:01
27

Understand contexts

The docker build command

The basic syntax of docker's build command is

docker build -t imagename:imagetag context_dir

The context

The context is a directory and determines what the docker build process is going to see: From the Dockerfile's point of view, any file context_dir/mydir/myfile in your filesystem will become /mydir/myfile in the Dockerfile and hence during the build process.

The dockerfile

If the dockerfile is called Dockerfile and lives in the context, it will be found implicitly by naming convention. That's nice, because it means you can usually find the Dockerfile in any docker container immediately.

If you insist on using different name, say "/tmp/mydockerfile", you can use -f like this:

docker build -t imagename:imagetag -f /tmp/mydockerfile context_dir

but then the dockerfile will not be in the same folder or at least will be harder to find.

rooni
  • 1,036
  • 3
  • 17
  • 33
Lutz Prechelt
  • 36,608
  • 11
  • 63
  • 88
  • What if my Dockerfile doesn't neet any context? In my use case, the folder I want to store the Dockerfile in contains other large files that just slow down the Docker build process. – WolfLink Aug 16 '23 at 20:39
  • @WolfLink In that case you should put your Dockerfile into an empty directory. You may not like it much, but that is the Docker folks' design decision. – Lutz Prechelt Aug 24 '23 at 16:35
18

To specify a Dockerfile when build, you can use:

docker build -t ubuntu-test:latest - < /path/to/your/Dockerfile

But it'll fail if there's ADD or COPY command that depends on relative path. There're many ways to specify a context for docker build, you can refer to docs of docker build for more info.

shizhz
  • 11,715
  • 3
  • 39
  • 49
  • Did this for *ubuntu 12.04* : ```docker build -t ubuntu:12.04 - < Dockerfile``` You must be in the root of the *Dockerfile* – Dominic Motuka Oct 23 '17 at 22:04
6

I face the same issue. I am using docker version:17.09.0-ce.

I follow below steps:

  1. Create Dockerfile and added commands for creating docker image
  2. Go to directory where we have created Dockfile
  3. execute below command $ sudo docker build -t ubuntu-test:latest .

It resolved issue and image created successsfully.

Note: build command depend on docker version as well as which build option we are using. :)

BMitch
  • 231,797
  • 42
  • 475
  • 450
Rajeev Rathor
  • 1,830
  • 25
  • 20
2

It's simple, whenever Docker build is run, docker wants to know, what's the image name, so we need to pass -t : . Now make sure you are in the same directory where you have your Dockerfile and run

docker build -t <image_name>:<version> . Example docker build -t my_apache:latest . assuming you are in the same directory as your Dockerfile otherwise pass -f flag and the Dockerfile.

docker build -t my_apache:latest -f ~/Users/documents/myapache/Dockerfile

Prashanth
  • 91
  • 6
  • In order to get that second command to run for me I had to change it slightly: `docker build -t my_apache:latest -f ~/Users/documents/myapache/Dockerfile` (note the addition of a `.` at the end) – Josh Oct 17 '18 at 20:07
2

One of the reasons for me getting an error was the file name make sure the file name is Dockerfile So i figured it out, hope it might help someone.