Ensures that each service in a Docker Compose configuration uses either build or image, but not both. Using both
directives can cause ambiguity and unpredictable behavior during container creation.
This rule is not fixable, as it requires user intervention to decide whether to keep build or image.
- Rule Name: no-build-and-image
- Type: error
- Category: best-practice
- Severity: major
- Fixable: false
-
checkPullPolicy (boolean): Controls whether the rule should allow the simultaneous use of
buildandimageif the service also specifiespull_policy(Defaulttrue).Behavior:
- If
checkPullPolicyistrue, the rule permits bothbuildandimageto be used together, but only ifpull_policyis present in the service definition. - If
checkPullPolicyisfalse, the rule enforces that each service uses eitherbuildorimageexclusively, regardless of the presence ofpull_policy.
- If
services:
web:
build: .
image: imageservices:
web:
build: .or
services:
web:
image: imageThe build and image directives in Docker Compose represent two different approaches to service configuration:
buildspecifies that the service should be built from a Dockerfile in the specified directory.imagespecifies that the service should use a pre-built image from a registry.
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.
Using both directives for the same service can lead to ambiguity and unexpected behavior during the build and deployment
process. Therefore, this rule enforces that each service should only use one of these directives unless
checkPullPolicy allows both.
This rule was introduced in v1.0.0.