Skip to content

Latest commit

 

History

History
89 lines (56 loc) · 2.57 KB

chapter-3-basic-container-operations.md

File metadata and controls

89 lines (56 loc) · 2.57 KB

Chapter 3: Using Volumes with Containers

3.1 Using Volumes with Containers

Volumes in Podman are used to persist data outside of a container's lifecycle. This section explores how to create and use volumes effectively.

3.1.1 Named Volumes

Named volumes are reusable storage spaces managed by Podman. They can be created and attached to containers.

Creating a Named Volume

podman volume create myvolume

Using a Named Volume with a Container

podman run -d --name myapp -v myvolume:/data nginx
  • In this example, the volume myvolume is mounted to the /data directory in the container.

3.1.2 Volume Mount Options

Podman provides additional options when mounting volumes to control access and behavior.

Using the U Option for Volume Mounting

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.

Read-Only Volumes

You can mount a volume as read-only to prevent modifications from the container.

podman run -d --name myapp -v myvolume:/data:ro nginx

Combined Options

You can combine options such as U and ro:

podman run -d --name myapp -v myvolume:/data:U,ro nginx

3.1.3 Using the --mount Command Option

The --mount option provides a more flexible way to define volume mounts.

Example: Mounting a Volume with --mount

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.

Example: Using Bind Mounts

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.

Advanced Example: Read-Only Bind Mount

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.