Note:
- This tutorial is available in an Oracle-provided free lab environment.
- It uses example values for Oracle Cloud Infrastructure credentials, tenancy, and compartments. When completing your lab, substitute these values with ones specific to your cloud environment.
Use storage with Podman containers
Introduction
This lab shows how to use different container storage types to access the host filesystem, or persist files.
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 8 installed
(Hands-on Lab) Connect to the Compute Instance
Note: This step is specific to the Oracle provided free lab environment.
To access the lab compute instance, first connect to the Oracle Cloud Infrastructure (OCI) Console and copy the compute instance Public IP address.
- Sign in to OCI Console, and select your Compartment.
- Click Instances.
-
Copy the Public IP to a temporary location (such as a text file) on your computer.
To copy, highlight the IP address with the mouse and press Ctrl+C.
- Right-click the Virtual Desktop and select Open Terminal Here.
-
Connect to the instance.
ssh -i ../.ssh/id_rsa opc@<IP_ADDRESS_OF_COMPUTE_INSTANCE>
Where
<IP_ADDRESS_OF_COMPUTE_INSTANCE>
is the IP address copied from the OCI Console. -
Accept the ECDSA key fingerprint.
You are now connected to the compute instance for this lab.
Use a Bind Mount for Webserver Data Storage
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
The use of
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
The
FROM
pulls theoraclelinux:8
image.The
WORKDIR
sets the working directory when the container runs.The
RUN
executes the command in a shell.The
ENTRYPOINT
configures the container to run the Simple Python HTTP server. -
Build the image.
podman build --tag oraclelinux:pyhttp .
--tag
specifies the name which will be assigned to the resulting image if the build process completes successfully.If imageName does not include a registry name, the registry name localhost will be prepended 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
The
-d
starts the container as a daemon process.The
-p
creates a port forward from 8080 on the host to 8000 in the container.The
--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.
-
On the host system, show that the
/opt/data
directory is empty.ll /opt/data
-
Show the
/opt
directory within the container empty by usingcurl
.curl localhost:8080
This works because the
/opt
directoy within the container is a bind mount to/opt/data
on the host system.The HTTP server root directory is set to
/opt
as that is with working diretory where the server was started. -
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 cretaed 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.
The
-v
creates the volumemy_data
and mounts within the container at/data
.If the name
my_data
was not passed to the volume, an anonymous volume gets created. An anonymous volume does not have a name for reference, 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 '' box1
The
-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
Selected the container mount point
/data2
to show the new containers mount point does not need to match the original container 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>
.
Learn More
Explore other labs on docs.oracle.com/learn or access more free learning content on the Oracle Learning YouTube channel. Additionally, visit education.oracle.com/learning-explorer to become an Oracle Learning Explorer.
For product documentation, visit Oracle Help Center.
Use storage with Podman containers
F37667-02
February 2021
Copyright © 2021, Oracle and/or its affiliates.
This software and related documentation are provided under a license agreement containing restrictions on use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this software, unless required by law for interoperability, is prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.
If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on behalf of the U.S. Government, then the following notice is applicable:
U.S. GOVERNMENT END USERS: Oracle programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, delivered to U.S. Government end users are "commercial computer software" pursuant to the applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, use, duplication, disclosure, modification, and adaptation of the programs, including any operating system, integrated software, any programs installed on the hardware, and/or documentation, shall be subject to license terms and license restrictions applicable to the programs. No other rights are granted to the U.S. Government.
This software or hardware is developed for general use in a variety of information management applications. It is not developed or intended for use in any inherently dangerous applications, including applications that may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this software or hardware in dangerous applications.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Intel and Intel Xeon are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Opteron, the AMD logo, and the AMD Opteron logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered trademark of The Open Group.
This software or hardware and documentation may provide access to or information about content, products, and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly disclaim all warranties of any kind with respect to third-party content, products, and services unless otherwise set forth in an applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be responsible for any loss, costs, or damages incurred due to your access to or use of third-party content, products, or services, except as set forth in an applicable agreement between you and Oracle.