Docker Compose "service refers to undefined network" in CI
Every network a service attaches to under its networks: key must be declared in the top-level networks: section. Naming a network on a service without declaring it makes Compose fail config validation with refers to undefined network.
What this error means
A docker compose up/config fails with service "<name>" refers to undefined network "<net>". The network is used but not declared at the top level.
docker
service "api" refers to undefined network "backend": invalid compose projectCommon causes
A network used but not declared
A service lists backend under its networks: but there is no top-level networks: backend: entry.
A typo between service and top-level network names
Mismatched names mean the referenced network is effectively undefined.
How to fix it
Declare the network at the top level
- Add the network to the top-level
networks:section. - Keep the names identical to the service reference.
docker-compose.yml
services:
api:
networks: [backend]
networks:
backend:
driver: bridgeAlign the network names
- Fix any typo so the service reference matches a declared network.
docker-compose.yml
networks:
backend: {}
frontend: {}How to prevent it
- Declare every referenced network at the top level.
- Keep service and top-level network names identical.
- Validate with
docker compose configin CI.
Related guides
Docker Compose "network declared as external, but could not be found"Fix Docker Compose "network X declared as external, but could not be found" in CI - an external network that…
Docker Compose "network not found" in CIFix the Docker Compose "network <name> not found" / "network ... declared as external, but could not be found…
Docker Compose "depends on undefined service" in CIFix Docker Compose "service <a> depends on undefined service <b>" in CI - a depends_on entry naming a service…