Hello, and welcome to today's video presentation on using Bind Mounts with Podman on Oracle
Linux 8.
This video provides a few demos covering different ways to use bind mounts for podman container
storage.
So let us get started.
Start by opening a terminal and connect to your host system.
In this exercise, the host system is a fully patched Oracle Linux 8 instance on Oracle
Cloud Infrastructure.
This instance also has the podman package pre-installed.
If you need instruction on installing or running Podman, see our other videos on this topic
at oracle.com/goto/oraclelinuxlearning under Virtualization and Containers.
A bind mount is simply a file or directory on the host machine mounted into the container.
One way to leverage a bind mount is to provide access to commands or applications that do
not exist within the minimal design of a specified container image.
For example, try running the ip command within the Oracle Linux 8-slim image by typing "podman
run --rm -ti oraclelinux:8-slim /usr/sbin/ip a."
The command fails and displays an error message indicating that the requested file or directory
does not exist.
To help resolve the error, use a bind mount when running the container.
Try again by typing the command "podman run -v /usr/sbin:/usr/sbin --rm -ti oraclelinux:8-slim
/usr/sbin/ip a."
The -v option takes the parameters source and destination separated by a colon.
The source is an existing location on the host machine and referenced by its full path.
The destination is a location created on-demand within the container if not already present
and mapped to the source.
Hit Enter.
The command fails again with a different message displaying an error while loading shared libraries.
The new error indicates the container is missing the library listed in the error message.
The library mentioned is located in the /usr/lib and /usr/lib64 directories on the host machine.
As the 8-slim image is a 64-bit Linux operating system, try rerunning the podman command while
adding the /usr/lib64 mount point to the container.
Type "podman run -v /usr/sbin:/usr/sbin -v /usr/lib64:/usr/lib64 --rm -ti oraclelinux:8-slim
/usr/bin/ip a."
The command executes successfully and displays the loopback and the ethernet adapter for
the container.
Another example of using a blind mount could be writing log files to the host machine or
accessing host-level content with a web application.
To demonstrate this example, make a directory by typing "sudo mkdir /opt/data; sudo chown
oracle.
/opt/data."
The sudo command is necessary as elevated privileges are needed to write to the /opt
directory.
The chown command changes ownership of the data directory created to the oracle user_id
and group_id to ensure our oracle user can write to the directory created.
As this example needs python installed, start a container to install python3 by typing "podman
run oraclelinux:8 dnf install python3 -y."
Once python3 completes the install, Run "podman ps -a" and copy that container's id.
Using the container_id, create a new image based on the modified container with python
installed.
This step is necessary as Podman does not allow adding ports or volumes to an existing
container.
Complete this step by typing "podman commit old_container_id new_image_name," which I
called ol8-python.
Now start a new container using the ol8-python image by typing the command on screen.
[ silence ... podman run -d -p 8080:8000 --name webapp1 -w /opt -v /opt/data:/opt ol8-python
/bin/python3 -m http.server 8000]
The -p will create a port forward from 8080 on the host to 8000 within the container.
The container will get the name webapp1.
The -w designates the working directory and where to start the command execution.
The -v maps the host directory /opt/data to the containers /opt directory.
The "/bin/python3 -m http.server 8000" starts a python program that creates a simple web
server listening on port 8000.
The web server serves contents from its startup directory, which in this case is /opt.
Verify the container is running using "podman ps."
The output shows the container in a running status along with the port forwards.
On the host machine, run the command "ll /opt/data" to show the empty local directory.
Now use "curl localhost:8080" to show the same within the container /opt directory via
the webserver.
Populate the host machine folder with files using the script shone on screen.
[silence ... for i in {1..10}; do touch /opt/data/file${i}; done]
View the contents of the host machine folder again using "ll /opt/data."
The script created ten files in the directory.
Repeat the same test using curl to check the container's directory.
The files exist there as well.
That completes this demonstration of using bind mounts with Podman Containers.
In this video, we showed two methods to leverage bind mounts.
First, we provide access to host machine programs and libraries that get used within the container.
In the second, we pass local storage, which can be written to or read by the container.
Thank you for watching this video and enjoy using Oracle Linux 8.
Please check our additional information on Oracle Linux 8 in the Oracle Linux 8 Documentation
or Oracle Linux Learning Library.