Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Docker Compose: service has neither an image nor a build context

Compose refuses a service with no source to run from. Fix missing image keys, indentation that orphaned a build block, profiles, and extends merges.

~5 min readDocker
#docker#docker-compose#yaml#build
Bold white headline reading NO IMAGE OR BUILD with a Compose service crate holding an empty cyan source slot.

Introduction & Quick Summary

Compose refuses the project before it starts any container:

code
validating /srv/app/docker-compose.yml: services.api: service "api" has neither an image nor a build context specified: invalid compose project

Every Compose service must say where its container image comes from. image: pulls a published image; build: compiles one from a Dockerfile. A service with neither has nothing to run, so the CLI rejects the whole file, not just that service.

Where it appears:

  • A service block where image: was deleted in favour of a local build, and build: was never added.
  • A YAML indentation mistake that pushed image: into a neighbouring block.
  • image: ${API_IMAGE} with the variable unset, which interpolates to an empty string.
  • A merge or extends that overrode build: with nothing.
  • A service that only exists to satisfy depends_on and was left as an empty mapping.

Quick answer: run docker compose config to see the resolved file, then give the service either an image: with a full reference or a build: block that names a context.

Core Mechanics, Signatures & Reproducible Setup

Compose loads the file, applies interpolation, merges anchors, resolves extends, and then validates the project model. Validation is structural: the loader does not check whether the image exists, only that the service has a source.

# docker-compose.yml -- reproduce with: docker compose config services: db: image: postgres:17 environment: POSTGRES_PASSWORD: example api: depends_on: - db ports: - "3000:3000"
code
validating /srv/app/docker-compose.yml: services.api: service "api" has neither an image nor a build context specified: invalid compose project

The corrected file, with both forms shown for comparison:

services: db: image: postgres:17 api: build: context: . dockerfile: Dockerfile ports: - "3000:3000" depends_on: - db worker: image: ghcr.io/acme/worker:1.4.2 depends_on: - db

Compose reports nearby problems with different wording, and the difference matters:

MessageMeaningFix
has neither an image nor a build context specifiedno source for the serviceadd image or build
build path ... either does not exist or is not accessiblecontext path wrongcorrect context
pull access denied for Ximage reference not published or privatelog in or fix the name
service "x" depends on undefined service "y"typo in a dependencyfix the name
env file ... not foundmissing env_filecreate it or remove the key
additional property x is not allowedunknown key for this Compose versioncheck the schema

Useful diagnostic commands:

bash
docker compose config # resolved YAML, interpolation applied docker compose config --services # list service names as Compose sees them docker compose config --quiet # validate only, no output docker compose build api # build one service from its Dockerfile

Key Comparison Table

Service sourceKeys usedWhen to choose
Published imageimage: repo/name:tagthe image is built and pushed elsewhere
Local buildbuild: .the code lives beside the compose file
Build with a tagbuild: . plus image: app:localyou want a stable name for later runs
Image plus buildbothbuild locally, tag it, allow pull as fallback
Extended serviceextends: { file, service }sharing a base definition across files

What each declaration actually does:

DeclarationPullsBuildsTags the result
image: nginx:1.27yesnouses the upstream tag
build: .noyesgenerated name, hard to reference
build: . + image: app:localnoyesapp:local
image: app:local + build: .only with --pullyesapp:local
neithern/an/arejected at load time

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. See the resolved file before guessing

Interpolation and merging happen before validation, so the file on disk can look correct while the resolved model is empty.

bash
docker compose config | sed -n '/api:/,/^ [a-z]/p'
# resolved output showing the empty service api: networks: default: null ports: - mode: ingress target: 3000 published: "3000"

When the resolved block has ports and dependencies but no image and no build, the source is missing in the file rather than hidden by a variable.

2. Find the empty interpolation

An unset variable becomes an empty string, and an empty image: value counts as absent.

services: api: image: ${API_IMAGE}
bash
env | grep API_IMAGE || echo "not set" docker compose config | grep -A2 ' api:'

Two fixes: give the variable a default, or make the missing value fail loudly.

services: api: image: ${API_IMAGE:?API_IMAGE must be set}

The :? form stops Compose with your message instead of the generic validation error. Keep defaults for local runs in a .env file next to the compose file, and check its contents with the environment tool before debugging further.

3. Repair indentation that swallowed the key

YAML accepts both image: at two spaces and at four, but a key one level too deep belongs to the previous block and disappears from the service.

services: api: ports: - "3000:3000" image: node:22 # this is a port mapping key, not a service key
services: api: ports: - "3000:3000" image: node:22 # correct: sibling of ports

Convert the file with the YAML tool and look at the JSON: a key nested under an array item is visible immediately. Compare against a known-good file with Diff when only one service broke.

4. Check what extends and anchors actually merged

An inherited definition can be overridden into an empty service, and anchors with <<: merge shallowly.

x-base: &base restart: unless-stopped build: context: . services: api: <<: *base image: "" # overrides nothing useful, and clears the source
services: api: <<: *base environment: NODE_ENV: production

Remove the empty image: key; a key with an empty value is not the same as a missing key to the validator. docker compose config shows the merged result, which is the only reliable way to know what survived.

5. Services that only exist for profiles or tools

A service gated behind a profile can still be validated when you run it explicitly. If the error appears only when targeting it, that service is the broken one.

services: migrate: profiles: ["tools"] image: ghcr.io/acme/api:1.4.2 command: ["npm", "run", "migrate"]
bash
docker compose --profile tools run --rm migrate docker compose --profile tools config --services

--profile tools evaluates the profile-gated services, and the error names whichever one has no source. Remove the profile from the command once the service is fixed, so a normal up does not start it.

Common Compilation/Runtime Mistakes & Version Gotchas

Assuming build: alone is enough to run. A service with only build: gets an auto-generated image name. It runs, but you cannot reference it from another compose file or push it. Add image: beside build: when the name matters.

Writing build as a string with the wrong shape. build: ./Dockerfile treats ./Dockerfile as the context directory, not the Dockerfile. Use build: { context: ., dockerfile: Dockerfile } when you need to be explicit.

Forgetting depends_on does not create services. depends_on only orders starts; if the referenced service is missing or misspelled, Compose tries to start the dependency and reports a different error. Check the service list with docker compose config --services.

Leaving the obsolete version: key. Compose v2 ignores version: and warns on it. It is not the cause of this error, but a file written for Compose v1 often carries other stale assumptions, including links and the build: <path> shorthand.

Interpolation versus container variables. ${VAR} is resolved by Compose from the shell and .env; $$VAR is passed through to the container. A value intended for the container, written with one $, can empty the image: line at load time and produce this error.

Version differences. Compose v2 in current Docker releases validates the project before building, so the error appears before any network or build output. Older docker-compose v1 sometimes printed a build error first. The fix is the same, but the first line of output differs and misleads searches.

Verification order. docker compose config --quiet for structure, docker compose config to read the resolved model, then docker compose build <service> and docker compose up -d. Each step isolates one failure class, which keeps the generic validation message from hiding a real build problem.

Reference material: Compose services reference and Compose build reference.

Official references

primary documentation

The Docker documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Docker guides

More in Docker

All Docker guides

The guides filed next to this one in Docker, in library order.