CS 133: Running Ant via Docker

These instructions will walk you through setting up a Docker container to run unit tests for the CS 133 SimpleDb labs if you don't have Ant installed on your computer. Before starting, you should have Docker Desktop and Visual Studio Code installed.

Overview

Note that there are several ways you could use Docker for this task. The approach in this guide will use Docker to simply compile your code and run unit tests using Ant; all your source code files will live on your computer in a location of your choosing.

There are two main steps to get set up:

  1. Create a Docker container that you can use to run Ant. You will only need to do this once this semester for the course, as you can reuse the same container across the different SimpleDb labs.
  2. Download the starter code for the lab and run Ant commands via the Docker container. Each lab will have its own starter code (and you'll use a new subdirectory). You'll need to slightly change the command you use to run Ant tests with Docker for each lab.

1. Create a Docker container that you can use to run Ant

If it's not running, open Docker desktop. Also open Visual Studio Code and open the directory where you plan to put the source code for the labs in CS 133. In the example screen shots below, I use the directory cs133local. This is where I'll put subdirectories for each of the labs in this course. Then open the terminal in VS Code. Your VS Code window should look something like this:

Next we'll create a Docker container using a Docker image for this course. When we create the Docker container, we'll "link" your local directory with a directory in the Docker container. Doing so will allow the Docker container to access your source code files and run Ant tasks on them.

We'll use the docker create command in your VS Code terminal window. In the command below, you'll want to replace the words "full path to local" with the full absolute path to your CS 133 directory. E.g., in this example the full absolute path to my cs133local directory is /tmp/My Documents/cs133local. Here's the command template:

docker create -i -v "full path to local":"/cs133docker" --name cs133container bethtrushkowsky/databases

After modifying the above command to use your local directory's full path, you can run it in your VS Code terminal window. After running it, your window will look something like this:

You've just created a Docker container called "cs133container". To actually use it, you'll have to start it with the start command, like this:

docker start cs133container

Note: If you stop the Docker container (e.g., by using the stop command or by closing Docker Deskop), you'll need to start it again to use it. You will not need to recreate it (unless you delete it).

2. Download the starter code for the lab and run Ant commands via the Docker container

Now that you've created the Docker container and started it, let's see how to run Ant commands with the starter code for Lab 1. If you haven't yet, download the tar.gz file into your the CS 133 directory on your computer. Your VS Code terminal should still be open to that directory as well. Your window should look something like this:

You can extract the starter code files using the tar command from the lab writeup; here it is again:

tar xvzkf cs133-lab1.tar.gz

Now let's run an Ant task to run one of the unit tests. We'll run the Ant task via Docker, using docker exec. This command has a couple important pieces, here's a template:

docker exec --workdir directory-to-run-command-in container-name command-to-run

Recall above when we created the Docker container that we "linked" your local CS 133 directory with one called "cs133docker" in the Docker container. Thus, to ask Docker to run an Ant command in your cs133-lab1 directory, we'll specifiy its working directory as "/cs133docker/cs133-lab1". The container's name is cs133container. Lastly, for the command-to-run, we want one of the Ant commands as described in the lab writeup. For example, to run the unit test TupleDescTest, the full docker command would look like this:

docker exec --workdir /cs133docker/cs133-lab1 cs133container ant runtest -Dtest=TupleDescTest

After running the command above, your VS Code window may look something like this (note that we expect this unit test to fail because no code has been written yet):