CSCI 338: Fall 2023

Software Engineering

CSCI 338: Fall 2023

Assignments > Lab 3: Intro to Docker

Due on Tue, 09/12 @ 11:59PM. 6 Points.

1. Background

2. Install Docker

Mac

You can either install the binary from the Docker website or use brew (brew instructions below):

1
2
3
brew install --cask docker  # use "cask" b/c it's a system-level package
# verify version:
docker --version

Next, run the Docker service by looking for Docker.app using Spotlight (magnifying glass). Once it starts, go back to the Terminal and issue:

1
2
# install hello world container:
docker run hello-world

You should see that it outputs a message similar to the one shown below to the command line:

Figure 1

Docker output after running docker run hello-world:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
70f5ac315c5a: Pull complete 
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Windows

Follow the instructions on Microsoft’s website: https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-containers#install-docker-desktop.

Then, activate WSL and type the following into your WSL shell:

1
docker run hello-world

You should see output similar to the output above (Figure 1)

3. FAQs

What is a container?

A container is an isolated process (process that can’t interact w/any other parts of your OS) that runs on a host machine (i.e., your laptop).

What is an image?

“A running container uses an isolated filesystem. This isolated filesystem is provided by an image, and the image must contain everything needed to run an application - all dependencies, configurations, scripts, binaries, etc. The image also contains other configurations for the container, such as environment variables, a default command to run, and other metadata.”

What is a Dockerfile?

“A Dockerfile is simply a text-based file with no file extension that contains a script of instructions. Docker uses this script to build a container image.” Example of a Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
# syntax=docker/dockerfile:1

# the base image (you're augmenting this image)
FROM node:18-alpine             
WORKDIR /app
COPY . .

# yarn is yet another package manager for installing dependencies
RUN yarn install --production  

# runs this command on startup
CMD ["node", "src/index.js"]    
EXPOSE 3000

Important: In the example above, it is assumed that a Node project already exists with src/index.js file and a package.json file with a set of Node dependencies. See the tutorial (link below) for more instructions on that.

How do I create a Dockerfile?

Check out the Dockerfile reference. Note that some of these configuration settings are a little complicated, but over time, after going through various tutorials, you will get more exposure to what the different commands do.

How do you build an image?

You can build an image using a Dockerfile. In the context of the Dockerfile shown above, issuing this command will build the image:

1
docker build -t getting-started .

How do you start an App Container?

Once you’ve built an image, you can run it using the docker run command (where you specify the name of your image). There are many optional flags you can use with the run command (e.g. -d, -p, but the only required argument is the name of the image you want to run:

1
docker run -dp 127.0.0.1:3000:3000 getting-started

Docker Cheatsheet

This just lists the commands we’ll be using in today’s tutorial:

Images

docker images Lists available images
docker rmi <your-image-id> Removes an image
Docker Hub Image registry Shows you all the available pre-defined images

Containers

docker build . Builds a container from a Dockerfile in the current directory (.)
docker build -t <name-of-container> . Builds a container from a Dockerfile in the current directory (.) and tags it with the name my-container
docker run <name-of-container> Creates and runs a new container from an image (including images in the Docker Hub Image registry)
docker ps Lists Docker processes that are currently running
docker stop <pid> Stops Docker container from running (by process id)
docker start <pid> Starts Docker container from running (by process id)
docker rm <pid> Deletes Docker container from running (by process id)

4. Your Tasks

4.1. Complete Steps 1-3 of the “Get Started” Tutorial

For today’s lab, you will complete Parts 1-3 of the Getting Started tutorial: https://docs.docker.com/get-started/.

Before you begin, a few notes:

4.2. Create a .dockerignore file

A .dockerignore file is a configuration file that describes files and directories that you want to exclude when building a Docker image. Used for excluding large files / directories that could be installed via package manager and/or sensitive information.

After completing steps 1-3, create a .dockerignore file and add the following files:

1
2
.DS_Store
node_modules

4.3. Submit

To submit this lab:

  1. Stage, commit, and push your lab03-your-username branch to GitHub. The branch should include all of the files from your getting-started-app, as well as the .dockerignore file you just created.
  2. Create a PR.
  3. On the course moodle, submit a screenshot of your running TODO app on port 5000.
  4. Once your PR has been approved, you can rebase / merge your changes into the main branch.