4  Using Volumes

This chapter will demonstrate how to: Recognize the purpose of volumes Mount a volume to a Docker container

In the last chapter we saw that we were using a container but were unable to access files we needed. We were trying to call the run_analysis.sh script but the isolation of containers that makes them useful also means that we didn’t have this script on our container.

So how do we get files we need onto a container we are using?

There are a few options:

What is a volume? A volume is a folder, likely from your computer, that can be accessed by your container.

In our growing diagram we can now see that attached to a container which is An environment we can run stuff in we now have a volume which is a Folder that can be accessed by the container

I think of volumes like portals:

via GIPHY

The portal/volume can be opened when you RUN the container. And when you stop and delete the container the portal is no longer there. But its a way that your container can access and modify files on your computer for the time being.

4.1 Activity Instructions

4.1.1 Docker

Our container is separate from our computer so if we want to use a file from our computer we have to attach it using a “volume”.

4.1.1.0.1 Step 1: Let’s add our containers-for-scientists-sandbox files

Let’s point a volume to our workshop files so we have them on our container.

We can specify a particular file path on our computer or give it $PWD. Then optionally we can give a : and a file path where we’d like it to be stored on the container. Otherwise it will be stored at the absolute top of the container. Note that $PWD is a special environment variable that stores the absolute path of the current working directory. You will need to be in the containers-for-scientists-sandbox-main for this to work.

This diagram shows a docker run command where a volume is specified. -v is used to specify the volume which has two pieces to the option. First is the Folder on your computer with files you want accessible then a colon is placed and on the other side of this colon a file path that indicates Where these files should show up on your container After this option the image name is given as is typical for docker run.

Now we can run:

docker run -v $PWD:/home cansav09/practice-image:1

If you have a windows machine you may have to run this variant instead. This version has a different ${} around the pwd part.

docker run -v ${pwd}:/home cansav09/practice-image:1

In Docker desktop you can specify a portal like this:

4.1.1.1 Step 2: Retry calling the script

Now we can run the following command but we will have to run docker ps and get the container ID we need to put here.

docker exec -it <REPLACE_WITH_CONTAINER_ID> bash /home/run_analysis.sh

or in the exec tab of the container in Docker desktop app, run

bash /home/run_analysis.sh

Now we have a new error. Error in loadnamespace(x) there is no package called rmarkdown. What do we think this means?

Now we have a new error! What does this mean?

Question: Does our container have all of the same software that our computer has?

Does our container have all of the same software that our computer has?

4.1.2 Podman

Our container is separate from our computer so if we want to use a file we have to attach it using a “volume”.

4.1.2.0.1 Step 1: Let’s add our containers-for-scientists-sandbox files

Let’s point a volume to our workshop files so we have them on our container.

We can specify a particular file path on our computer or give it $PWD Then optionally we can give a : and a file path we’d like this to be stored on on the container. Otherwise it will be stored at the absolute top of the container.

Now we can run:

podman run -v $pwd:/home cansav09/practice-image:1

If you have a windows machine you may have to run this variant instead. This version has a different ${} around the pwd part.

podman run -v ${pwd}:/home cansav09/practice-image:1

This diagram shows a podman run command where a volume is specified. -v is used to specify the volume which has two pieces to the option. First is the Folder on your computer with files you want accessible then a colon is placed and on the other side of this colon a file path that indicates Where these files should show up on your container. After this option the image name is given as is typical for docker run.

4.1.2.1 Step 2: Retry calling the script

Now we can run the following command but we will have to run podman ps and get the container ID we need to put here.

podman exec -it <REPLACE_WITH_CONTAINER_ID> bash /home/run_analysis.sh

Now we have a new error:

Error in loadNamespace(x): There is no package called 'rmarkdown'

What does this mean?

Question: Does our container have all of the same software that our computer has?

Does our container have all of the same software that our computer has?