DockerInterview preparation
Intermediate·Concept·2 min read

Volumes vs networks vs Compose?

Practice this question

Definition:

Volumes provide storage independent of a container's disposable writable layer. Container networks define communication paths and service-name resolution, while Compose declares a set of cooperating containers and their configuration. Together they make a local multi-service setup repeatable without placing every service in one container.

Key points:

  • Volumes persist data independently of a container's writable layer.

  • Networks let containers communicate; localhost means that same container.

  • Compose describes multiple services, networks and volumes in one configuration.

  • Persisting a database volume is not a backup; deletion, corruption and operator mistakes still require a recovery plan.

Example — two Compose services:

  • App service: api; database service: db; both join the same Compose network.
  • The app connects to db:5432, because service-name DNS resolves the database container.
  • A host mapping such as 5433:5432 lets a host client use port 5433; the app still uses port 5432 inside the network.
  • Database data belongs in a volume, with a separate tested backup procedure.

Interview point:

  • A database's host port is for access from outside that network; peer containers normally use the service name and container port.
Adapted from 09_TESTING_GIT_DOCKER_CLOUD.md · “Volumes vs networks vs Compose?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.