AICollection Help

docker build

The docker build command is used to build an image from a Dockerfile. This command reads the instructions from a Dockerfile and creates a Docker image based on those instructions.

Basic Syntax

docker build [OPTIONS] PATH | URL | -

Key Options and Parameters

  1. -t, --tag: Name and optionally a tag in the 'name:tag' format.

    docker build -t myapp:latest .
  2. -f, --file: Name of the Dockerfile (default is 'PATH/Dockerfile').

    docker build -f Dockerfile.dev -t myapp:dev .
  3. --build-arg: Set build-time variables.

    docker build --build-arg VERSION=1.0 -t myapp:latest .
  4. --no-cache: Do not use cache when building the image.

    docker build --no-cache -t myapp:latest .
  5. --rm: Remove intermediate containers after a successful build (default is true).

    docker build --rm -t myapp:latest .
  6. --pull: Always attempt to pull a newer version of the image.

    docker build --pull -t myapp:latest .

Examples

  1. Building an Image from a Dockerfile

    docker build -t myapp:latest .

    This command builds an image named myapp with the tag latest from the Dockerfile in the current directory.

  2. Specifying a Different Dockerfile

    docker build -f Dockerfile.dev -t myapp:dev .

    This command builds an image using Dockerfile.dev instead of the default Dockerfile.

  3. Using Build Arguments

    docker build --build-arg VERSION=1.0 -t myapp:latest .

    This command sets a build-time variable VERSION to 1.0.

  4. Building Without Cache

    docker build --no-cache -t myapp:latest .

    This command builds the image without using any cached layers.

  5. Pulling the Latest Base Image

    docker build --pull -t myapp:latest .

    This command ensures that the latest version of the base image is used.

Conclusion

The docker build command is essential for creating Docker images from a Dockerfile. Understanding its options and parameters allows you to effectively build and manage Docker images based on your requirements.

Last modified: 05 December 2024