Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 83/ 160
05. Docker Compose & Multi-Container Apps

Defining Services, Builds, Images & Container Names

Declare services with image or build, and set clear container names.

Beginnerservices · build3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Keep service names stable for DNS and let Compose choose container names unless a real external tool requires the exception.

Think of it like…

A service is a role in the cast; the generated actor tag can change per performance, but the role name remains on the playbill.

Where you’ve seen it

In practice, services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } } is the concrete move for this job: declare services with image or build, and set clear container names. The release is not accepted until docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity confirms the result.

Words you’ll hear

services
The implementation control in services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } } for declare services with image or build, and set clear container names.
build
The result to observe with docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity, rather than infer from a successful command alone.
image
The production boundary exposed when pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged.

Visual diagram

Loading interactive diagram

Theory & deep dive

services control path

The concrete mechanism is services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } }. The observable result is docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity; if those disagree, inspect the runtime evidence before changing unrelated settings.

Declare services with image or build, and set clear container names. Keep services, build and image in the same deployment record so the intent and the observed state cannot drift apart.

Concrete services setup

services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } }

Key takeaway: Prove build with docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity, not with command success alone.

image failure boundary

A service name is also a network DNS record and survives container recreation. Setting container_name to the production hostname breaks --scale and makes a second project collide, while an image-only service cannot consume local Dockerfile changes until built separately. The narrow failure to design for is pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged.

Accept this compromise explicitly: Local build plus image tag supports fast iteration; image-only services are simpler when CI has already produced the artifact. The controlled check-docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged.

Key takeaway: Local build plus image tag supports fast iteration; image-only services are simpler when CI has already produced the artifact.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: A service name is also a network DNS record and survives container recreation. Setting container_name to the production hostname breaks --scale and makes a second project collide, while an image-only service cannot consume local Dockerfile changes until built separately.

Keep service names stable for DNS and let Compose choose container names unless a real external tool requires the exception.
Key numbers at this step
Intent
services

When to use it & trade-offs

What it is

A service is a logical container workload. image selects a registry artifact, build supplies context/Dockerfile options, and using both lets Compose tag the freshly built image. container_name disables Compose’s normal scalable naming and introduces global daemon-name collisions.

Why it matters: A service name is also a network DNS record and survives container recreation. Setting container_name to the production hostname breaks --scale and makes a second project collide, while an image-only service cannot consume local Dockerfile changes until built separately.

When to reach for it

  • The fit is strongest for this job: declare services with image or build, and set clear container names. Pair it with docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity in the runbook.
  • Production services changes should make build observable through services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } }, rather than trusting the runtime default.
  • Keep a rollback example for the case where pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged.

When to avoid or be careful

  • Do not accept command success while the system can still fail by pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged.
  • Avoid erasing image protection merely to dodge this cost: “Local build plus image tag supports fast iteration; image-only services are simpler when CI has already produced the artifact”.
  • Do not replace the evidence path-docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity-with a screenshot or a mutable tag.

services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } }: practical choices

Explicit services
Best for: Declare services with image or build, and set clear container names, with a reviewed runbook.

Strengths: A service is a logical container workload. image selects a registry artifact, build supplies context/Dockerfile options, and using both lets Compose tag the freshly built image. container_name disables Compose’s normal scalable naming and introduces global daemon-name collisions.

Watch out: Local build plus image tag supports fast iteration; image-only services are simpler when CI has already produced the artifact.

Ambient default
Best for: Short-lived build experiments with low blast radius.

Strengths: It can begin with services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } } and fewer surrounding controls.

Watch out: It silently accepts pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged, which makes the result hard to reproduce after promotion.

Managed image
Best for: Teams that centralize image policy across many workloads.

Strengths: A service name is also a network DNS record and survives container recreation. Setting container_name to the production hostname breaks --scale and makes a second project collide, while an image-only service cannot consume local Dockerfile changes until built separately.

Watch out: The platform adds an ownership and debugging layer; retain docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity as the direct check.

Interview signals & numbers to know

When this topic comes up

  • services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } }
  • docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity
  • Failure signature: pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged
  • Trade-off: Local build plus image tag supports fast iteration; image-only services are simpler when CI has already produced the artifact.

Benchmarks & rules of thumb

1 DNS service name -
provided on the default Compose network
1 container_name -
is globally unique on the daemon
2 build/input modes -
source build or prebuilt image

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } } as proof and skipping docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity.
  • Pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged
  • Optimizing services without accepting that local build plus image tag supports fast iteration; image-only services are simpler when CI has already produced the artifact.

Likely interviewer follow-ups

  • Why does services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } } address services here rather than through application code?
  • How would you distinguish healthy build from the failure “pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged” using docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity?
  • When would this trade-off-“Local build plus image tag supports fast iteration; image-only services are simpler when CI has already produced the artifact”-justify a different image design?
Interview tip: Give one concrete operation-services: { api: { build: { context: ., target: runtime }, image: acme/api:dev, container_name: api-debug } }-and one proof-docker compose config --images and docker compose ps --format json to distinguish service, image and generated container identity. Then name the boundary: pinning container_name for every service and then trying to scale, or changing a service key but leaving dependent DNS names unchanged.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Defining Services, Builds, Images & Container Names with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Compose Specification & compose.yaml Structure
Next topic
Service Dependencies (depends_on) & Health-Check Conditions (service_healthy)