Share files between two containers
You can use volumes to persist data in containers even after the container is deleted. A volume is a folder stored on the host machine that does not increase the container size and can be shared among multiple containers, making it easier to back up or migrate data.
Before you begin
- The
container-toolsmeta-package is installed.
Procedure
-
Create a volume:
$ podman volume create hostvolume -
Display 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.txt -
List all files in the directory defined by the
mntPointvariable:$ ls $mntPoint/host.txt -
Run 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 /containervolume1host.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 can see two files:
$ ls $mntPointcontainer1.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/bash -
List the files in the shared volume on the container:
# ls /containervolume2container1.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 $mntPointcontainer1.rxt container2.txt host.txtFor more information, see the
podman-volume(1)man page on your system.