All volumes are stored in /var/lib/docker/volumes and are mounted as read-write by default.
Docker volume is used to persist and share container data across containers. Folders on your host machine’s hard drive are mounted into containers as volumes, allowing the container to write data into the host volumes.
Volume Types
Anonymous Volumes
Named Volumes
Bind Mounts
Read-Only Volumes
Anonymous volumes only exist as long as the container exists. They are automatically created and removed with the container. Bind mounts can prevent certain files from being overlapped.docker run -v /app/logs <image>
Named volumes persist even if you stop or remove the container. You cannot directly edit data inside a named volume — use bind mounts for that purpose.docker run -v mylog:/app/logs apiapp
docker run --mount source=mylog,destination=/app/logs apiapp
Bind mounts allow you to reflect code changes from local to container in real time. They are ideal for development but not recommended for production since environments may have different folder structures.The source and destination paths must be absolute paths:
- macOS / Linux:
$(pwd):/app
- Windows:
"%cd%":/app
docker run -v <local-path>:<container-path> <image>
docker run --mount type=bind,source=<local-path>,destination=<container-path> <image>
# Examples
docker run -v /app/api:/app/api myapi
docker run --mount type=bind,source=/app/api,destination=/app/api myapi
Read-only volumes prevent local files from being modified by changes made inside the container.docker run --mount type=bind,source=/root/logs,target=/api/logs,readonly myapi
docker run -v "/root/logs:/api/logs:ro" myapi
The readonly flag and :ro suffix indicate that this volume is for reading only.
Volume Operations
List volumes
Create a volume
docker volume create <volume-name>
Inspect a volume
docker volume inspect <volume-name>
Remove a volume
docker volume rm <volume-name>
docker volume remove <volume-name>
docker volume prune # remove all unused volumes