CpS 404 Internet App Development

Lab 3: Docker

Objectives

  • Learn to use Docker to deploy web applications

Prerequisites

Install MySQL Workbench (not MySQL Server - you’ll install that as a Docker container) and Docker Desktop on your computer, if you don’t already have it installed.

Instructions

Run MySQL Database Server with Docker

In this part of the lab, you will run a MySQL database server in a Docker container on your laptop.

  1. To begin, copy the class lab3 folder to your computer. Review the docker-compose.yml file. It defines two Docker “services” named mysql1 and app1. The mysql1 service, when started, runs a MySQL container. The app1 service, when started, runs in a Node.js container that uses the MySQL database.

    Notice the port mappings in this file. In order for these services to start successfully, you must not be running any services on your computer that use port 3306 (such as an existing MySQL instance) or port 80 (such as a local web server), or the container startup will fail. If that happens, you can either stop your existing MySQL instance / web server, to allow the containers to start, or you can change the port numbers in the docker-compose.yml file, which will require you to make adjustments to the procedures given below.

  2. Execute the following to start the MySQL database:

    cd cps404/lab3
    docker compose up -d mysql1
    

    This command should download the Docker image needed to create a MySQL Docker container, configure it with the passwords specified in the docker-compose.yml file (which can be overridden using environment variables), run the scripts in the db directory to configure and start it listening on port 3306, the standard MySQL port.

  3. Verify that mysql is running using docker ps:

    docker ps
    

    You should see a single container entry named mysql1.

  4. Experiment with connecting to the MySQL database using the MySQL Workbench. When specifying the hostname, use the address localhost. Use the username mysql, password mysql. The default port of 3306 should work unless you have changed it in the docker-compose.yml file. After connecting, look in the mydb database. You should see a Person table containing the values defined in the lab3/db/mydb.sql script (review the script and confirm that is the case).

  5. Experiment with stopping and starting the mysql container:

    docker compose down 
    docker compose up -d mysql1
    

    Normally, stopping a container destroys all of the data it contains, but this docker-compose.yml configuration is set up to preserve the MySQL database files in between restarts using a docker “ephemeral volume” which is retained and reused when the docker mysql1 container is restarted.

    If you run into problems with MySQL failing to start, or failing to allow connections or logins from the web application, add the -v option to docker-compose down to delete the MySQL data volume:

    docker compose down -v
    

    Then restart it:

    docker compose up -d mysql1
    

Test the Web Application

In this part of the lab, you will run an express web application (located in the lab3/webapp folder) in a Docker container. The web application accesses data in the MySQL database running in the Docker container you just started. The express web application will run in one Docker container, and the MySQL database will run in a separate Docker container.

  1. In the lab3 folder, execute the following command to setup the Docker container for the web app:
    docker compose run app1 npm install
    

    This will cause Docker to download the Node.js image specified in the docker-compose.yml file for the app1 service and run the command “npm install” in the container. That will download and install the required libraries specified in webapp/package.json. After this command completes, there should be a node_modules folder in the webapp folder.

  2. Now, start the Docker container for the web app:
    docker compose up app1
    

    Notice that you’re omitting the -d option that you used when you started the mysql1 service. Without the -d option, the application will run in the foreground, so that you can more conveniently see the logs and stop/restart it from the command line.

    On successful startup, the docker engine will map the container’s port 8888 to port 80. Try accessing it there via http://localhost/hello.

  3. Make a small change to the application (add a console.log() call to the app.get(‘/hello’) code. In order for the change to take effect, stop the application by pressing Ctrl-C in the terminal. Then, use the docker compose up app1 command to start it again.

  4. Stop the MySQL server and the application:
    docker compose down 
    

Setup a VPS

In this part of the lab, you will create a Virtual Private Server (VPS) for use in this class.

I suggest looking around for a service provider such as Linode, DigitalOcean, or Microsoft Azure that offers free credits and inexpensive VPS options. The GitHub Student Developer Pack also offers free credits.

Once you’ve signed up with a provider, create a VPS. Different providers use different terms for a VPS. For example, DigitalOcean calls them “Droplets”. Your VPS should have a minimum of 1 GB RAM. I suggest choosing Ubuntu 22.04 (LTS) as the OS for your VPS.

Deploy to VPS

Login to your VPS and execute the following commands to install and configure software.

  1. Follow these instructions to install Docker: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-22-04

    If you are logged in as the root user in your VPS, you need execute only step 1 (not step 2 or any of the following).

  2. Copy files in the class lab3 folder to your instance. The easy way to do this is using the git clone command:
    git clone https://github.com/sschaub/cps404
    

    That will create a folder in your home directory named cps404 with a copy of the class files. You’ll find the lab3 folder inside.

  3. Execute the following commands to start the mysql1 and app1 containers on the VPS instance:

    cd cps404/lab3
    docker compose run app1 npm install   
    docker compose up -d
    

    Then, use the docker ps command to verify that they are running.

  4. Use your web browser to access the application running on your VPS:

    http://ip-address-of-your-VPS/hello

Production Deployment

In this part of the lab, you configure your application to start automatically when the VPS starts.

  1. Edit the docker-compose.yml file and change the two restart values from “no” to “always”.

  2. Use the docker compose command to start the containers:
    cd cps404/lab3
    docker compose run app1 npm install   
    docker compose up -d
    
  3. Verify that all containers were started:
    docker ps
    

    And verify that you can access your application via a web browser. Then, reboot the server. Wait for the server to start back up. Then, login and check to see if the docker containers are running:

    docker ps
    

    It may take a minute after the server lets you login before the docker containers are back up. Verify that you are able to access the web application using your browser.

Tips on Starting Over

If you get into trouble at various points in this lab, you might want to start over. Try using these commands, in the following sequence, to reset Docker to a clean state:

cd cps404/lab3
docker compose down -v        # Stop containers and remove the MySQL data volume
docker ps                     # Verify that nothing is running
docker container prune        # Remove all stopped docker containers 

Submission

Create a report with screen shots demonstrating the success of various steps. Your report should start with a cover sheet with your name, the assignment title, date, course number. Generate a pdf named report.pdf, and submit via Canvas.