Note:

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:

What Do You Need?

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.

  1. 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 the opc user and group and ensures read and write access to the directory created.
  2. 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 the oraclelinux: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.
  3. 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.
  4. Show the new image.

    podman images
    
  5. 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 name webapp1 to the container.
    • -v maps the bind mount /opt/data/ on the host to /opt in the container.
  6. Verify the container is running.

    podman ps -a
    

    The container shows a status of UP.

  7. Show the /opt/data directory is empty on the host system.

    ll /opt/data
    
  8. 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.

  9. 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.

  10. Verify the script created the files successfully.

    ll /opt/data
    
  11. 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.

  12. 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.

  1. Create a volume using podman volume.

    podman volume create my_vol
    
  2. List volumes.

    podman volume ls
    
  3. Remove a volume.

    podman volume rm my_vol
    
  4. 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 volume my_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.

  5. Get a listing of files in /data.

    ls -l /data
    
  6. Create a test file in the volume and verify it exists.

    touch /data/sample.txt
    ls -l /data
    
  7. Leave and exit the container.

    exit
    
  8. Show the container has stopped.

    podman ps -a
    
  9. Inspect the container and get a list of volumes used.

    podman inspect -f '{{.Mounts}}' box1
    
    • -f formats the output and shows only the container volume details.
  10. 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.

  11. Stop and then remove the container.

    podman stop box1
    podman rm box1
    
  12. Check the volume still exists.

    podman ps -a
    podman volume ls
    

    podman ps -a shows the container is removed, while podman volume ls shows the volume remains.

  13. 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 mounted
    • src: the name or unique id of a volume
    • dst: 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.

  14. The data in the volume persists.

    ls -l /data2
    
  15. Leave the container.

    exit
    
  16. 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, use podman volume rm <VOLUME_NAME>.

For More Information

See other related resources:

More Learning Resources

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.