How to fix the Docker build requires exactly one argument?


Introduction

Docker build is a container image builder command. The command helps you to create your container image with some extra tweakable functionalities. While building the image using the “docker build” command beginners face a very common and simple problem of arguments. In this article, we have discussed various ways of encountering that error and how to resolve the error. Different ways of executing the build are also mentioned in the article.

Creation of Dockerfile

First of all, we will create a dockerfile. This dockerfile will help us to create a docker container image of the “apache-web-server”. The index page is stored as the default location /usr/local/apache2/htdocs/

Make a directory “TUTORIALSPOINT”.

$mkdir TUTORIALSPOINT

Get inside the “TUTORIALSPOINT” directory, create a file named “Dockerfile”, and open the file with any of the code editors. Here, we used Visual Studio code.

$cd TUTORIALSPOINT $touch Dockerfile $code .

Paste the below Dockerfile codel.

#use httpd as the base image FROM httpd:latest #set the working directory WORKDIR /usr/local/apache2/htdocs/ #expose the port on the image EXPOSE 80

Different Build Errors

Let us see different build errors we might encounter while creating a container image using the docker build command.

Example 1

If the command is executed inside the “TUTORIALSPOINT” directory where Dockerfile is present.

$pwd

Output

/home/hemant/TUTORIALSPOINT

Now see the build error.

$docker build

Output

"docker build" requires exactly 1 argument. See 'docker build --help'. Usage: docker build [OPTIONS] PATH | URL | - Build an image from a Dockerfile

Example 2

If the command is executed outside the “TUTORIALSPOINT” directory where no Dockerfile is present.

$pwd

Output

/home/hemant

Now see the error.

$docker build

Output

"docker build" requires exactly 1 argument. See 'docker build --help'. Usage: docker build [OPTIONS] PATH | URL | - Build an image from a Dockerfile

Example 3

If the command is executed outside the “TUTORIALSPOINT” directory where no Dockerfile is present.

$pwd

Output

/home/hemant

Now see the error.

$docker build .

Output

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/hemant/Dockerfile: no such file or directory

Example 4

If the command is executed inside the “TUTORIALSPOINT” directory but the “Dockerfile” is renamed as any other file like “Dockerfile_2”.

$pwd

Output

/home/hemant/TUTORIALSPOINT

Now see the error.

$docker build .

Output

unable to prepare context: unable to evaluate symlinks in Dockerfile path: lstat /home/hemant/Dockerfile: no such file or directory

Understand the Build Command

Till now, different build errors are seen. Now understanding the “docker build” command will be best. Below we have a breakdown of all the parts of the command.

The general syntax of the command is −

$docker build [options] [local path or url]

Important options are listed below

File

The default filename of the dockerfile is “Dockerfile” but to change it you can use the “-f” flag. This flag requires the relative path of the dockerfile with the name you have saved it.

Tag

The “-t” flag is used when we want to rename the image that is going to be created using the docker build. This can also be written as “--tag”.

The general syntax to assign tag − name − version If you don’t define the version of the image then docker itself provides a “latest” version.

PATH or Context Directory

This path is a local relative path, it is going to be used by the dockerfile as a reference when executing the commands like COPY and ADD. This path is also known as the “context directory”. Docker daemon first gets the context directory and then finds the files that are required to be copied in the image while building.

URL

If the files that you want to copy are not present on your local system, Instead of the local path just mention the “URL” to the remote directory. For example, you can use a GitHub repository URL.

Implementation Cases

There are various circumstances when building a docker image. Some of the circumstances are mentioned below.

Case 1: Simplest implementation of the “docker build”

Step 1 − Create a directory “TUTORIALSPOINT”

$mkdir TUTORIALSPOINT

Step 2 − Create a dockerfile named “Dockerfile”

$cd TUTORIALSPOINT
$touch Dockerfile

Save the below code in it.

#use httpd as the base image FROM httpd:latest #set the working directory WORKDIR /usr/local/apache2/htdocs/ #expose the port on the image EXPOSE 80

Open this file and save the below code in it.

Step 3 − Build the docker image

Simplest implementation

$docker build .

Output

Sending build context to Docker daemon 3.072kB Step 1/3 : FROM httpd:latest ---> fe8735c23ec5 Step 2/3 : WORKDIR /usr/local/apache2/htdocs/ ---> Using cache ---> 933e884d81bc Step 3/3 : EXPOSE 80 ---> Using cache ---> a127ab71efab Successfully built a127ab71efab

The image name will be none and it will get an ID. To name the image, use the below command.

$docker build –t image1:version1 .

Output

Sending build context to Docker daemon 3.072kB Step 1/3 : FROM httpd:latest ---> fe8735c23ec5 Step 2/3 : WORKDIR /usr/local/apache2/htdocs/ ---> Using cache ---> 933e884d81bc Step 3/3 : EXPOSE 80 ---> Using cache ---> a127ab71efab Successfully built a127ab71efab Successfully tagged image1:version1

To check the created image.

$docker images

Output

REPOSITORY TAG IMAGE ID CREATED SIZE image1 version1 a127ab71efab 2 days ago 145MB

Case 2: Change the name of the Dockerfile

Follow the below steps for using dockerfile instead of “Dockerfile”.

Step 1: Create a file name as “tutorialfile” with the same dockerfile code as above.

$touch tutorialfile

Step 2: Now build the image using this dockerfile

General syntax: docker build –f complete_path_of_dockerfile –t name:version .

$docker build -f /home/hemant/TUTORIALSPOINT/tutorialfile -t test2:v1 .

Output

Sending build context to Docker daemon 3.584kB Step 1/3 : FROM httpd:latest ---> fe8735c23ec5 Step 2/3 : WORKDIR /usr/local/apache2/htdocs/ ---> Using cache ---> 933e884d81bc Step 3/3 : EXPOSE 80 ---> Using cache ---> a127ab71efab Successfully built a127ab71efab Successfully tagged test2:v1

This will create a docker image from the “tutorialfile”. Check-in created image.

$docker images

Output

REPOSITORY TAG IMAGE ID CREATED SIZE test2 v1 a127ab71efab 2 days ago 145MB

Case 3: Change the Context Directory

In the above examples, we have used the context directory as the “current working directory” by using “dot”.

Step 1: Create a new dockerfile named “newdockerfile”

$touch newdockerfile

Add the below code to the dockerfile.

#use httpd as the base image FROM httpd:latest #set the working directory WORKDIR /usr/local/apache2/htdocs/ RUN rm index.html #copy all the files in TUTORIALSPOINT to the container image. COPY . . #expose the port on the image EXPOSE 80

Step 2: Get out of the TUTORIALSPOINT Directory.

First copy the path of the current working directory.

$pwd

Get out of the directory.

$cd ..

Step 3: Now execute the docker build command.

$docker build \
   -f /home/hemant/TUTORIALSPOINT/tutorialfile \
   -t test3:v1 \
/home/hemant/TUTORIALSPOINT

Output

Sending build context to Docker daemon 2.56kB Step 1/5 : FROM httpd:latest ---> fe8735c23ec5 Step 2/5 : WORKDIR /usr/local/apache2/htdocs/ ---> Using cache ---> 933e884d81bc Step 3/5 : RUN rm index.html ---> Using cache ---> 0775ee5efabc Step 4/5 : COPY . . ---> ae8dd9f280da Step 5/5 : EXPOSE 80 ---> Running in bccffebf7db2 Removing intermediate container bccffebf7db2 ---> 6261cc2ca031 Successfully built 6261cc2ca031 Successfully tagged test3:v1

Step 4: Now run the container and check if the context directory is set to “home/hemant/TUTORIALSPOINT”

$docker run -itd --name cont -p 8080:80 test3:v1

Step 5: Check if the Context directory is copied on the apache server. Visit the http://localhost:8080/

Hence, the context directory was copied and set well.

Conclusion

Here in this article, we learned the use of the “docker build” command. All the key features of implementing the command in various circumstances were covered. Most importantly, the “context” setting is a well-known doubt in the beginner community.

Updated on: 11-Jan-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements