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.
Volumes
Section titled “Volumes”Managed by Docker/Podman, stored in their own area on disk.
docker volume create pgdatadocker run -d --name db -e POSTGRES_PASSWORD=secret -v pgdata:/var/lib/postgresql/data postgres:17docker volume lsdocker volume inspect pgdatadocker volume rm pgdatadocker volume prune # remove volumes not used by any containerBind mounts
Section titled “Bind mounts”A folder from the host, mounted into the container. Handy in development to edit code without rebuilding.
docker run -d -p 8080:80 -v "$PWD/site:/usr/share/nginx/html:ro" nginx:1.27:romounts it read-only.- The host path must be absolute (
$PWDhelps).
On systems with SELinux (Fedora, RHEL…), Podman and Docker need the folder relabelled or the container gets “Permission denied”:
podman run -v "$PWD/site:/usr/share/nginx/html:Z" … # private to this containerpodman run -v "$PWD/site:/usr/share/nginx/html:z" … # shared between containersNetworks
Section titled “Networks”Containers on the same user-defined network reach each other by container name:
docker network create appnetdocker run -d --name db --network appnet -e POSTGRES_PASSWORD=secret postgres:17docker run -d --name api --network appnet -p 8000:8000 -e DB_HOST=db myapp:1.0Here api connects to the database at host db, port 5432. Only port 8000 is published to the host.
docker network lsdocker network inspect appnetdocker network rm appnetTo reach a service running on the host from inside a container, use host.docker.internal (Docker Desktop) or host.containers.internal (Podman).