While creating the image from the Dockerfile, usually beginners face the “buildx requires 1 argument” error. Let’s first understand why this problem is encountered and how to fix it.

Why “docker buildx build requires 1 argument” Error Occur in Docker?

Docker is utilized to create, deploy, and deliver applications or software in executable packages (containers). Applications are containerized through Docker images. To containerize the user program or app, you first need to create the Docker image. The Docker image is a simple instruction template that instructs the container on how to dockerize the application.

Docker images can be generated through Dockerfile. The Dockerfile is a file that contains commands and instructions on how to containerize the application. This will first generate the image of the container, then when the image is executed, it will create and deploy the container.
When the user tries to generate the image from the Dockerfile instructions, the user may encounter an error “buildx build requires 1 argument”. This error occurs because Docker Daemon failed to access the build content to generate the new image. Here are some common reasons that can generate the stated error:

  • When Docker Daemon fails to access the Dockerfile to create the container snapshot, the user encounters the stated error.
  • When mistakenly, the user forgets to put “.” at the end of the command. The “.” means read the Dockerfile from the current working directory.
  • This error may occur, if the user fails to provide arguments such as the user specifying the image name but without using the “–tag” or “-t” option. Incorrect command format can also generate the “buildx requires 1 argument” error.
  • When the user creates the Dockerfile with any other name like “Dockerfile1”, then the user faces the error because, with the “.” argument, Docker Daemon is supposed to find the “Dockerfile” instead of “Dockerfile1”. If your Dockerfile name is different, the user needs to specify the filename in the “docker build” command with the “-f” option:
buildx requires 1 argument error

Note: However, sometimes, users specify the correct path of Dockerfile with the correct format but still get an error “buildx Requires 1 argument”. This is because they forget to put “.” at the end of the command. Users may think they have specified the exact path and do not require the “.” argument. 

The “.” argument at the end of the command is necessary in both cases either reading the file from the currently opened directory or from another path:

buildx requires 1 argument error

Let’s walk through the below section to tackle the stated issue and understand how to use the “docker build” command in the right way to generate the container template (image).

How to Fix the “docker buildx build requires 1 argument” Error in Docker?

To correctly use to “docker build” command to ignore the “buildx requires 1 argument” error, go through the following procedure:

Step 1: Create a Dockerfile

First, make a new file in the nano editor named “Dockerfile”. This file does not container in file extension:

sudo nano Dockerfile
sudo nano Dockerfile

Paste the below-mentioned commands into the file. Here, “FROM” is utilized to set the base image for the container, and “COPY” will copy the source program to the container. For instance, we have created a simple HTML program in the “index.html” file. The “ENTRYPOINT” will specify the points from where the container will start execution:

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]
FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

Save the Dockerfile commands through “CTRL+S” and exit the editor through “CTRL+X”.

Step 2: Execute Build Command

Next, utilize the build command to generate the snapshot (image) for the container. For this purpose, run the “docker build -t <image> .” command. Here, we are reading the build context or Dockerfile from the currently opened directory:

docker build -t html-image .

Here, you can see we did not encounter any error and successfully generated the image. 

docker build -t html-image .

For confirmation, display the images of Docker:

docker images

Here, we have created “html-image” effectively:

docker images

Step 3: Fire Up Container

Now, fire up the container from the image generated in the above step. To do so, carry out the below command:

docker run -p 80 html-image
docker run -p 80 html-image

Now, list down the containers and check if the container is executed and on which port it is exposing the containerize application:

docker ps -a
docker ps -a

Open up the system browser, navigate to “http://localhost:<port-no>” and check if application is running:

Output: Welcome to Hands-on Cloud Tutorial

How to Read Dockerfile From a Specific Path?

Another common error that occurs while executing the “docker build” is “Failed to read dockerfile: open Dockerfile…”. This error is encountered because Docker Daemon does is unable to find the Dockerfile in the current directory or on the specified path:

Failed to read dockerfile: open Dockerfile…

To fix the “Failed to read dockerfile” problem and to avoid the “buildx requires 1 argument” error, specify the correct Dockerfile path and Dockerfile name. Also, make sure, you are using the correct “docker build” command format.

To read files from another directory or from the specific path, utilize the “-f” option along with the path to Dockerfile. Now, the build command syntax will be “docker build -t <image> -f <path-to-Dockerfile> .”:

docker build -t html-image -f docker/html/Dockerfile .
docker build -t html-image -f docker/html/Dockerfile .

We have discussed the reason behind the “buildx requires 1 argument” error and provided the procedure to use “docker build” in the right way.

Conclusion

To “buildx requires 1 argument” error occurs due to various reasons such as the user not using the “docker build” command in the right way, Docker Daemon not Dockerfile in the current directory, or the Dockerfile name being different. To fix the stated issue, try to use the “docker build” command in the right format. If the Dockerfile is placed in some other directory, use the “-f” option along with the Dockerfile path such as “docker build -t <image> -f <path-to-Dockerfile> .” This write-up has discussed how to tackle the “buildx build requires 1 argument” error.