Docker build command

Docker command reference


docker buildThe command is used to build a Docker image from a Dockerfile.

docker buildThe command reads the instructions defined in the Dockerfile, builds the image step by step, and saves the final result to the local image repository.

Syntax

docker build [OPTIONS] PATH | URL | -
  • PATH: the directory path containing the Dockerfile, or.(Current directory).
  • URL: Points to the remote repository address (such as a Git repository) containing the Dockerfile.
  • -: Read the Dockerfile from standard input.

Common options:

  • -t, --tag: Specify a name and tag for the built image.
  • -f, --file: Specify the path to the Dockerfile (default isPATHunderDockerfile)。
  • --build-arg: Set build arguments.
  • --no-cache: Build the image without using the cache layer.
  • --rm: Remove intermediate containers after a successful build (enabled by default).
  • --force-rm: Always remove intermediate containers, regardless of whether the build succeeds or fails.
  • --pull: Always try to pull the latest base image from the registry.

More options explanation:

  • --build-arg=[]: Set variables when building the image.
  • --cpu-shares: Set CPU usage weight.
  • --cpu-period: Limit the CPU CFS period.
  • --cpu-quota: Limit the CPU CFS quota.
  • --cpuset-cpus: Specify the available CPU IDs.
  • --cpuset-mems: Specify the available memory node IDs.
  • --disable-content-trust: Ignore content trust verification (enabled by default).
  • -f: Specify the path to the Dockerfile.
  • --force-rm: Force removal of intermediate containers during the build process.
  • --isolation: Use the specified container isolation technology.
  • --label=[]: Set metadata for the image.
  • -m: Set the maximum memory value.
  • --memory-swap: Set the maximum value of swap space (memory + swap space),-1means no limit on swap space.
  • --no-cache: Do not use cache when building the image.
  • --pull: Try to pull the latest version of the base image.
  • --quiet, -q: Quiet mode; output only the image ID after a successful build.
  • --rm: Remove intermediate containers after a successful build (enabled by default).
  • --shm-size: Set/dev/shmsize, with a default value of 64M.
  • --ulimit: Set the Ulimit configuration.
  • --squash: Compress all steps in the Dockerfile into a single layer.
  • --tag, -t: Specify a name and tag for the image in the formatname:tagorname; multiple tags can be set for one image in a single build.
  • --network: Set during the buildRUNthe network mode of the instruction, with the default value beingdefault。

1. Build image

docker build -t myimage:latest .

This reads the Dockerfile from the current directory and builds an image named myimage:latest.

2. Specify the Dockerfile path

docker build -f /path/to/Dockerfile -t myimage:latest .

This reads the Dockerfile from the /path/to/ directory and builds an image named myimage:latest.

3. Set build arguments

docker build --build-arg HTTP_PROXY=http://proxy.example.com -t myimage:latest .

This will use the HTTP_PROXY environment variable during the build process.

4. Build the image without using the cache layer

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

This will ignore all cached layers when building the image, ensuring every step is re-executed.

Example - Building an image using a Dockerfile

1. Create a Dockerfile with the following content:

# Dockerfile 示例
FROM ubuntu:20.04
LABEL maintainer="[email protected]"
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/index.html
CMD ["nginx", "-g", "daemon off;"]

2. Build image

docker build -t mynginx:latest .

Output example:

Sending build context to Docker daemon  3.072kB
Step 1/5 : FROM ubuntu:20.04
20.04: Pulling from library/ubuntu
...
Step 2/5 : LABEL maintainer="[email protected]"
...
Step 3/5 : RUN apt-get update && apt-get install -y nginx
...
Step 4/5 : COPY index.html /var/www/html/index.html
...
Step 5/5 : CMD ["nginx", "-g", "daemon off;"]
...
Successfully built 123456789abc
Successfully tagged mynginx:latest

3. Verify image

docker images

Output example:

Notes

REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
mynginx      latest    123456789abc   10 minutes ago   200MB
  • Ensure the Dockerfile syntax is correct and execute each step in order.
  • Usage.dockerignoreThe file excludes unnecessary files and directories to reduce the size of the build context.
  • In production environments, try to use minimal base images to reduce image size and improve security.
  • Avoid exposing sensitive information (such as passwords, keys) in the Dockerfile.

docker buildThe command is the core tool for building Docker images. By defining a clear Dockerfile, you can automatically build the application's runtime environment and dependencies. When using it, make sure to set options appropriately and optimize the Dockerfile to improve build efficiency and image quality.


Docker command reference

other extensions