Build an image from a Dockerfile

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Builds a Docker image from the Dockerfile in the current directory and tags it, so it can be run locally or pushed to a registry.

dockerbuildimagedockerfile

Parameters

Name and tag for the built image, such as myapp:dev.

Commands

Build the image from ./Dockerfile using the current directory as build context

docker build --tag <tag> .

Confirm the built image exists and see its size

docker image ls <tag>

Verification

docker image ls <tag>

The image is listed with a recent CREATED time and a non-zero size.

Undo

Remove the image that this build created

docker rmi <tag>

Pitfalls

  • The whole current directory is sent as build context; add a .dockerignore to exclude large or sensitive files.
  • Rebuilding with the same tag leaves the previous image as a dangling <none> image; prune occasionally.
  • Use --file to point at a Dockerfile outside the current directory, and --no-cache to force a clean rebuild.

Related