Use storage with Podman containers
Introduction
This lab shows how to use different container storage types to access the host filesystem or persist files. This tutorial is targeted at users of Oracle Linux 8 or later.
Objectives
In this lab, you'll run several exercises that:
- Use bind mounts to access host level resources
- Use volumes to persist data
What Do You Need?
- A system with Oracle Linux installed
Use a Bind Mount for Webserver Data Storage
Note: When using the free lab environment, see Oracle Linux Lab Basics for connection and other usage instructions.
This example demonstrates using a bind mount as the document root for a containerized Python HTTP server.
Open a terminal and make a directory.
sudo mkdir /opt/data; sudo chown opc. /opt/data
sudo
is necessary as elevated privileges are required to write to the/opt
directory.- Using
chown
changes ownership of the directory to theopc
user and group and ensures read and write access to the directory created.
Create a
Dockerfile
.echo "FROM os/oraclelinux:8 WORKDIR /opt RUN dnf -y module install python38 && \ dnf clean all ENTRYPOINT /bin/python3 -m http.server 8000" > Dockerfile
cat ./Dockerfile
FROM
pulls theoraclelinux:8
image.WORKDIR
sets the working directory when the container runs.RUN
executes the command in a shell.ENTRYPOINT
configures the container to run the Simple Python HTTP server.
Build the image.
podman build --tag oraclelinux:pyhttp .
--tag
specifies the name of the resulting image if the build process completes successfully.- If imageName does not include a registry name, the registry name localhost prepends to the image name.
Show the new image.
podman images
Start a container based on the new image.
podman run -d -p 8080:8000 --name webapp1 -v /opt/data:/opt oraclelinux:pyhttp
-d
starts the container as a daemon process.-p
creates a port forward from 8080 on the host to 8000 in the container.--name
option assigns the namewebapp1
to the container.-v
maps the bind mount/opt/data/
on the host to/opt
in the container.
Verify the container is running.
podman ps -a
The container shows a status of UP.
Show the
/opt/data
directory is empty on the host system.ll /opt/data
Use
curl
to show the/opt
directory within the container is empty.curl localhost:8080
This command works because the container's
/opt
directory is a bind mount to the host system's/opt/data
directory.The HTTP Server's root directory uses
/opt
with the working directory set where the server starts.Add files to the host system's
/opt/data
directory.for i in {1..10}; do touch /opt/data/file${i}; done
Using the script creates 10 empty files.
Verify the script created the files successfully.
ll /opt/data
Verify that the HTTP server within the container also sees the newly created files.
curl localhost:8080
The steps show the successful use of a bind mount to allow reading and writing to the host from within a container. Any data written to the host persists after a container stops or gets removed.
Stop and remove the containers.
podman ps -a podman stop <CONTAINER_NAME> podman rm <CONTAINER_NAME>
Using Volumes with Containers
A volume is a storage device created and managed by Podman. Volumes are created directly using the podman volume
command or during container creation.
Create a volume using
podman volume
.podman volume create my_vol
List volumes.
podman volume ls
Remove a volume.
podman volume rm my_vol
Start a container and create a volume attached to it.
podman run -it -v my_data:/data --name box1 oraclelinux:8
The container starts an interactive shell and presents a prompt.
-v
creates the volumemy_data
and mounts/data
within the container.
An anonymous volume gets created without passing the name
my_data
to the volume. An anonymous volume does not have a reference name and is identified only by its unique id.Get a listing of files in
/data
.ls -l /data
Create a test file in the volume and verify it exists.
touch /data/sample.txt ls -l /data
Leave and exit the container.
exit
Show the container has stopped.
podman ps -a
Inspect the container and get a list of volumes used.
podman inspect -f {% raw %}'{{.Mounts}}'{% endraw %} box1
-f
formats the output and shows only the container volume details.
Restart the container, and check if the file still exists.
podman restart box1 podman exec box1 ls -l /data
The command
exec
runs the requested command against the restarted container.Stop and then remove the container.
podman stop box1 podman rm box1
Check the volume still exists.
podman ps -a podman volume ls
podman ps -a
shows the container is removed, whilepodman volume ls
shows the volume remains.Mount the existing volume to a new container.
podman run -it --mount 'type=volume,src=my_data,dst=/data2' --name box2 oraclelinux:8
--mount
: takes the following key-value pairs when mounting an existing volume.type
: the type of storage being mountedsrc
: the name or unique id of a volumedst
: the mount point within the container
The container mount point
/data2
shows that the new container mount point does not need to match the original mount point.The data in the volume persists.
ls -l /data2
Leave the container.
exit
Remove the container and all unused volume storage.
podman rm -v box2 podman ps -a podman volume ls podman volume prune podman volume ls
The
podman volume prune
removes all volumes not used by at least one container. If you only want to remove a single volume, usepodman volume rm <VOLUME_NAME>
.
For More Information
See other related resources: