Skip to content

Volumes and networks

A container’s own filesystem disappears when the container is removed. Anything you want to keep goes in a volume or a bind mount.

Managed by Docker/Podman, stored in their own area on disk.

Terminal window
docker volume create pgdata
docker run -d --name db -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data postgres:17
docker volume ls
docker volume inspect pgdata
docker volume rm pgdata
docker volume prune # remove volumes not used by any container

A folder from the host, mounted into the container. Handy in development to edit code without rebuilding.

Terminal window
docker run -d -p 8080:80 -v "$PWD/site:/usr/share/nginx/html:ro" nginx:1.27
  • :ro mounts it read-only.
  • The host path must be absolute ($PWD helps).

On systems with SELinux (Fedora, RHEL…), Podman and Docker need the folder relabelled or the container gets “Permission denied”:

Terminal window
podman run -v "$PWD/site:/usr/share/nginx/html:Z" … # private to this container
podman run -v "$PWD/site:/usr/share/nginx/html:z" … # shared between containers

Containers on the same user-defined network reach each other by container name:

Terminal window
docker network create appnet
docker run -d --name db --network appnet -e POSTGRES_PASSWORD=secret postgres:17
docker run -d --name api --network appnet -p 8000:8000 -e DB_HOST=db myapp:1.0

Here api connects to the database at host db, port 5432. Only port 8000 is published to the host.

Terminal window
docker network ls
docker network inspect appnet
docker network rm appnet

To reach a service running on the host from inside a container, use host.docker.internal (Docker Desktop) or host.containers.internal (Podman).