Kennisbank › Docker › Managing Docker volumes and networks
docker gevorderd 01 June 2026

Managing Docker volumes and networks

Managing Docker Volumes and Networks

Managing Docker Volumes and Networks

This article explains how to manage Docker volumes and networks for persistent data and secure service communication in multi-container applications.

Prerequisites

- Docker Engine 24.0 or higher - Docker Compose V2 (built into Docker Desktop or installed separately) - Basic knowledge of Docker containers and `docker-compose.yml` - Terminal or CLI with the `docker` command available

Steps

1. Create a named volume for persistent data Use `docker volume create` instead of bind mounts. This gives Docker full control over storage. docker volume create mijn-data

2. Define volumes declaratively in docker-compose.yml Add a top-level `volumes` section and attach it to a service. This ensures reproducibility and clear separation.

volumes:
     db-data:
   
   services:
     database:
       image: postgres:16
       volumes:
         - db-data:/var/lib/postgresql/data

3. Create a user-defined bridge network Instead of the default network, use a named network for better DNS resolution and isolation.

networks:
     app-net:
       driver: bridge

4. Attach services to the network Specify which services should communicate with each other. A database does not need to be directly reachable from the outside world.

services:
     web:
       image: nginx:latest
       networks:
         - app-net
     database:
       image: postgres:16
       networks:
         - app-net

5. Isolate a network with `internal: true` Use this for databases or backend services that do not need internet access.

networks:
     internal-net:
       driver: bridge
       internal: true

6. Add a service to multiple networks An application service can, for example, be part of both a public and an internal network.

services:
     app:
       image: my-app:latest
       networks:
         - frontend-net
         - backend-net

7. Clean up unused volumes Volumes no longer attached to a container continue to take up disk space. Clean them up periodically. docker volume prune

8. Use volume plugins for multi-host setups For clusters (Docker Swarm, Kubernetes), use external storage via plugins. This enables volume migration between hosts.

volumes:
     shared-data:
       driver: nfs
       driver_opts:
         server: nfs-server.example.com
         path: /exports/data

Verification

Check that volumes are correctly created and networks are functioning:

List all volumes
docker volume ls

Inspect a volume docker volume inspect mijn-data

Show networks and attached containers docker network ls docker network inspect app-net

Test DNS resolution between services: docker compose exec web ping database

Frequently Asked Problems

1. Container cannot find another service by hostname Cause: Services are not in the same user-defined network. Solution: Explicitly add both services to the same named network in `docker-compose.yml`.

2. Data disappears after container removal Cause: Use of container-internal storage instead of a volume. Solution: Define a named volume and attach it to the service. Committing containers is not a substitute.

3. Volume space runs out, but the container is still running Cause: Unused volumes remain. Solution: Run `docker volume prune` regularly or automate it in a cronjob. Add `--filter` to retain specific volumes.

Common errors arise from mixing bind mounts and volumes in the same application — choose one strategy and stick with it.

← Terug naar kennisbank