Skip to main content

How to Build a Docker Image from a Dockerfile with docker build

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A Dockerfile is a short recipe for an image: what to start from and what to run. docker build turns it into an image you can run like any other. This guide builds a two-line image that prints a greeting, with real output from Docker 29.9.3.

Syntax​

Syntax
docker build -t NAME PATH
PartWhat it means
-t NAMEThe name for the new image, such as hello-image
PATHThe folder with the Dockerfile and the files the build may use, usually ., the current folder

The reference: "The docker buildx build command starts a build using BuildKit." docker build is that command. The same command works in PowerShell.

Prerequisites​

  • Docker: Docker Desktop on Mac or Windows, running.

Step 1: Write the Dockerfile​

In an empty folder, create a file named Dockerfile, with no extension:

Dockerfile
FROM alpine:3.24
CMD ["echo", "Hello from my image"]

The Dockerfile in a macOS-style code window: FROM alpine:3.24 and CMD echo Hello from my image.

  • FROM alpine:3.24: start from the small Alpine Linux image.
  • CMD [...]: the command a container from this image runs when it starts.

Step 2: Build the Image​

From that folder:

Terminal
docker build -t hello-image .
Output
[+] Building 0.1s (5/5) FINISHED
=> [internal] load build definition from Dockerfile
=> => transferring dockerfile: 90B
=> [internal] load metadata for docker.io/library/alpine:3.24
=> [internal] load .dockerignore
=> => transferring context: 2B
=> [1/1] FROM docker.io/library/alpine:3.24
=> exporting to image
=> => exporting layers
=> => writing image sha256:9fad4bd81b49af94b960a6598eeffcbe9569bacc71214613e18fb98abaa59f4f
=> => naming to docker.io/library/hello-image
View build details: docker-desktop://dashboard/build/desktop-linux/desktop-linux/1fngg48xg14j0vfjvx6ge4s9e

docker build -t hello-image . in a terminal: Building 5/5 FINISHED, the steps from loading the Dockerfile to naming the image docker.io/library/hello-image.

BuildKit read the Dockerfile, used alpine:3.24 and saved the result as hello-image.

Step 3: Run It​

Terminal
docker run --rm hello-image
Output
Hello from my image

docker run --rm hello-image in a terminal, printing Hello from my image.

The container ran the image's CMD and printed the greeting. --rm removes the container when it stops.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the .ERROR: docker: 'docker buildx build' requires 1 argumentEnd the command with the folder, .
Building in a folder without a DockerfileERROR: failed to build: failed to solve: failed to read dockerfile: open Dockerfile: no such file or directoryRun it where the Dockerfile is, and check the file's name has no extension

Troubleshooting​

SymptomLikely causeFix
open Dockerfile: no such file or directoryThe file is named Dockerfile.txt or sits in another folderRename it to Dockerfile, or cd into its folder

FAQ​

What does the dot mean?​

It's the build context. Docker's build context guide: "The build context is the set of files that your build can access." Here it's the current folder, with the Dockerfile in it.

Where is the image now?​

On this computer, with your other images: docker images hello-image listed hello-image:latest, with a disk usage of 8.66MB.

Conclusion​

A two-line Dockerfile and docker build -t hello-image . made an image, and docker run --rm hello-image printed Hello from my image.

Additional Resources​