When building docker images with a Dockerfile in the same directory, the following works every time
$ docker build -t project/app:latest .
Sending build context to Docker daemon 135.9MB
...
However, when using -f to specify a different Dockerfile to use, docker complains ...
$ docker build -t project/app:latest -f ../some/path/Dockerfile.other
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
I can easily provide the PATH, and the build will work, but why is the PATH still required if I'm specifying the absolute path to the Dockerfile with -f?
The PATH is for specifying the build context (the tree from which COPY instructions copy things), which need not have any relation to the location of the Dockerfile.
Quoting the docs:
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. The build process can refer to any of the files in the context. For example, your build can use a COPY instruction to reference a file in the context.
From my tutorial, it creates clone Dockerfile (Dockerfile2) and build the second Docker.
docker build . -f Dockerfile2 -t
But I don't understand what . does.
According to Docker documentation.:
-t : tag ...
-f : file...
What is this command doing? - Thanks
Obviously the tag is missing. Anyway you're telling Docker:
Hey Docker, the current directory is your build context so copy everything from this location (except the files and directories mentioned in the .dockerignore file) and build the image for me using the instructions from the Dockerfile2 file. Also, please tag it using the provided tag so I can reference it easily.
If you ommit the file (drop the -f argument), then the default Dockerfile file is assumed.
. means the current directory where you are. And the Dockerfile is in it. If you do not in the directory of the Dockerfile, you will get the error.
The full command : docker build path -f Dockfile -t containerName. Also the document docker build [OPTIONS] PATH | URL | -.
From the Docker build documentation:
Usage:
docker build [OPTIONS] PATH | URL | -
build: Build an image from a Dockerfile
.: Specifies that the PATH is ., and so all the files in the local directory get tar d and sent to the Docker daemon. The PATH specifies where to find the files for the “context” of the build on the Docker daemon.
--file , -f: Name of the Dockerfile (Default is ‘PATH/Dockerfile’).
--tag , -t: Name and optionally a tag in the ‘name:tag’ format. You can apply multiple tags to an image.
So, what is happening is: Docker I want to build an image from a Dockerfile called Dockerfile2 tagged with the value (you didn't set the value of the tag) in the current path.
I have a .dockerignore file, but for one use case, I want to specify the contents of .dockerignore at the command line, something like:
docker build --ignore="node_modules" -t foo .
is there a way to do this from the command line? I am not seeing this in the docs: https://docs.docker.com/engine/reference/commandline/build/#build-with--
No, docker build does not offer an alternative to the .dockerignore file.
That is why I usually keep a symbolic link .dockerignore pointing to the actual .dockerignore_official, except for certain case, where I switch the symlink to .dockerignore_module.
This is a workaround, but that allows me to version the different version of dockerignore I might need, and choose between the two.
Update April 2019: as mentioned by Alba Mendez in the comments, PR 901 should help:
dockerfile: add dockerignore override support
Frontend will first check for <path/to/Dockerfile>.dockerignore and, if it is found, it will be used instead of the root .dockerignore.
See moby/buildkit commit b9db1d2.
It is in Docker v19.03.0 beta1, and Alba has posted an example here:
You need to enable Buildkit mode to use i
$ export DOCKER_BUILDKIT=1
$ echo "FROM ubuntu \n COPY . tmp/" > Dockerfile
$ cp Dockerfile Dockerfile2
$ touch foo bar
$ echo "foo" > .dockerignore
$ echo "bar" > Dockerfile2.dockerignore
$ docker build -t container1 -f Dockerfile .
$ docker build -t container2 -f Dockerfile2 .
$ docker run container1 ls tmp
Dockerfile
Dockerfile2
Dockerfile2.dockerignore
bar
$ docker run container2 ls tmp
Dockerfile
Dockerfile2
Dockerfile2.dockerignore
foo
Update August 2019: this is now in Docker 19.03, with the following comment from Tõnis Tiigi:
#12886 (comment) allows setting a dockerignore file per Dockerfile if the repository contains many. (Note that this was the exact description for the initial issue)
BuildKit automatically ignores files that are not used, automatically removing the problem where different sets of files needed to ignore each other.
The cases where same Dockerfile uses different sets of files for different "modes" of build (eg. dev vs prod) can be achieved with multi-stage builds and defining the mode changes with build arguments.
I can't seem to get docker build to run correctly:
wangyaos-MBP-3:~ wangyao$ cd /Users/wangyao/Ozintel/docker/flexcloud/
wangyaos-MBP-3:flexcloud wangyao$ ls
Dockerfile apache-tomcat-7.0.62 jdk1.8.0_45.jdk
wangyaos--3:flexcloud wangyao$ docker build -t="Users/wangyao/Ozintel/docker/flexcloud" .
Invalid namespace name (Users). Only [a-z0-9-_] are allowed.
wangyaos-MBP-3:flexcloud wangyao$ cd /Users/wangyao/
wangyaos-MBP-3:~ wangyao$ docker build -t="Users/wangyao/Ozintel/docker/flexcloud" .
Cannot locate Dockerfile: Dockerfile
wangyaos-MBP-3:~ wangyao$ docker build -t="Users/wangyao/Ozintel/docker/flexcloud"
docker: "build" requires 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build a new image from the source code at PATH
How should I use docker build?
Slow down and take a look at the docs.
To use docker build, the easiest way is to cd into the directory with the Dockerfile then run something like:
$ docker build -t flexcloud .
The -t argument specifies the repository name and tag, not the directory with the Dockerfile. If you want to give a path to a different Dockerfile, you can use the -f argument. The . at the end specifies the "build context", in this case the current working directory.
Trying to follow the instructions for building a docker image from the docker website.
https://docs.docker.com/examples/running_redis_service/
this is the error I get will following the instructions on the doc and using this Dockerfile
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y redis-server
EXPOSE 6379
ENTRYPOINT ["/usr/bin/redis-server"]
sudo docker build -t myrepo/redis
docker: "build" requires 1 argument. See 'docker build --help'.
How do resolve?
You need to add a dot, which means to use the Dockerfile in the local directory.
For example:
docker build -t mytag .
It means you use the Dockerfile in the local directory, and if you use docker 1.5 you can specify a Dockerfile elsewhere. Extract from the help output from docker build:
-f, --file="" Name of the Dockerfile(Default is 'Dockerfile' at context root)
In my case this error was happening in a Gitlab CI pipeline when I was passing multiple Gitlab env variables to docker build with --build-arg flags.
Turns out that one of the variables had a space in it which was causing the error. It was difficult to find since the pipeline logs just showed the $VARIABLE_NAME.
Make sure to quote the environment variables so that spaces get handled correctly.
Change from:
--build-arg VARIABLE_NAME=$VARIABLE_NAME
to:
--build-arg VARIABLE_NAME="$VARIABLE_NAME"
Did you copy the build command from somewhere else (webpage or some other file)? Try typing it in from scratch.
I copied a build command from an AWS tutorial and pasted it into my terminal and was getting this error. It was driving me crazy. After typing it in by hand, it worked! Looking closer and my previous failed commands, I noticed the "dash" character was different, it was a thinner, longer dash character than I got if I typed it myself using the "minus/dash" key.
Bad:
sudo docker build –t foo .
Good:
sudo docker build -t foo .
Can you see the difference?.. Cut and paste is hard.
In case anyone is running into this problem when trying to tag -t the image and also build it from a file that is NOT named Dockerfile (i.e. not using simply the . path), you can do it like this:
docker build -t my_image -f my_dockerfile .
Notice that docker expects a directory as the parameter and the filename as an option.
Use the following command
docker build -t mytag .
Note that mytag and dot has a space between them . This dot represents the present working directory .
Just provide dot (.) at the end of command including one space.
example:
command: docker build -t "blink:v1" .
Here you can see "blink:v1" then a space then dot(.)
Thats it.
You Need a DOT at the end...
So for example:
$ docker build -t <your username>/node-web-app .
It's a bit hidden, but if you pay attention to the . at the end...
From the command run:
sudo docker build -t myrepo/redis
there are no "arguments" passed to the docker build command, only a single flag -t and a value for that flag. After docker parses all of the flags for the command, there should be one argument left when you're running a build.
That argument is the build context. The standard command includes a trailing dot for the context:
sudo docker build -t myrepo/redis .
What's the build context?
Every docker build sends a directory to the build server. Docker is a client/server application, and the build runs on the server which isn't necessarily where the docker command is run. Docker uses the build context as the source for files used in COPY and ADD steps. When you are in the current directory to run the build, you would pass a . for the context, aka the current directory. You could pass a completely different directory, even a git repo, and docker will perform the build using that as the context, e.g.:
docker build -t sudobmitch/base:alpine --target alpine-base \
'https://github.com/sudo-bmitch/docker-base.git#main'
For more details on these options to the build command, see the docker build documentation.
What if you included an argument?
If you are including the value for the build context (typically the .) and still see this error message, you have likely passed more than one argument. Typically this is from failing to parse a flag, or passing a string with spaces without quotes. Possible causes for docker to see more than one argument include:
Missing quotes around a path or argument with spaces (take note using variables that may have spaces in them)
Incorrect dashes in the command: make sure to type these manually rather than copy and pasting
Incorrect quotes: smart quotes don't work on the command line, type them manually rather than copy and pasting.
Whitespace that isn't white space, or that doesn't appear to be a space.
Most all of these come from either a typo or copy and pasting from a source that modified the text to look pretty, breaking it for using as a command.
How do you figure out where the CLI error is?
The easiest way I have to debug this, run the command without any other flags:
docker build .
Once that works, add flags back in until you get the error, and then you'll know what flag is broken and needs the quotes to be fixed/added or dashes corrected, etc.
On older versions of Docker it seems you need to use this order:
docker build -t tag .
and not
docker build . -t tag
You can build docker image from a file called docker file and named Dockerfile by default. It has set of command/instruction that you need in your docker container.
Below command creates image with tag latest, Dockerfile should present on that location (. means present direcotry)
docker build . -t <image_name>:latest
You can specify the Dockerfile via -f if the file name in not default (Dockerfile)
Sameple Docker file contents.
FROM busybox
RUN echo "hello world"
Open PowerShelland and follow these istruction.
This type of error is tipically in Windows S.O.
When you use command build need an option and a path.
There is this type of error becouse you have not specified a path whit your Dockerfile.
Try this:
C:\Users\Daniele\app> docker build -t friendlyhello C:\Users\Daniele\app\
friendlyhello is the name who you assign to your conteiner
C:\Users\Daniele\app\ is the path who conteins your Dockerfile
if you want to add a tag
C:\Users\Daniele\app> docker build -t friendlyhello:3.0 C:\Users\Daniele\app\
The following command worked for me. Docker file was placed in my-app-master folder.
docker build -f my-app-master/Dockerfile -t my-app-master .
My problem was the Dockerfile.txt needed to be converted to a Unix executable file. Once I did that that error went away.
You may need to remove the .txt portion before doing this, but on a mac go to terminal and cd into the directory where your Dockerfile is and the type
chmod +x "Dockerfile"
And then it will convert your file to a Unix executable file which can then be executed by the Docker build command.
#Using a file other than Dockerfile instead.
#Supose my file is `Dockerfile-dev`
docker build -t mytag - < Dockerfile-dev
In my case I was using a dash (slightly longer hyphen) symbol – before the t option was the problem.
docker build –t simple-node .
Replace with a hyphen/ minus symbol.
docker build -t simple-node .
I got this error when using Docker with Jenkins pipeline within a pipeline script. The solution was to use this syntax in the pipeline script:
docker.build("[my_docker_image_tag]", "-f ./path/to/my/Dockerfile.jvm .")
Docker Build Command Format
In your powershell :
There is this type of error because you have not specified a path whith your Dockerfile.
Try this:
$ docker build -t friendlyhello:latest -f C:\\TestDockerApp\\Dockerfile.txt
friendlyhello is the name you assign to your container and add the version , just use the :latest
-f C:\TestDockerApp\Dockerfile.txt
- you want to add a tag because the build command needs a parameter or tag
- The DockerFile is a text document so explicitly add the extension .txt
**Try this format :
$ docker build -t friendlyhello:latest -f C:\\TestDockerApp\\Dockerfile.txt .**