Build is an optional part of the Compose Specification. It tells Compose how to (re)build an application from source and lets you define the build process within a Compose file in a portable way. build can be either specified as a single string defining a context path, or as a detailed build definition.
In the former case, the whole path is used as a Docker context to execute a Docker build, looking for a canonical
Dockerfile at the root of the directory. The path can be absolute or relative. If it is relative, it is resolved
from the directory containing your Compose file. If it is absolute, the path prevents the Compose file from being portable so Compose displays a warning.
In the latter case, build arguments can be specified, including an alternate Dockerfile location. The path can be absolute or relative. If it is relative, it is resolved
from the directory containing your Compose file. If it is absolute, the path prevents the Compose file from being portable so Compose displays a warning.
Using build and image
When Compose is confronted with both a build subsection for a service and an image attribute, it follows the rules defined by the pull_policy attribute.
If pull_policy is missing from the service definition, Compose attempts to pull the image first and then builds from source if the image isn't found in the registry or platform cache.
Publishing built images
Compose with build support offers an option to push built images to a registry. When doing so, it doesn't try to push service images without an image attribute. Compose warns you about the missing image attribute which prevents images being pushed.
Illustrative example
The following example illustrates Compose Build Specification concepts with a concrete sample application. The sample is non-normative.
When used to build service images from source, the Compose file creates three Docker images:
example/webapp: A Docker image is built usingwebappsub-directory, within the Compose file's folder, as the Docker build context. Lack of aDockerfilewithin this folder returns an error.example/database: A Docker image is built usingbackend