Core Concepts

Volumes and Networking

Persist data with volumes and connect containers together using Docker networks.

Docker Volumes

Containers are ephemeral — data is lost when the container is removed. Volumes persist data beyond the container lifecycle.

Types:

  • Named volumes: Managed by Docker (docker volume create)
  • Bind mounts: Map a host directory into the container
  • tmpfs mounts: In-memory only

Docker Networking

Containers can communicate with each other over Docker networks.

Types:

  • bridge (default): Containers on same network can communicate
  • host: Container uses host's network directly
  • none: No networking

Container Communication

Containers on the same network can reach each other by container name or service name (in Compose).

Example

bash
# Volumes
# Create a named volume
docker volume create my-data

# Run container with named volume
docker run -d   -v my-data:/var/lib/postgresql/data   -e POSTGRES_PASSWORD=secret   postgres:16

# Bind mount (map host directory)
docker run -d   -v $(pwd)/html:/usr/share/nginx/html   -p 8080:80   nginx

# Inspect volumes
docker volume ls
docker volume inspect my-data

# Networking
# Create a custom network
docker network create my-network

# Connect containers to the same network
docker run -d   --name postgres   --network my-network   -e POSTGRES_PASSWORD=secret   postgres:16

docker run -d   --name app   --network my-network   -e DATABASE_URL=postgresql://postgres:secret@postgres:5432/mydb   my-app:latest

# Containers can now communicate by name!
# app can reach postgres using hostname "postgres"

# Inspect network
docker network ls
docker network inspect my-network
Try it yourself — BASH