Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Docker build Command

Docker build Command

Docker Command ReferenceDocker Command Reference


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 | -

Common options:

More options:

** 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

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 ReferenceDocker Command Reference