Explain Docker networking modes, volumes and Docker Compose.
Networking
- bridge (default): containers get private IPs on a virtual bridge;
-p 8080:8080publishes a port through NAT. User-defined bridge networks add built-in DNS, so containers reach each other by service or container name; the default bridge does not. - host: the container shares the host network stack: no NAT and best performance, but no port isolation.
- none: no networking.
- overlay: spans several hosts (Swarm); Kubernetes uses CNI plugins instead.
- macvlan: the container appears as a physical device on the LAN.
Storage
- The container's writable layer is ephemeral and slow (copy-on-write); never store data there.
- Named volumes (
docker volume create pgdata): managed by Docker, persistent, portable; the right choice for databases in development. - Bind mounts: a host directory mounted into the container; good for source code in development and config files, but tie the container to the host's layout and permissions.
- tmpfs: in-memory, for sensitive or scratch data.
Docker Compose
Defines a multi-container application in YAML: services, networks, volumes, environment, dependencies with health checks, profiles. Ideal for local development and integration testing (Spring Boot 3.1+ can start a compose.yaml automatically with spring-boot-docker-compose). Not a production orchestrator for multi-host systems.
Example
yaml
services:
app:
build: .
ports: ["8080:8080"]
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/shop # "db" resolved by Compose DNS
depends_on:
db: { condition: service_healthy }
db:
image: postgres:16-alpine
environment: { POSTGRES_DB: shop, POSTGRES_PASSWORD: secret }
volumes: [pgdata:/var/lib/postgresql/data]
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
volumes:
pgdata:Follow-up questions interviewers ask
Try answering each one before you open it.
Why can't my application connect to the database at localhost inside a container?
Inside a container, localhost is the container itself, not the host or another container. Use the other container's service name on a shared user-defined network (db:5432), or host.docker.internal to reach a service on the host (Docker Desktop, or add --add-host=host.docker.internal:host-gateway on Linux).