Difficulty: intermediate
Estimated Time: 15-20 minutes

One of the properties of container images is that they are immutable. That is, although you can make changes to the local container filesystem of a running image, the changes are not permanent. When that container is stopped, any changes are discarded. When a new container is started from the same container image, it reverts back to what was originally built into the image.

Although any changes to the local container filesystem are discarded when the container is stopped, it can sometimes be convenient to be able to upload files into a running container. One example of where this might be done is during development and a dynamic scripting language is being used. By being able to modify code in the container, you can modify the application to test changes before rebuilding the image.

In addition to uploading files into a running container, you might also want to be able to download files. During development these may be data files or log files created by the application.

In this course you will learn how to transfer files between your local machine and a running container. You will also learn how you can set up live synchronisation to enable you to make changes to code on your local machine and have the result immediately used by the application running in the container without you needing to manually copy them.

You will be using just the oc command line tool in this exercise.

In this course you learnt about oc commands you can use to transfer files to and from a running container, as well as how to set up live synchronization so changes are automatically copied from a local machine into a running container.

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 rsync <pod-name>:/remote/dir/filename ./local/dir: Copy a single file from the pod to the local directory.

oc rsync <pod-name>:/remote/dir ./local/dir: Copy the directory from the pod to the local directory.

oc rsync <pod-name>:/remote/dir/. ./local/dir: Copy the contents of the directory from the pod to the local directory.

oc rsync <pod-name>:/remote/dir ./local/dir --delete: Copy the contents of the directory from the pod to the local directory. The --delete option ensures that the resulting directories will match exactly, with directories/files in the local directory which are not found in the pod being deleted.

oc rsync ./local/dir <pod-name>:/remote/dir --no-perms: Copy the directory to the remote directory in the pod. The --no-perms option ensures that no attempt is made to transfer permissions, which can fail if remote directories are not owned by user container runs as.

oc rsync ./local/dir <pod-name>:/remote/dir --exclude=* --include=<filename> --no-perms: Copy the single file to the remote directory in the pod. The --no-perms option ensures that no attempt is made to transfer permissions, which can fail if remote directories are not owned by user container runs as.

oc rsync ./local/dir <pod-name>:/remote/dir --no-perms --watch: Copy the directory to the remote directory in the pod and then keep the remote directory syncrhonized with the local directory, with any changed files automatically being copied to the pod. The --no-perms option ensures that no attempt is made to transfer permissions, which can fail if remote directories are not owned by user container runs as. The --watch option is what enables the synchronization mechanism.

oc run dummy --image centos/httpd-24-centos7: Run a dummy application pod which 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 a dummy application pod at the directory /mnt so 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 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.

Don’t stop now! The next scenario will only take about 10 minutes to complete.

Transferring Files in and out of Containers

Step 1 of 5

Topic 1 - Creating an Initial Project

Before we get started, you need to login and create a project in OpenShift to work in.

To login to the OpenShift cluster used for this course from the Terminal, run:

oc login -u developer -p developer

This will log you in using the credentials:

  • Username: developer
  • Password: developer

You should see the output:

Login successful.

You don't have any projects. You can try to create a new project, by running

    oc new-project <projectname>

To create a new project called myproject run the command:

oc new-project myproject

You should see output similar to:

Now using project "myproject" on server "https://172.17.0.41:8443".

You can add applications to this project with the 'new-app' command. For example, try:

    oc new-app centos/ruby-22-centos7~https://github.com/openshift/ruby-ex.git

to build a new example application in Ruby.

We are not going to use the web console for this course, but if you want to check anything from the web console, switch to the Dashboard and use the same credentials to login as you used above to login from the command line.