Docker in the classroom (part 1)
In this series of posts I’m going to explain why and how Docker can be a great resource for teachers and lecturers. Since you’re reading this post, I’m going to assume you have heard of Docker and are wondering how it might help you. My aim is to convince you of its value, and to show you that the learning curve is not at all steep if your primary goal is to facilitate learning.
In each post I’ll introduce a use case, starting with some very simple ones and gradually ramping up to the intermediate level. This week it’s MySQL basics.
First off, what is Docker?
Docker is a container platform, which means it provides a framework for running applications inside containers. If that already sounds a bit technical, all you really need to know about containers is that they provide an isolated environment in which the application can run. In other words, they won’t interfere with the rest of your system. The other important characteristic of containers is that they will consume less of your system’s resources than a virtual machine environment, as well as being easier to manage.
Why use Docker in the classroom?
- Docker is cross-platform — meaning you don’t have to worry what OS your students are running. Nor do you have to persuade your IT service team to implement dual boot or roll out additional VMs or switch to cloud services or whatever other complicated/costly solution you thought you’d need to be able to teach basic stack skills. In practice, it means you write one set of lab instructions which everyone ought be able to follow.
- It is easy to learn —while things can get more complicated, for the simple use cases we’re interested in, Docker really isn’t hard. If you’ve ever had to install databases or web servers on your system, you’ll be amazed how easy Docker is by comparison.
- Nothing bad is going to happen — you can spin up and remove containers in seconds. Your students can make a complete mess in their containers and nobody will get hurt.
- Because it is worth learning — Docker is so widely used that you might as well call it a core skill for anyone likely to work in tech — including you!
What’s the catch?
The main catch to using Docker (with students) is the persistent storage conundrum. With Docker, you have to assume that anything you create inside a container can easily be destroyed. In short, nothing is permanent. For some students this will be hard to get their heads around, so you’ll need to come up with a suitable workflow to ensure your students don’t lose work. For example, using Docker alongside a repository management system such as git is one solution. I will cover a few of the options in a later post.
Other issues to be aware of are:
- Need for a free Docker hub account in order to pull container images from Docker hub (I’ll explain what that means shortly). Depending on your lab setup this could mean one account for everyone, or getting students to make their own accounts.
- Hyper-V and container features need to be enabled on Windows machines. In lab scenarios, this may be something your IT team needs to be made aware of.
- Older versions of Windows (<10) not supported
Case study 1: Docker for learning MySQL and SQL
The fact you’re still reading must mean you’re still considering Docker for the classroom… Great, so let’s start with the first of our use cases: MySQL.
Most students’ foray into databases starts with Microsoft Access or a relational database management system such as SQL Lite or MySQL. I’m not going to debate which is the best entry-level database management system here—you’re best placed to make that decision because you know your students and you know your own strengths and resources too. What I will say is, at some point, many students are going to need to learn relational databases and SQL, and that it needn’t be a painful experience.
Here I’ll show you how to create a safe space for students to learn the basics of MySQL.
NB: It’s still down to you to create the lesson content!
What you’ll need
- Docker or Docker Desktop — stick with the default Linux containers if you’re using Docker Desktop.
- Free Docker Hub account
- Command-line shell such as Windows Powershell
- Some pre-prepared .sql files containing the statements needed to setup a minimal database. Feel free to use one of my dummy databases.
Step 1: Pull the latest MySQL image from Docker hub
Container images are files which contain the instructions required to run an executable version of an application in a docker container. There are thousands of container images available to download from Docker hub, and getting hold of one is as simple as executing
docker pull image:version
from a terminal. You can also create your own images (something we’ll leave off doing until a later post).
Open a terminal and execute:
docker pull mysql:latest
You should now see the image listed when you execute:
docker images
Step 2: Practice running/stopping/restarting a MySQL container
Once you have downloaded the image, spinning up a container is easily accomplished with
docker run image
(plus a few optional parameters which I’ll explain in just a sec.)
Example:
docker run --name mysqlContainer -e MYSQL_ROOT_PASSWORD=pwd -d mysql
In the above I’ve set these optional parameters:
- I’ve given the container a name by setting the
--name
option. If you don’t do this docker will give the container a weird random name. - I’ve set the container’s
MYSQL_ROOT_PASSWORD
environment variable. Environment variables are set with the-e
flag. - I’ve used the
-d
flag to make the container run in the background
You should now see the container listed when you execute:
docker container ls -a
If you want to stop the container you can simply run:
docker stop mysqlContainer
To restart the container:
docker restart mysqlContainer
Or to remove it completely:
docker rm mysqlContainer
Please note that you cannot remove containers while they are running.
Step 3: Access the MySQL interactive shell
Now you know how to start up a MySQL container, let’s have some fun with it…
If you were running MySQL locally and wanted to start the interactive shell, you’d simply run mysql
from a terminal right? (Psst, now is not the time to talk about MySQL GUIs. We’ll assume you want your students to feel less than terrified by CLIs. Darn, if only more teachers were like you.) Well, fortunately docker lets you execute commands on a container using its exec
command.
Example:
docker exec -it mysqlContainer mysql -p
Boom! We’re in the MySQL shell.
Step 4: Restore a database from .sql file(s)
The last thing I’m going to show you is how to restore a database from a bunch of statements written in .sql files. This is a useful thing to be able to do, either because you want to give students a database to practice on, or because you want them to port their own database into docker.
If you’ve used MySQL before then you’re probably familiar with the source
statement which allows you to execute a batch of queries from a .sql file. That’s exactly what we’re about to do, only we need to make sure our .sql file is available from the container first. Fortunately, docker gives us the cp
command to copy local files into the container’s file system.
Example:
docker cp setup.sql mysqlContainer:/var/lib/mysql/setup.sql
In the above, setup.sql
is a path to the file I intend to copy, followed by the name of the container I want to copy it to and a location. I’m copying it to /var/lib/mysql
because that is MySQL’s default data directory.
Back in the MySQL shell, restoring the database is as simple as:
SOURCE /var/lib/mysql/setup.sql
Boom! You have a lovely database to play with. I always keep my schema setup statements separate from my insert statements, so you’ll need to repeat the above with dummy_data.sql
if you’re using one of my dummy databases.
Summary
I hope this simple exercise has convinced you to try docker in your classroom. If it has, stay tuned for the next edition!
Please feel free to comment with your own experiences using docker in an educational setting.