« Back to the main CS 300 website

Lab 0: Getting Set Up (Docker & Git)

Due February 1, 2022, at 8:00pm EST


Introduction

Welcome to your first lab! The main focus will be to get you set up with the tools we’ll be using throughout CS300, and give you an intuitive understanding of each one.

The main tools we’ll be using are Docker, a convenient way to manage virtual environments as “containers”, and Git, the most prominent version control software.

Tool Use
Docker Software environment virtualization
Git Version control software
GitHub Online code storage and version control

Why are we using these tools?

What if I don’t have my own computer?

Development Environment Setup

Your computer is probably running Mac OS X, Windows, or Linux. These different operating systems all come with different libraries and preinstalled software. A Virtual Machine (VM) can establish the same software environment for everyone.

Software inside a VM believes it is running on a physical computer, even though it is running within an OS within another computer. Achieving this level of virtualization has costs, including the cost of emulating the underlying hardware.

In this class, we will use a container, a technology that emulates an OS without the full overhead of a VM. The container runs a Linux-based operating system, Ubuntu. Our grading server also runs a Linux-based OS, so if your code works in the container, it will work on the grading server.

(Optional reading) Virtual machines vs. containers?

We will talk about this in the OS section of the course. But if you’re curious, here is a more detailed walk-through of the differences between a Virtual Machine and a container.

Docker

Docker is one of the most popular container solutions and widely used in industry. In CS 300, we use Docker because it lets you run a course container on Windows, macOS, or Linux.

Task: Download and install Docker.

You may download Docker here. On Linux machines, follow the instructions here.

After downloading Docker, follow Docker’s instructions to install it on your OS. Accept if Docker asks for privileged access.

On Windows or macOS, open the Docker Desktop application after it has been installed. You may see a message similar to “Your Docker is starting…”. Once this message goes away, your Docker has started successfully!

Verify Docker is installed by executing the following:

$ docker --version

A docker version number should be printed.

After installing Docker, a Docker process (the Docker daemon) will run in the background. Run the following command to verify:

$ docker info

This should print some information about your Docker installation.

If you see the following error:

ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

it means Docker hasn’t started running yet. On Windows or macOS, ensure your Docker Desktop is running. On Linux, try the command sudo systemctl docker restart in a terminal.

Windows-based computers only

To run the following steps in this lab, you will need to set up Windows Subsystem for Linux (WSL). WSL should already be enabled after you install Docker, but you may still need to install a Linux distribution. This will run in an actual Linux VM, and you will run your Docker container within that VM (turtles all the way down for you!).

  1. Do I have a Linux distribution (Linux distro) installed?
    • Run wsl -l -v in the Command Prompt or Powershell. If there is only “Docker Desktop” and “Docker Desktop Data”, you do not have a Linux distribution installed. Proceed to step 2.
    • Otherwise, you have a Linux distro installed. Proceed to step 3.
  2. Install a Linux Distribution.
    • Run wsl --set-default-version 2 to ensure Ubuntu will be installed under WSL 2.
    • Install “Ubuntu 20.04” from Microsoft Store.
    • Click “Open” after Ubuntu is downloaded. A terminal will open and guide you through the installation process.
  3. Ensure your Linux Distribution runs on WSL 2.
    • From the output of wsl -l -v, find out if your Linux distro is using WSL 1 or WSL 2. If it’s WSL1:
      • Run wsl --set-version <distro name> 2 to update your distro to use WSL 2.
  4. Set your default Linux distro
    • Run wsl --setdefault <distro-name> to configure your default Linux distro. <distro-name> should be “Ubuntu-20.04” if you installed using step 2.

Enter wsl in your Command Prompt or Powershell, and you’ll enter into your WSL! For the rest of the Lab, run commands within your WSL Linux environment, unless otherwise specified.

You will also need to connect Docker with WSL. To do so, open your Docker Desktop’s settings (on its top right corner), click “Resources”, “WSL integration”, then enable integration with your Linux distro. Then, click “Apply and Restart”.

Set up CS 300 Docker Environment

In Docker, an environment is defined as a Docker image. An image specifies the operating system environment that a container provides. An image can also contain additional software dependencies and configurations. These instructions are specified in a file, the so-called Dockerfile.

Next, you will download the the course’s setup code and create the CS 300 Docker image!

macOS only: install Apple development tools

If you’re running on macOS, you will need to install a set of Apple-recommended command-line tools via the following command:

xcode-select --install

This ensures that your computer has installed git, a program we’ll use below. Alternatively, you may also download and install git directly, following instructions here.


Task: Do the following to set up your development environment.

  1. Enter the directory on your computer where you want to do your coursework. For Windows users, choosing somewhere in the C drive will make the following steps easier.

Then, enter the following command to download our development environment to the new subdirectory DEV-ENVIRONMENT (you can choose your own name, DEV-ENVIRONMENT is a placeholder!):

$ git clone https://github.com/csci0300/cs300-s22-devenv.git DEV-ENVIRONMENT

This command clone a GitHub repository onto your computer; we will explain the details of Git and cloning later in this lab.

  1. Run cd DEV-ENVIRONMENT to enter the directory you have just created.

  2. Inside this folder, do the following:

$ cd docker             # enters the folder containing Dockerfiles
$ ./cs300-build-docker  # builds a docker image you'll use in the semester

./cs300-build-docker builds your docker image, which may take a while. It’s normal for this script to run for up to 15 minutes.

Why are there two Dockerfiles?

You may notice that there are two Dockerfiles in the docker folder: Dockerfile and Dockerfile.arm64. Why are there two Dockerfiles, when CS 300 only uses one container?

Machines run on different architectures, and we will explore that in our course during the semester. Computers that use an Intel chip runs on Intel’s x86 architecture. Recent Macs released by Apple begin to use Apple silicon, which run on the ARM architecture.

Different architectures use different sets of CPU instructions. Therefore, programs running on different architectures need to be compiled differently.

Some CS 300 projects are designed to run on the x86 architecture. When running these projects on a different architecture (e.g., on an Apple M1 machine), they must be cross-compiled to use x86 instructions.

Students running on ARM machines use DockerFile.arm64, which installs additional packages to compile x86 binaries on an ARM machine. Other students, running on chips that use the x86 architecture, use Dockerfile.

Entering the container

Once you created your Docker image, you will want to create a container running this image.

Task: Enter your container and explore it by running a few commands. Then, exit the container by exit, or Ctrl-D.

Windows users: use the Windows-specific instructions below instead.

Make sure you’re inside directory DEV-ENVIRONMENT when running the command below.

$ ./cs300-run-docker          # enters your Docker container
cs300-user@9899143429a2:~$    # you're inside the container!
cs300-user@9899143429a2:~$ uname
Linux
cs300-user@9899143429a2:~$ echo "Hello world!"
Hello world!
cs300-user@9899143429a2:~$ ls -lah
total 24K
drwxr-xr-x 6 cs300-user cs300-user  192 Jan 25 22:23 .
drwxr-xr-x 1 root       root       4.0K Jan 25 22:25 ..
-rw-r--r-- 1 cs300-user cs300-user  132 Jan 25 22:23 .bash_profile
-rw-r--r-- 1 cs300-user cs300-user 4.0K Jan 25 22:23 .bashrc
-rw-r--r-- 1 cs300-user cs300-user   25 Jan 25 22:23 .gdbinit
-rw-r--r-- 1 cs300-user cs300-user  813 Jan 25 22:23 .profile
cs300-user@9899143429a2:~$ exit  # or Ctrl-D

Don’t worry if the number after cs300-user is different. This is an identifier that uniquely identifies this container.

You may run any Linux commands inside this container. To exit, enter exit or use Ctrl-D.

Instructions for Windows only

If you see errors along the lines of bash: '\r': command not found or bash: /home/cs300-user/.bash_profile: line 5: syntax error: unexpected end of file when you enter the container, you need to convert your files’ line endings (characters that delineate the end of a line) from Windows to UNIX (Linux) format.

To do so, do the following:

  1. Enter into WSL. You may do so by opening Ubuntu in the start menu or through the wsl command.
  2. Use cd to navigate to the folder where you just cloned the setup repository. If you cloned your setup repository to your C: drive, use cd /mnt/c to enter the C: drive from your WSL.
  3. Run sudo apt-get update, then install dos2unix with sudo apt-get -y install dos2unix.
  4. Run dos2unix ./cs300-run-docker (you should only need to run this once).
  5. Next, run ./cs300-run-docker.
  6. If you see the next line begins with cs300-user@@9899143429a2:~$ ..., you’re inside the container, hopefully without errors!

Shared Folders

If my docker container is a separate (virtual) computer than my laptop, how will I move files between the two?”, you may ask. Great question! You’ll move files between the two in any of the ways you can move files between any two computers! (Bear with us!)

To get files to and from department machines, you may have used things like scp, sftp, and Cyberduck, which allow you to copy files over the network. These work, but are inconvenient if you’re constantly moving them back and forth. (Git would technically also fall under this category, even though it’s fancier, as you’ll find out shortly.)

If you’re lucky, you may have been exposed to FUSE and sshfs, which allow you to mount a filesystem over the network. This allows your computer to navigate the remote files as if they were stored locally, which means any changes you make are happening on both ends automatically.

Inside of the container, your home directory (/home/cs300-user, or ~) is actually a mount of the home directory inside your DEV-ENVIRONMENT directory. Any changes you make in one will be visible in the other.

Task: Outside of the container, go into your [DEV-ENVIRONMENT]/home folder, and create a file and a folder. Then, go into your container, delete that file, and add a file to the folder. This is so you get a feel for what is going on, since you’ll be using this on a regular basis!

Help

Outside of the container, in your [DEV-ENVIRONMENT]/home folder:

$ touch cool_file
$ mkdir awesome_folder
$ cd ..
$ ./cs300-run-docker

Inside the container:

cs300-user@9899143429a2:~$ ls # print the file and dir we just created
awesome_folder    cool_file
cs300-user@9899143429a2:~$ rm cool_file
cs300-user@9899143429a2:~$ cd awesome_folder
cs300-user@9899143429a2:~$ touch even_cooler_file
cs300-user@9899143429a2:~$ exit # or just CTRL-D

Back outside the container:

$ cd home            # this enters the mounted directory
$ ls                 # should just show awesome_folder
awesome_folder
$ cd awesome_folder
$ ls                 # should show even_cooler_file
even_cooler_file

If you move or rename your dev-environment folder…

Your Docker container will still try to mount to the original dev-environment path, even after you rename, remove, or move the folder DEV-ENVIRONMENT.

After moving your dev-environment folder, you’ll need to delete the old container and start a new container. You can do so with:

./cs300-run-docker --clean

You should be able to enter a container, and see all of your work now!

Text Editors and IDEs

What development environment or editor to use is a question that divides people and has led to countless hours being wasted on heated discussion, offline and online for decades. Some people prefer very simple editors that run in their terminal, while others prefer graphical integrated development environments (IDEs). For CS 300, we don’t really care what you use – if it works for you, great!

To make your life easier, however, you probably want to use an editor with syntax highlighting for C and C++, the main programming languages we’ll use in CS 300.

You can edit your source code inside the course container, or on your normal operating system. Recall that the home directory inside the container is a mount of your local cs300/home directory (or whatever you named it). This means that you can install any editor or IDE of your choice on your local machine, edit your files and work on your assignments locally, and all of your changes will be reflected in your container! You can then compile, build, and run your assignments inside your container. This is the power of mounting :fire:!

Below are some popular choices for C and C++ development:

Simple editors:

IDEs:

Installing text editors

Our containers do not include terminal-based text editors like vim and emacs. To make it easier to edit code within the container, let’s install some text editors!

Linux distributions, like Ubuntu, let you manage your packages, or tools, with a neat tool called apt. This tool handles installing, deleting, and updating packages.apt can install many things, including tools to build (or compile), and debug your code. Our containers already come with these tools for your convenience.

Here, we’ll use apt to install some text editors!

Task: Install text editors.

Within your container, use apt to install:

You will need to use sudo, which runs commands with elevated privileges.

First, do:

$ sudo apt-get update

This builds an up-to-date index of packages available to download.

Then, for each package, do:

$ sudo apt install <package>
Help
$ sudo apt install vim
$ sudo apt install emacs-nox

Custom packages and ./cs300-run-docker --clean

Running ./cs300-run-docker --clean will remove any custom packages, such as vim, that you have installed. This is because --clean removes any existing cs300 containers on your system.

Fortunately, you should be able to re-install your packages with apt using your new-found package installation skills!

If you have custom configurations for your packages, (e.g., a .vimrc file for vim), the configurations are persisted even if you have used --clean. This is because user-specific configurations are stored in ~ (or its sub-directories), which are just a mount of your [DEV-ENVIRONMENT]/home directory on your machine.

Git

Git is an awesome version control tool for your code. In essence, it allows you to continuously save your project as a sequence of small changes instead of one final thing. Some of the cool things this grants you are:

Learning Git

There are a ton of resources out there to learn about Git, so we won’t try to recreate them. Instead, we’ll link you to a few tutorials, and encourage you to find more on your own!

Before you start running Git commands, you’ll need to configure your name and email in Git.

Task: Within your container, set your name and email in Git.

cs300-user@9899143429a2:~$ git config --global user.name "Jennifer Stevenson"
cs300-user@9899143429a2:~$ git config --global user.email "jennifer_stevenson@brown.edu"

If you are not already signed up to GitHub with your brown.edu email address, add it to your profile so that GitHub knows to correctly attribute commits to your username.

Optional: you can cache your GitHub credentials to avoid entering them in each time.

cs300-user@9899143429a2:~$ git config --global credential.helper store

Now, take a look at some beginning Git tutorials.

Task:

Read the Harvard CS 61 Intro to Git.

Then, complete this section of the Earth Lab Version Control Intro tutorial. (Run the commands in section 3 inside your container).

More Resources

Feel free to check out other resources to learn more!

Git – The Simple Guidequick and dirty guide
Git Handbookin-depth introduction

GitHub Classroom Setup

We use GitHub Classroom to distribute the assignments throughout the semester. You will be working with two repositories: one for projects, and one for labs. Each will contain a directory for each project or lab.

Task: Accept the GitHub Classroom assignment for projects and labs, then clone each repo into your container.

Projects: Set up CS300 Projects 2022
Labs: Set up CS300 Labs 2022

Help
  1. Enter into your container.
$ ./cs300-run-docker
cs300-user@9899143429a2:~$    # inside the container!
  1. Clone each repo. Do this by going to the GitHub Repo link created by classrooms, and clicking Clone or download. Copy the url in the box, and use it to clone like so:
$ git clone git@github.com:cs300-s22-projects-YOURUSERNAME.git
$ git clone git@github.com:cs300-s22-labs-YOURUSERNAME.git

… where YOURUSERNAME is your GitHub username. The above commands assume that you have registered an SSH public key with GitHub; if you haven’t, try using https://github.com/cs300... instead of the git@github.com:cs300-... URL.

Once you cloned the repository, you should see the snake in the projects repo, and the lab0 directory in the labs repo. These are the only assignments we’ve released so far; you will add others later.

Exercise

At this point, you have your version control set up. Let’s do a quick exercise to practice your newfound Git skills. The results of this part of the lab are what you hand in at the end.

Inside the lab0 you should see a file hello_world.c. This is a Hello World program, and is the one you’ll make changes to. To run the program you need to compile it first (we will cover compilation in detail in a later lab). For now, just use the following commands:

$ gcc hello_world.c -o hello_world # to compile it
$ ./hello_world # to run it

Note: Make sure you do everything inside of your container.

Task:

  1. Modify the program so it prints the following tounge twister:
    Oh, Shelley? She sells seashells by the shilling. Silly, selling Shelley.
  2. Then, commit and push your changes.
Help

Committing and pushing:

$ git add hello_world.c
$ git commit -m "Added tounge twister."
$ git push

You have now committed your changes to Git and they are stored in the version history (git log should show your new commit!). You’ve also pushed the new commit to GitHub’s servers. Next, you’ll set up the grading server and hand in this lab.

Handin Instructions

Head over to the grading server. If you were registered for CS 300 on the first day of classes, you should have received credentials by email.

If you only recently registered or you’re shopping the course but haven’t registered, you won’t have an account yet. In that case, please complete this form and wait until you receive your credentials. We will create accounts based on the form entries at 8pm every day.

Step 1: Log into the grading server with the password you received. Once you’re logged in, enter your GitHub username (at the top).

Step 2: Add the repository URL under the “Labs” category. Click “Labs” to initiate a fetch of your repository from GitHub.


Note: If your GitHub account is not linked to your Brown email address, the grading server will give you a command to run to verify your repository.

Step 3: You should see your commit listed on the Labs page, as shown above. You’ll also see a button labeled “Lab 0 checkoff”. Click this button to have your lab submission checked off and graded.

You should see a screen similar to this one, with a grade of 2 points for Lab 0:


Step 4: Use your anonymous ID located at the top right corner of the grading server to fill out this quick Diversity Survey.

Filling out the survey is required and your grade for Lab 0 and Lab 1 will depend on whether you filled this out (even if you passed the checkoff on the grading server!).


Congratulations, you’ve completed Lab 0 and are set up for CS 300! :tada: