3  Using Containers

This chapter will demonstrate how to: Recognize the practical definitions of what an image and a container are Pull an image Run a container Identify how containers could be used

Let’s actually dive into using containers. You can follow along with this activity with either Docker or Podman. We recommend using Docker but if for admin reasons you are unable to use Docker, Podman’s commands work nearly identically to Docker.

3.0.1 A note about “window juggling”

When learning another software skill like containers, it sometimes means you’ll have to keep track of more than one window. We call this “Window Juggling”. It’s an overlooked skill but something you’ll become more comfortable with.

IMPORTANT: Use the checkboxes to follow along with each step in the activities! This is to help you keep track because the steps have to be done in order!

3.1 Activity Instructions

3.1.1 Docker

3.1.1.1 Set up

If not already installed, go here to install Docker, following the instructions for your particular operating system.
If you don’t have a Docker account create an account when prompted, or go here.
Start up Docker desktop by double clicking on the app. It may take some time to start up.

First you need to install docker by going to https://docs.docker.com/engine/install/

3.1.1.2 Get workshop files

Download the files for this activity clicking here: https://github.com/fhdsl/containers-for-scientists-sandbox/archive/refs/heads/main.zip
Put this file on your desktop so it is easily findable.
Double click the zip file (or right click and choose “unzip” or “decompress” to unzip the file.

Download a zip file of the files for this workshop https://github.com/fhdsl/containers-for-scientists-sandbox and clicking the download zip file button

3.1.1.3 Activity 1: Pull and run a Docker image

This activity is built so that you will encounter errors that this guide will work you through! One of the most common container stumbling blocks is understanding the idea that containers are isolated from your computer.

Most Common Stumbling Block: Containers are isolated from your computer If your computer has: Files Software Or anything else Your container does NOT have it

Which means: If your computer has files, software, or anything else. Your container by default does NOT have it unless you do something to get those files and software on there.

We are going to encounter these stumbling blocks look out. The image shows huge cartoon eyes.

We will encounter both an error when we don’t have a file we need AND an error when we don’t have a software package we need. We will guide you through what to do when these expected errors occur.

3.1.1.4 Step 1: Docker pull

We need to get the Docker image we want to use. We do this by “pulling” it. Pulling is a term used in GitHub terminology too. It just means we’re taking something from the internet and getting a copy locally (on our computer). Pulling is basically like downloading.

docker pull - we can borrow an image from online where an image. In this image the internet is represented by a cloud where docker pull is an arrow pointing down from the cloud. is A snapshot of an environment that’s easily shareable

Run this command in your Terminal or Command Prompt window:

docker pull cansav09/practice-image:1

When pulling the image you should see some progress bars and Download complete messages from your command line.

3.1.1.4.1 Step 2: Check what images you have

To see what images we have we can run docker image ls.
Alternatively, you can visit the Images tab of the docker desktop app.
We should see cansav09/practice-image:1 show up in the output.

We can look at what images we have by going to the Images tab.

3.1.1.4.2 Step 3: Use the Run command to start a container from the image!

Remember images can be shared easily but to actually use the stuff we need to turn our image into a container! To do that we will use the docker run command

A similar image to before but now we are showing that docker run turns images into containers. Where an image is A snapshot of an environment that’s easily shareable and a container is An environment we can run stuff in

To launch the image for use, we can use docker run to turn it into a container.

docker run cansav09/practice-image:1

Alternatively, we can press the play button for the image in images tab of the desktop app.

To see what containers we have running we have we can run (in a new terminal session):

docker ps

Alternatively, you can visit the Containers tab of the Docker Desktop app.

A similar image to before but now we are showing that docker run turns images into containers. Where an image is A snapshot of an environment that’s easily shareable and a container is An environment we can run stuff in

Pat yourself on the back! You have a running container!

Yay! You are running a Docker container… But now what?

What can we do with running containers?

Examples of ways to use your Docker image/container: Run a single command Run a script Run with a port to a browser where you can work interactively (only some images) Have a GitHub Action or other app run on a container! AND MORE!

3.1.1.4.3 Step 4: Run a single command

To run stuff interactively from the command line we can do:

docker exec -it <PUT_CONTAINER_ID_HERE> bash

Running and IDEs from a Docker container is a great option. here we are showing examples of how RStudio or Jupyter can be run from a container using a browser and a container

You can find the CONTAINER ID in the containers tab of the desktop app or by using the docker container ls command in the terminal. Note that whereas the docker run command creates a new container from a docker image, the docker exec command executes a command inside an already running container. The -it option tells docker to execute the command interactively. In this case the actual command being specified is bash. Altogether the result is an interactive Bash shell inside the already running container.

If you run the above command you should see your terminal/command prompt now have the initial part change to something like:

root@e7b19cfb4461:/

Where you’ll noticed the CONTAINER ID is now shown there.

You are now using command line inside of your container. Trying running any bash command like ls to see what files are here for example.

Your screen will end up looking like this (but this printout is from a Mac, windows will look a little different in some parts):

(base) firstnamelastname@FirstNames-MacBook-Pro ~ % docker exec -it e7b19cfb4461 bash

root@e7b19cfb4461:/# ls
bin   etc   lib    libexec  mnt   rocker_scripts  sbin  tmp
boot  home  lib32  libx32   opt   root            srv   usr
dev   init  lib64  media    proc  run             sys   var
root@e7b19cfb4461:/#

When you are ready to leave your container type exit and press Enter.

3.1.1.4.4 Step 5: Try calling a script

Make sure you are in the unzipped workshop directory. To run a script using the docker container we could just add reference to a script at the end.

cd containers-for-scientists-sandbox-main
docker exec -it <PUT_CONTAINER_ID_HERE> bash run_analysis.sh

We can interact with our container by going to the exec tab and calling commands from here.

BUT! You will find that this command won’t work yet though, why?

bash: run_analysis.sh: No such file or directory

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

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

This won’t work because the file run_analysis.sh is not a file that our container has. Docker containers do not have all the files that our computer does; they only have the files we add to it.

3.1.2 Podman

3.1.2.1 Set up Podman

Go here to install Podman, following the instructions for your particular operating system.
Open up your command line.
Start up Podman by running the following:

podman machine init
podman machine start

3.1.2.2 Get workshop files

Download the files for this activity clicking here: https://github.com/fhdsl/containers-for-scientists-sandbox/archive/refs/heads/main.zip
Put this file on your desktop so it is easily findable.
Double click the zip file (or right click and choose “unzip” or “decompress” to unzip the file.

Download a zip file of the files for this workshop https://github.com/fhdsl/containers-for-scientists-sandbox

This activity is built so that you will encounter errors that this guide will work you through! Most common container stumbling block is understanding the idea that containers are isolated from your computer

Most Common Stumbling Block: Containers are isolated from your computer If your computer has: Files Software Or anything else Your container does NOT have it

Which means: If your computer has files, software, or anything else. Your container by default does NOT have it unless you do something to get those files and software on there.

We are going to encounter these stumbling blocks look out. The image shows huge cartoon eyes.

We will encounter both an error of when we don’t have a file we need AND an error of when we don’t have a software package we need. We will guide you through what to do when these expected errors occur.

3.1.2.2.1 Step 1: Podman pull

We need to get the image we want to use. We do this by “pulling” it. Pulling is a term used in GitHub terminology too. It just means we’re taking something from the internet and getting a copy locally (on our computer).

docker pull - we can borrow an image from online where an image. In this image the internet is represented by a cloud where docker pull is an arrow pointing down from the cloud. is A snapshot of an environment that’s easily shareable

Run this command in your Terminal or Command Prompt window:

podman pull cansav09/practice-image:1

If this command is running properly you should see some output that looks like this:

As podman pulls the image you will see progress messages in your command line console

3.1.2.2.2 Step 2: Check what images you have

To see what images we have we can run podman image ls.
You should see cansav09/practice-image:1 show up in the output and it will look a bit like this below:

docker.io/cansav09/practice-image  1   91619ca583b8  1 month ago  2.39 GB
3.1.2.2.3 Step 3: Run the image to start a container!

Remember images can be shared easily but to actually use the stuff we need to turn our image into a container! To do that we will do podman run

A similar image to before but now we are showing that docker run turns images into containers. Where an image is A snapshot of an environment that’s easily shareable and a container is An environment we can run stuff in

To launch the image for use, we can use podman run to turn it into a container.

podman run cansav09/practice-image:1

If this command has run properly it will return something like this:

When we run the container we’ll see a message like this. That ends in done.

Don’t worry too much about all this output, every image is configured differently and this output will change depending on that.

To see what containers we have running we have we can run:

podman ps

Your output will look a little like this. NOTE though that the CONTAINER ID and NAMES are randomly generated for each container and will change, so that part won’t be the same!

CONTAINER ID  IMAGE                                COMMAND     CREATED        STATUS        PORTS       NAMES
e7b19cfb4461  docker.io/cansav09/practice-image:1  /init       2 minutes ago  Up 2 minutes  8787/tcp    hopeful_proskuriakova

Pat yourself on the back! You have a running container!

Yay! You are running a Docker container… But now what?

What can we do with running containers?

Examples of ways to use your Docker image/container: Run a single command Run a script Run with a port to a browser where you can work interactively (only some images) Have a GitHub Action or other app run on a container! AND MORE!

3.1.2.2.4 Step 4: Run the container interactively

To run stuff interactively from the command line we can do:

But you’ll need to replace the <PUT_CONTAINER_ID_HERE> portion of this command with the CONTAINER ID reported when you ran podman ps.

podman exec -it <PUT_CONTAINER_ID_HERE> bash

If you run this you should see your terminal/command prompt now have the initial part change to something like:

root@e7b19cfb4461:/

Where you’ll noticed the CONTAINER ID is now shown there.

You are now using command line inside of your container. Trying running any bash command like ls to see what files are here for example.

Your screen will end up looking like this:

(base) firstnamelastname@FirstNames-MacBook-Pro ~ % podman exec -it e7b19cfb4461 bash

root@e7b19cfb4461:/# ls
bin   etc   lib    libexec  mnt   rocker_scripts  sbin  tmp
boot  home  lib32  libx32   opt   root            srv   usr
dev   init  lib64  media    proc  run             sys   var
root@e7b19cfb4461:/#

When you are ready to leave your container type exit and press Enter.

3.1.2.2.5 Step 5: Try calling a script

To run a script using the container we could just add reference to a script at the end:

podman exec -it <PUT_CONTAINER_ID_HERE> bash run_analysis.sh

BUT! You will find that this command won’t work yet though, why?

This won’t work because the file run_analysis.sh is not a file that our container has. Docker containers do not have all the files that our computer does; they only have the files we add to it.

bash: run_analysis.sh: No such file or directory

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

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

This won’t work because the file run_analysis.sh is not a file that our container has. Docker containers do not have all the files that our computer does; they only have the files we add to it.