Docker is an open-source container platform. It allows us to package applications into containers and standardize the executable components that combine application source code with the operating system. It is a software development toolkit for building, sharing, and running individual containers.
Dockerfile is a text file that contains a list of commands which can be used to build an image. It is the simplest way to automate image creation.
One of the benefits of Dockerfile is that we just need to write commands equivalent to Linux shell commands, so we don't need to learn any new syntax for it.
We may face different issues while building an image using the Dockerfile. Here in this tutorial, we will learn to solve a very common Docker build issue.
Let's first understand the error “Docker build Requires 1 Argument”.
2. Understanding the Problem
In this section, first, we‘ll create a sample Dockerfile to reproduce the error and then use different approaches to resolve it. Let‘s create a new Dockerfile named “Dockerfile“ with the following content:
FROM centos:7 RUN yum -y install wget \ && yum -y install unzip \ && yum install -y nc \ && yum -y install httpd && \ && yum clean all EXPOSE 6379 ENTRYPOINT ["ping"] CMD ["google.com"]
Here, we have written a sample Dockerfile that uses a “centos:7” as a base image. Hence, it supports all the commands of centos.
We have also installed some other utility commands of the Docker into the Dockerfile.
The command to reproduce the issue “Docker build Requires 1 Argument” is:
$ docker build "docker build" requires exactly 1 argument. See 'docker build --help'. Usage: docker build [OPTIONS] PATH | URL | - Build an image from a Dockerfile
This issue will also occur if we use the Docker build command with different options.
Let's explore one of the options:
$ docker build -t test_image/centos "docker build" requires exactly 1 argument. See 'docker build --help'. Usage: docker build [OPTIONS] PATH | URL | - Build an image from a Dockerfile
Here, in both cases, we faced the issue of “Docker build Requires 1 Argument”.
3. Different Ways to Build a Docker Image
Before, we move forward to solve the error “Docker build requires 1 Argument”, let's first understand the Docker build command using different options.
The Docker build is run by the Docker daemon. Firstly Docker build sends the entire context to the daemon. The ideal situation is to start with an empty directory and add only the files needed to build the Docker image using the Dockerfile:
$ docker build . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM centos:7 14.04: Pulling from library/centos 2e6e20c8e2e6: Extracting [============> ] 17.83MB/70.69MB 0551a797c01d: Download complete 512123a864da: Download complete
We can also use -f flag to build an image by pointing a Dockerfile from a particular location:
$ docker build -f /root/dockerImage/Dockerfile .
In order to tag an image, we can use -t option with the Docker build command:
$ docker build -t test_image/centos .
We can also pass build arguments with Docker build command:
$ docker build -t test_image/centos --build-arg JAVA_ENV=1.8 .
To clean up the build of an image, we can use –no-cache option in the command:
$ docker build -t test_image/centos --build-arg JAVA_ENV=1.8 --no-cache .
In the older version of the Docker, we needed to pass –no-cache=true, but it is not in the case of newer versions. We can also create a Dockerfile without having the file name as Dockerfile:
$ docker build -f /root/dockerImage/DockerFile_JAVA .
Here we created a Docker image using DockerFile_JAVA file name.
4. Due to Insufficient Arguments
The most common reason for “Docker build Requires 1 Argument” error is when we try to build the image without providing sufficient arguments. Here, in the arguments, we need to provide the directory with the command.
By default we provided dot(.) in the command which specifies the Docker daemon to use the shell’s current working directory as the build context:
$ docker build .
The dot(.) basically tells the Docker that Dockerfile has to be used from the current directory. We can also change the Docker build context using the following command:
$ docker build /root/test
Another issue related to Docker build that we may face:
$ docker build -f /root/test/Dockerfile2 . unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /root/test/Dockerfile2: no such file or directory
Here in the above command, we are trying to build a Docker image using the file name “Dockerfile2”, if this file is not present in the current directory, we will get the following error:
unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /root/test/Dockerfile2: no such file or directory
In order to solve this issue, we need to provide the correct file name with -f option.
In this tutorial, we learned about the issues related to the Docker build command.
Initially, we explored different ways of building a Docker image, then we addressed the issue with the Docker build arguments, and finally, we learned a few more issues related to the Docker build command.