Volumes in Podman are used to persist data outside of a container's lifecycle. This section explores how to create and use volumes effectively.
Named volumes are reusable storage spaces managed by Podman. They can be created and attached to containers.
podman volume create myvolume
podman run -d --name myapp -v myvolume:/data nginx
- In this example, the volume
myvolume
is mounted to the/data
directory in the container.
Podman provides additional options when mounting volumes to control access and behavior.
The U
option ensures that the volume's ownership matches the container's user namespace.
podman run -d --name myapp -v myvolume:/data:U nginx
- Here, the
U
flag updates the ownership of the mounted volume to align with the container's user namespace.
You can mount a volume as read-only to prevent modifications from the container.
podman run -d --name myapp -v myvolume:/data:ro nginx
You can combine options such as U
and ro
:
podman run -d --name myapp -v myvolume:/data:U,ro nginx
The --mount
option provides a more flexible way to define volume mounts.
podman run -d --name myapp --mount type=volume,source=myvolume,target=/data nginx
- type: Specifies the mount type (
volume
in this case). - source: The name of the volume to mount.
- target: The path inside the container where the volume is mounted.
Bind mounts map a host directory to a container.
podman run -d --name myapp --mount type=bind,source=/host/data,target=/container/data nginx
- type: Specifies the mount type (
bind
for host directories). - source: The host directory path.
- target: The container directory path.
podman run -d --name myapp --mount type=bind,source=/host/data,target=/container/data,readonly nginx
- Adding the
readonly
flag ensures the container cannot modify the mounted directory.
This chapter provides foundational knowledge for using and customizing volumes in Podman containers, emphasizing the flexibility and security options available with commands like -v
and --mount
.