Managing Docker volumes and networks
Managing Docker volumes and networks
This article explains how to manage Docker volumes and networks on Pixzo, aimed at users who already work with Docker containers but have not yet configured volumes or networks.
Requirements
- Docker Engine 24.0 or higher - Access to a terminal with Docker commands - Existing Docker containers to attach to volumes and networks
Steps
1. Check existing volumes
docker volume ls
This command shows all volumes on your system. Volumes store data that must survive after a container is stopped or removed. Unlike a container's internal filesystem layer, which disappears when the container is removed, volumes persist unless you explicitly delete them.
2. Create a new volume
docker volume create mijn-volume
This command creates a new volume named `mijn-volume`. Docker assigns the `local` driver by default, which means the volume is stored on the host machine under `/var/lib/docker/volumes/`.
3. Attach a volume to a container
docker container run -d --name mijn-container -v mijn-volume:/data nginx:alpine
The `-v` option attaches the volume `mijn-volume` to the path `/data` in the container. All data the container writes to `/data` is stored in the volume and remains when you remove the container with `docker container rm mijn-container`.
4. Inspect volume details
docker volume inspect mijn-volume
This command shows metadata about the volume, including the mount point on the host and the driver used. You will also see whether the volume is used by containers.
5. Remove a volume
docker volume rm mijn-volume
Docker only removes volumes that are not used by a container. This precaution prevents you from accidentally losing data. Use `docker volume rm -f mijn-volume` to remove a volume still attached to a container — this forces the removal.
6. Show available networks
docker network ls
This command shows all networks on your system. Docker creates three networks by default: `bridge`, `host`, and `none`. The `bridge` network is the default network for containers and allows them to communicate with each other via IP addresses.
7. Create a custom network
docker network create --driver bridge mijn-netwerk
With this command you create a new bridge network. Custom networks provide better isolation and controlled communication between containers. You can also add a host-to-IP mapping when starting a container with the `--add-host` option, which adjusts DNS resolution inside the container.
8. Attach a container to a network
docker network connect mijn-netwerk mijn-container
This command connects an existing container to a network. You can attach a container to multiple networks. Containers within the same network can reach each other via their container name, which simplifies communication between services.
Verification
Check whether your volume and network function correctly:
docker volume ls
docker network ls
docker container inspect mijn-container | grep -A 10 "Networks"The first two commands confirm that the volume and network exist. The inspection command shows which network the container is attached to. You will also see whether the volume is correctly mounted under the path `/data`.
Frequently asked problems
1. Cannot remove a volume
Docker refuses the removal if a container uses the volume. Solve this by first stopping and removing the container:
docker container rm -f mijn-container
docker volume rm mijn-volume2. Containers cannot reach each other via the network
Containers on different networks do not communicate with each other. Make sure both containers are on the same network:
docker network connect mijn-netwerk container-a
docker network connect mijn-netwerk container-b3. Unused volumes take up disk space
Clean up unused volumes with the cleanup command:
docker volume prune
Add `--filter` to remove only volumes older than a certain time:
docker volume prune --filter "until=168h"
Docker applies the same caution when cleaning up networks — `docker network prune` only removes networks not used by any container.
The lifecycle of volumes and networks is independent of that of containers. A container can be paused with `docker container pause`, which temporarily stops all processes, but the volume and the network connection remain intact. When you resume the container, applications continue where they left off — with the same volume data and network connections. That independence means you can manage volumes and networks without affecting the containers, but it also requires you to keep track yourself of which resources are active.