Docker Build Command
[Docker Command Encyclopaedia](#)
* * *
The `docker build` command is used to build Docker images from a Dockerfile.
The `docker build` 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 PATH | URL | -
* **`PATH`**: The directory path containing the Dockerfile or `.` (current directory).
* **`URL`**: Points to a remote repository address containing the Dockerfile (such as a Git repository).
* **`-`**: Read 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 is `Dockerfile` under `PATH`).
* **`--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 build success or failure.
* **`--pull`**: Always try to pull the latest base image from the registry.
More options:
* **`--build-arg=[]`**: Set variables when building the image.
* **`--cpu-shares`**: Set CPU usage weight.
* **`--cpu-period`**: Limit CPU CFS period.
* **`--cpu-quota`**: Limit CPU CFS quota.
* **`--cpuset-cpus`**: Specify usable CPU IDs.
* **`--cpuset-mems`**: Specify usable 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 build.
* **`--isolation`**: Use specified container isolation technology.
* **`--label=[]`**: Set metadata for the image.
* **`-m`**: Set maximum memory.
* **`--memory-swap`**: Set maximum swap space (memory + swap), `-1` means unlimited swap.
* **`--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, only output the image ID after successful build.
* **`--rm`**: Remove intermediate containers after successful build (enabled by default).
* **`--shm-size`**: Set the size of `/dev/shm`, default value is 64M.
* **`--ulimit`**: Set Ulimit configuration.
* **`--squash`**: Squash all steps in the Dockerfile into one layer.
* **`--tag, -t`**: Specify name and tag for the image, format is `name:tag` or `name`; you can set multiple tags for an image in a single build.
* **`--network`**: Set network mode for `RUN` instructions during build, default value is `default`.
**1γBuild an image**
docker build -t myimage:latest .
This will read the Dockerfile from the current directory and build an image named myimage:latest.
**2γSpecify Dockerfile path**
docker build -f /path/to/Dockerfile -t myimage:latest .
This will read the Dockerfile from the /path/to/ directory and build 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 image without cache layers**
docker build --no-cache -t myimage:latest .
This will ignore all cache layers when building the image, ensuring every step is re-executed.
### Example - Build an image using Dockerfile
1γCreate a Dockerfile with the following content:
# Dockerfile Example 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 .
Output example:
Sending build context to Docker daemon 3.072kBStep 1/5 : FROM ubuntu:20.0420.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 123456789abcSuccessfully tagged mynginx:latest
3γVerify the 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.
* Use `.dockerignore` file to exclude files and directories that are not needed, 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.
The `docker build` command is the core tool for building Docker images. By defining a clear Dockerfile, you can automate the construction of the application's runtime environment and dependencies. When using it, ensure reasonable option settings and optimize the Dockerfile to improve build efficiency and image quality.
* * Docker Command Encyclopaedia](#)
YouTip