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.

Introduction & Quick Summary
Compose refuses the project before it starts any container:
validating /srv/app/docker-compose.yml: services.api: service "api"
has neither an image nor a build context specified: invalid compose projectEvery 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, andbuild: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
extendsthat overrodebuild:with nothing. - A service that only exists to satisfy
depends_onand 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"validating /srv/app/docker-compose.yml: services.api: service "api"
has neither an image nor a build context specified: invalid compose projectThe 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:
- dbCompose reports nearby problems with different wording, and the difference matters:
| Message | Meaning | Fix |
|---|---|---|
has neither an image nor a build context specified | no source for the service | add image or build |
build path ... either does not exist or is not accessible | context path wrong | correct context |
pull access denied for X | image reference not published or private | log in or fix the name |
service "x" depends on undefined service "y" | typo in a dependency | fix the name |
env file ... not found | missing env_file | create it or remove the key |
additional property x is not allowed | unknown key for this Compose version | check the schema |
Useful diagnostic commands:
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 DockerfileKey Comparison Table
| Service source | Keys used | When to choose |
|---|---|---|
| Published image | image: repo/name:tag | the image is built and pushed elsewhere |
| Local build | build: . | the code lives beside the compose file |
| Build with a tag | build: . plus image: app:local | you want a stable name for later runs |
| Image plus build | both | build locally, tag it, allow pull as fallback |
| Extended service | extends: { file, service } | sharing a base definition across files |
What each declaration actually does:
| Declaration | Pulls | Builds | Tags the result |
|---|---|---|---|
image: nginx:1.27 | yes | no | uses the upstream tag |
build: . | no | yes | generated name, hard to reference |
build: . + image: app:local | no | yes | app:local |
image: app:local + build: . | only with --pull | yes | app:local |
| neither | n/a | n/a | rejected 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.
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}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 keyservices:
api:
ports:
- "3000:3000"
image: node:22 # correct: sibling of portsConvert 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 sourceservices:
api:
<<: *base
environment:
NODE_ENV: productionRemove 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"]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 documentationThe Docker documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Docker guides
All Docker guidesDocker build: failed to compute cache key: not found
BuildKit cannot hash a file that is not in the context. Correct the context path, the .dockerignore rules, or the COPY source and stage name.
Docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock
An endpoint mismatch, a stopped daemon, or a missing socket mount. Resolve which of the three, then start, retarget, or mount.
Docker Compose: bind: address already in use and Port Conflicts
A published port is a host socket, and two projects cannot own it. How to find the real owner, why an override file merged instead of replacing, and how.
Docker Buildx: Building Multi-Architecture Images (AMD64 & ARM64) Explained
One tag, several architectures: the driver that supports it, QEMU versus cross-compilation, registry cache, and reading the manifest list.
More in Docker
All Docker guidesThe guides filed next to this one in Docker, in library order.