Example: deploying a web app with Docker
A server for this term
- You have an account on cs347.mathcs.carleton.edu. You can SSH to it using your Carleton username and password.
Before using docker for the first time on this machine, you need to run:
dockerd-rootless-setuptool.sh install --skip-iptablesTest your setup with:
docker run --rm hello-world
A tiny example
Here are steps you can take to deploy a small Flask web application on cs347. For the web app itself, I just copied a sample from my CS257 samples. This one illustrates using some shared HTML and CSS across multiple pages.
- Login to cs347.mathcs.carleton.edu (either
sshin a terminal or use the VS Code SSH plugin or something similar). - Grab a clone of my CS347 repository.
- Poke around the
dockerdirectory in my repo. In particular, take a look at theDockerfileand the main program at the bottom ofapp.py. Build an image based on this flask app.
cd docker docker build -t flask-demo --rm . [--rm ensures that temporary images created during the build process are deleted] docker images [to make sure your image is showing up]Check your email from April 30 to see the ports you have been allocated on cs347. Use those ports as needed during your experiments. I will call your port PORT in the commands below, but it will be something like 5123.
Run a container based on the flask-demo image:
docker run --rm -p PORT:5000 flask-demoThis will run the container attached to your terminal, so to terminate the container, you can just hit Ctrl-C. The Flask app in the container is listening for connections to its internal port 5000, which is in turn mapped to cs347's port PORT. Thus, an outsider can connect to the container's Flask app by connecting to cs347's port PORT.
(Why can I be sure the Flask app is listening at the container's port 5000? Where is that info stored?)
One more note: the
--rmflag ensures that when this container stops running for any reason, the stopped container will be deleted, which I find handy for this kind of experimental/pedagogical container.(By the way, I usually just do normal internet searches, but for fun I just asked ChatGPT "why does docker keep terminated containers?" and I got a really helpful response. A good example of why I am only an LLM skeptic, not a full-on LLM-hater. The discussions I found with that same query via Google were interesting, but less to-the-point and harder to parse than what I got from ChatGPT.)
- Open a browser and go to http://cs347.mathcs.carleton.edu:PORT/. Does it work?
- Kill your container with a Ctrl-C.
Launch the container again, but this time, include the
--detachflag:docker run --rm --detach -p PORT:5000 flask-demoThis will run the container in the background (i.e., disconnected or "detached" from your terminal), so you can logoff and it will still be running.
- Try logging off cs347 and hitting the app with your browser again. Does it work?
Now you need to terminate the container.
docker ps -a to get a list of containers, from which you can get your container's id and name docker stop CONTAINER_ID_OR_NAMETrying to go to the web app again with your browser should now fail.
- All done!