This is part three of a three-part series. In this post, we’ll cover copying files into a new persistent volume. Part one covered manually copying files into and out of a container. Part two went into live synchronization.

Part Three: Copying Files to a New Persistent Volume

If you are mounting a persistent volume into the container for your application and you need to copy files into it, then oc rsync can be used in the same way as described previously to upload files. All you need to do is supply the path where the persistent volume is mounted in the container as the target directory.

If you haven't yet deployed your application, but you're wanting to prepare a persistent volume with all the data it needs to contain in advance, you can still claim a persistent volume and upload the data to it. In order to do this, you'll need to deploy a dummy application to mount the persistent volume against.

To create a dummy application for this purpose, run this command:

oc run dummy --image centos/httpd-24-centos7

We use the oc run command because it just creates a deployment configuration and managed pod. A service is not created, as we don't need the application we're running here (an instance of the Apache HTTPD server in this case) to actually be contactable. We're using the Apache HTTPD server purely as a means of keeping the pod running.

To monitor the startup of the pod and ensure it's deployed, run:

oc rollout status dc/dummy

Once it's running, you can see that a more limited set of resources is created, compared to what would be created when using oc new-app. View this by running:

oc get all --selector run=dummy -o name

Now that we have a running application, we next need to claim a persistent volume and mount it against our dummy application. When doing this, we assign it a claim name of data so that we can refer to the claim by a set name later on. We mount the persistent volume at /mnt inside of the container, which is the traditional directory used in Linux systems for temporarily mounting a volume.

oc set volume dc/dummy --add --name=tmp-mount --claim-name=data --type pvc --claim-size=1G --mount-path /mnt

This will cause a new deployment of our dummy application, this time with the persistent volume mounted. Again, monitor the progress of the deployment so we know when it's complete, by running:

oc rollout status dc/dummy

To confirm that the persistent volume claim was successful, you can run:

oc get pvc

With the dummy application now running, and with the persistent volume mounted, find the name of the pod for the running application with the following command:

oc get pods --selector run=dummy

This will return something like this, with your unique pod name that you’ll need to use in the following commands:

NAME            READY       STATUS      RESTARTS        AGE
dummy-1-9j3p3 1/1 Running 0 1m

We can now copy any files into the persistent volume, using the /mnt directory (where we mounted the persistent volume) as the target. In this case, since we're doing a one off copy, we can use the tar strategy instead of the rsync strategy.

oc rsync ./ dummy-1-9j3p3:/mnt --strategy=tar

When complete, you can validate that the files were transferred by listing the contents of the target directory inside of the container.

oc rsh dummy-1-9j3p3 ls -las /mnt

If you're done with this persistent volume and perhaps needed to repeat the process with another persistent volume but with different data, you can unmount the persistent volume but retain the dummy application.

oc set volume dc/dummy --remove --name=tmp-mount

Monitor the process once again to confirm that the re-deployment has completed.

oc rollout status dc/dummy

Look up the name of the current pod again:

oc get pods --selector run=dummy

Look again at what is in the target directory. It should be empty at this point. This is because the persistent volume is no longer mounted, and you're looking at the directory within the local container file system.

oc rsh dummy-1-9j3p3 ls -las /mnt

If you already have an existing persistent volume claim, as we now do, you could mount the existing claimed volume against the dummy application instead. This is different than above, where we both claimed a new persistent volume and mounted it to the application at the same time.

oc set volume dc/dummy --add --name=tmp-mount --claim-name=data --mount-path /mnt

Look for completion of the re-deployment:

oc rollout status dc/dummy

Again, get the name of the current pod:

oc get pods --selector run=dummy

Check the contents of the target directory. The files we copied to the persistent volume should again be visible.

oc rsh dummy-1-9j3p3 ls -las /mnt

When you're done and want to delete the dummy application, use oc delete to delete it, using a label selector of run=dummy to ensure we only delete the resource objects related to the dummy application.

oc delete all --selector run=dummy

Check that all the resource objects have been deleted:

oc get all --selector run=dummy -o name

Although we've deleted the dummy application, the persistent volume claim still exists, and can later be mounted against the actual application to which the data belongs.

oc get pvc

Congratulations! That’s it for this series on transferring files!

In this post, you've learned about oc commands that you can use to copy files into a persistent volume.

You can find a summary of the key commands covered below. To see more information on each oc command, run it with the --help option.

  • oc run dummy --image centos/httpd-24-centos7: Run a dummy application pod that can be used to mount persistent volumes to facilitate copying files to a persistent volume.
  • oc set volume dc/dummy --add --name=tmp-mount --claim-name=<claim-name> --type pvc --claim-size=1G --mount-path /mnt: Claim a persistent volume and mount it against the dummy application pod at the directory /mnt so that files can be copied into the persistent volume using oc rsync.

  • oc set volume dc/dummy --add --name=tmp-mount --claim-name=<claim-name> --mount-path /mnt: Mount an existing persistent volume against a dummy application pod at the directory /mnt so that files can be copied into the persistent volume using oc rsync.

  • oc rsync ./local/dir <pod-name>:/remote/dir --strategy=tar: Copy the directory to the remote directory in the pod. The --strategy=tar option indicates to use tar to copy the files rather than rsync.

Would You Like to Learn More?

This post is based on one of OpenShift’s interactive learning scenarios. To try it and our other tutorials without needing to install OpenShift, visit

Do you have an OpenShift Online account? There's no reason to wait. Get your applications running in minutes with no installation needed. Sign up for the free trial of OpenShift Online.

What other topics would you like to see in the future on this blog? We're happy to make tutorials about anything that helps you with your OpenShift experience. Comment and let us know!


OpenShift Container Platform

< Back to the blog