Introduction

  • TL;DR: docker build is the core command used to create a Docker image from a blueprint called a Dockerfile and a set of files known as the ‘build context’. The path specified at the end of the command (e.g., .) defines this context, while the -t flag assigns a name and tag to the image. The build process constructs the image by executing each instruction in the Dockerfile as a distinct layer, and using a .dockerignore file is crucial for optimizing build speed and image size by excluding unnecessary files.

The docker build command is the engine that turns your source code and instructions into a portable, runnable Docker image. This process involves reading a Dockerfile, collecting necessary files, and assembling them into a layered image that can be run as a container on any Docker host.

The basic syntax is:

1
docker build [OPTIONS] PATH | URL | -

The PATH argument is fundamental, defining the build context.

Why it matters: docker build is the heart of creating reproducible and portable application environments as code. It empowers developers to build an identical image anywhere, finally solving the age-old “it works on my machine” problem.

Core Concept 1: The Build Context

A common point of confusion for beginners is the . at the end of a docker build . command. This . signifies that the current directory should be used as the build context.

The build context is the set of files and directories at the specified PATH that are sent to the Docker daemon for the build. When the command runs, the Docker client archives this directory and sends it to the daemon. This is why any COPY or ADD instructions in your Dockerfile must refer to files that exist within this context.

The Importance of .dockerignore

Including unnecessary files in the build context (e.g., .git directory, node_modules, local logs, secrets) is problematic:

  1. Slower Builds: The daemon has to process a larger tarball.
  2. Larger Images: Unneeded files can bloat your final image.
  3. Security Risks: Sensitive credentials can be accidentally baked into the image.

The .dockerignore file solves this by excluding files and directories, similar to .gitignore.

Example .dockerignore file:

1
2
3
4
5
6
.git
.gitignore
node_modules
npm-debug.log
Dockerfile*
README.md

Why it matters: Properly managing your build context with a .dockerignore file is the first step toward creating efficient, lightweight, and secure Docker images. It’s the simplest and most effective optimization you can apply to your build process.

##Basic docker build Usage and Key Options

Let’s use a simple Node.js application to demonstrate the build process.

Project Structure:

1
2
3
4
5
.
├── .dockerignore
├── Dockerfile
├── package.json
└── server.js

Dockerfile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
FROM node:18-alpine

WORKDIR /app

# Separate COPY and RUN to leverage build cache
COPY package*.json ./
RUN npm install

# Copy the rest of the source code
COPY . .

EXPOSE 8080
CMD ["node", "server.js"]

Building and Tagging (-t) an Image

The most common build command is:

1
2
# docker build -t [image-name]:[tag] [build-context-path]
docker build -t my-node-app:1.0 .
  • -t or –tag: Assigns a name and tag to the image in name:tag format. If the tag is omitted, it defaults to latest. A common convention is username/repository:version.
  • .: Specifies the current directory as the build context.

Commonly Used Options

OptionDescriptionExample Usage
-f, –fileSpecifies the path and name of the Dockerfile to use. Defaults to Dockerfile in the context’s root.docker build -f ./dockerfiles/Dockerfile.dev .
--build-argPasses build-time variables to ARG instructions in the Dockerfile.docker build –build-arg VERSION=1.2 -t my-app .
--no-cacheDisables the use of cached layers, forcing a fresh build from scratch.docker build –no-cache -t my-app .
--pullAlways attempts to pull a newer version of the base image before starting the build.docker build –pull -t my-app .
--platformUsed to build an image for a different architecture (e.g., linux/arm64 on an Apple Silicon Mac).docker build –platform linux/amd64 -t my-app .

Sheets로 내보내기

Why it matters: These options provide the flexibility to create tailored images for different environments (like development vs. production) from the same codebase. The -f and –build-arg flags are especially powerful for managing image variations.

Leveraging the Build Cache for Optimization

Docker uses a layer caching mechanism to accelerate builds. Each instruction in a Dockerfile corresponds to a layer in the image.

During a build, Docker checks each instruction against previous builds. If an instruction and its input files haven’t changed, Docker reuses the existing layer from its cache. As soon as an instruction’s cache is “invalidated” (because a file changed or the instruction was modified), that layer and all subsequent layers are rebuilt.

This makes the order of your Dockerfile instructions critical for performance.

  • Place instructions that change infrequently at the top. (e.g., installing system dependencies).
  • Place instructions that change frequently at the bottom. (e.g., copying your application source code).

In our example, we COPY package*.json ./ and run npm install before COPY . .. This is because package.json changes less often than the source code, allowing Docker to reuse the heavy node_modules layer from the cache most of the time.

Why it matters: Understanding and optimizing for the build cache can drastically reduce your development feedback loop. In a CI/CD pipeline, saving even a few seconds on each build can lead to significant gains in overall developer productivity.


Summary

  • docker build is the command to create a Docker image using a Dockerfile and a build context.
  • The build context is the set of files sent to the Docker daemon; it should be optimized with a .dockerignore file.
  • The -t flag is essential for assigning a meaningful name and version tag to your image.
  • Docker’s layer cache speeds up builds by reusing unchanged layers, making the order of instructions in your Dockerfile crucial for optimization.

#Docker #dockerbuild #Dockerfile #DockerImage #Container #DevOps #BuildOptimization

References

  1. docker build command reference | Docker Documentation | 2025-09-27 | https://docs.docker.com/engine/reference/commandline/build/
  2. Best practices for writing Dockerfiles | Docker Documentation | 2025-09-27 | https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
  3. Understanding the Docker build context | Snyk | 2023-08-22 | https://www.google.com/search?q=https://snyk.io/blog/understanding-the-docker-build-context/
  4. How To Build a Node.js Application with Docker | DigitalOcean | 2023-01-18 | https://www.digitalocean.com/community/tutorials/how-to-build-a-node-js-application-with-docker
  5. Speed up Docker builds with dockerignore | ITNEXT | 2023-05-15 | https://www.google.com/search?q=https://itnext.io/speed-up-docker-builds-with-dockerignore-347c65388c1c