5.12. Sharing files between two containers
You can use volumes to persist data in containers even when a container is deleted. Volumes can be used for sharing data among multiple containers. The volume is a folder which is stored on the host machine. The volume can be shared between the container and the host.
Main advantages are:
- Volumes can be shared among the containers.
- Volumes are easier to back up or migrate.
- Volumes do not increase the size of the containers.
Prerequisites
-
The
container-toolsmeta-package is installed.
Procedure
Create a volume:
$ podman volume create hostvolumeDisplay information about the volume:
$ podman volume inspect hostvolume [ { "name": "hostvolume", "labels": {}, "mountpoint": "/home/username/.local/share/containers/storage/volumes/hostvolume/_data", "driver": "local", "options": {}, "scope": "local" } ]Notice that it creates a volume in the volumes directory. You can save the mount point path to the variable for easier manipulation:
$ mntPoint=$(podman volume inspect hostvolume --format {{.Mountpoint}}).Notice that if you run
sudo podman volume create hostvolume, then the mount point changes to/var/lib/containers/storage/volumes/hostvolume/_data.Create a text file inside the directory using the path that is stored in the
mntPointvariable:$ echo "Hello from host" >> $mntPoint/host.txtList all files in the directory defined by the
mntPointvariable:$ ls $mntPoint/ host.txtRun the container named
myubi1and map the directory defined by thehostvolumevolume name on the host to the/containervolume1directory on the container:$ podman run -it --name myubi1 -v hostvolume:/containervolume1 registry.access.redhat.com/ubi10/ubi /bin/bashNote that if you use the volume path defined by the
mntPointvariable (-v $mntPoint:/containervolume1), data can be lost when runningpodman volume prunecommand, which removes unused volumes. Always use-v hostvolume_name:/containervolume_name.List the files in the shared volume on the container:
# ls /containervolume1 host.txtYou can see the
host.txtfile which you created on the host.Create a text file inside the
/containervolume1directory:# echo "Hello from container 1" >> /containervolume1/container1.txt-
Detach from the container with
CTRL+pandCTRL+q. List the files in the shared volume on the host, you should see two files:
$ ls $mntPoint container1.rxt host.txtAt this point, you are sharing files between the container and host. To share files between two containers, run another container named
myubi2.Run the container named
myubi2and map the directory defined by thehostvolumevolume name on the host to the/containervolume2directory on the container:$ podman run -it --name myubi2 -v hostvolume:/containervolume2 registry.access.redhat.com/ubi10/ubi /bin/bashList the files in the shared volume on the container:
# ls /containervolume2 container1.txt host.txtYou can see the
host.txtfile which you created on the host andcontainer1.txtwhich you created inside themyubi1container.Create a text file inside the
/containervolume2directory:# echo "Hello from container 2" >> /containervolume2/container2.txt-
Detach from the container with
CTRL+pandCTRL+q. List the files in the shared volume on the host, you should see three files:
$ ls $mntPoint container1.rxt container2.txt host.txtFor more information, see the
podman-volume(1)man page on your system.