Quiz Entry - updated: 2026.11.10
A website is split into three services: frontend (port 4200), backend (port 3000, reaches its database under the name database) and a MongoDB 5.x database. What does the Compose file need so that all three can talk to each other?
All three on a shared network (Compose's default network or a user-defined bridge), the database service named exactly database, depends_on so the backend starts after it, a mongo:5.x tag, and published ports 4200:4200 and 3000:3000 but none for the database.
services:
frontend:
build: frontend
ports:
- "4200:4200"
depends_on:
- backend
backend:
build: backend
ports:
- "3000:3000"
depends_on:
- database
database:
image: mongo:5.0
Why each piece is there:
- Shared network and the service name. Compose puts all services of a project on one network with built-in DNS, where each service name is a hostname. The backend's code connects to
database, so the service must be called exactly that. Any other name does not resolve. depends_on: databasestarts the database before the backend, avoiding errors at start-up (strictly, it ensures start order, not readiness).mongo:5.0tag. The backend is incompatible with the current MongoDB major version; without a tag Docker would pullmongo:latest.- Ports. Each port is mapped to the same number on the host. The frontend is opened in the browser on 4200. The backend is published on 3000 because the frontend's code runs in the user's browser, so its API calls come from outside the Docker network. The database gets no
ports:entry: only the backend needs it, over the internal network, and leaving it unpublished keeps it off the host's interfaces. build:instead ofimage:for frontend and backend, since they come from the (completed) Dockerfiles in their folders; start withdocker compose up --build -d.
Go deeper:
Docker Docs: Networking in Compose — the default project network and service names as hostnames.
Docker Docs: Manage secrets securely in Docker Compose — keeping credentials out of the Compose file.