Docker build Command
docker build The build command is used to build a Docker image from a Dockerfile.
docker build The command builds the image step by step by reading the instructions defined in the Dockerfile and saves the final result to the local image repository.
Syntax
docker build [OPTIONS] PATH | URL | -
PATH: The path to a directory containing the Dockerfile, or.(the current directory).URL: A remote repository address containing the Dockerfile (such as a Git repository).-: 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 (the default isPATHunderDockerfile)。--build-arg: Set build arguments.--no-cache: Build the image without using cache layers.--rm: Remove intermediate containers after a successful build (enabled by default).--force-rm: Always remove intermediate containers, regardless of whether the build succeeds.--pull: Always try to pull the latest base image from the registry.
More options:
--build-arg=[]: Set variables used when building the image.--cpu-shares: Set the CPU usage weight.--cpu-period: Limit the CPU CFS period.--cpu-quota: Limit the CPU CFS quota.--cpuset-cpus: Specify the CPU IDs that can be used.--cpuset-mems: Specify the memory node IDs that can be used.--disable-content-trust: Ignore content trust verification (enabled by default).-f: Specify the path to the Dockerfile.--force-rm: Force the removal of intermediate containers during the build.--isolation: Use the specified container isolation technology.--label=[]: Set metadata for the image.-m: Set the maximum memory limit.--memory-swap: Set the maximum swap space (memory + swap),-1means unlimited 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 the/dev/shmsize; the default value is 64M.--ulimit: Set Ulimit options.--squash: Squash 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 an image in a single build.--network: Set the network mode for theRUNinstruction during the build; the default value isdefault。
** 1. Build the 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 uses the HTTP_PROXY environment variable during the build.
** 4. Build without cache layers**
docker build --no-cache -t myimage:latest .
This ignores all cache layers when building the image, ensuring that every step is executed again.
Example: Building an Image with a Dockerfile
1. Create a Dockerfile with the following content:
# Dockerfile 示例
FROM ubuntu:20.04
LABEL maintainer="yourname@example.com"
RUN apt-get update && apt-get install -y nginx
COPY index.html /var/www/html/index.html
CMD ["nginx", "-g", "daemon off;"]
2. Build the image
docker build -t mynginx:latest .
Example output:
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="yourname@example.com"
...
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 the image
docker images
Example output:
Notes
REPOSITORY TAG IMAGE ID CREATED SIZE
mynginx latest 123456789abc 10 minutes ago 200MB
- Make sure the Dockerfile syntax is correct and that every step is executed in order.
- Use
.dockerignorefile to exclude unnecessary files and directories, reducing the size of the build context. - In production, try to use a minimal base image to reduce image size and improve security.
- Avoid exposing sensitive information (such as passwords and keys) in the Dockerfile.
docker build The build command is the core tool for building Docker images. By defining a clear Dockerfile, you can automate the construction of an 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