This repository contains a tutorial for members of CSCI 435 to walk through and understand how Terraform can be used to quickly build, run, and destroy docker containers.
People walking through the tutorial should be working in the tutorial folder. The final
folder contains the completed code that should be used if you get stuck. Their directory contents
are identical, the tutorial folder just has incomplete files!
You don't really need to worry about the tutorial/app directory -- it contains a single file which is used to create the FastAPI server when you actually run your docker container.
The four files you should be modifying are:
tutorial/Dockerfiletutorial/main.tftutorial/variables.tftutoral/outputs.tf
This project requires Docker and Terraform.
- To download Docker, refer to these docs to choose the correct version for your system. Confirm the installation worked correctly and docker is running by running
docker --help - Refer to these docs for installing terraform. Confirm the installation worked correctly by running
terraform -help
- The first step to complete the tutorial is to complete the
Dockerfile- To test that you completed
Dockerfilecorrectly, ensure you are in thetutorialdirectory and rundocker build -t myimage .This should build a Docker image calledmyimage, which you can confirm by runningdocker image ls. Remove this image by runningdocker image prune.
- To test that you completed
- Next, move to
main.tf. Complete the tworesourceblocks fordocker_imageanddocker_container.- You will need to simultaneously modify
variables.tfto declare any values that are prepended withvars.or else terraform will get mad at you because the value doesn't exist.
- You will need to simultaneously modify
- Finally, move to
outputs.tf. Following the comments, create anoutputthat catches thecontainer_namethat you specified invariables.tf
- When your code is error-free and you think it's ready to run, ensure you are in
tutorialand runterraform init- This will create a folder
.terraform.lock.hclthat holds information on yourrequired_providers
- This will create a folder
- Run
terraform plan. This will output a lot of information to your terminal, basically outlining all the changes terraform is going to make.- NOTE: If this step fails with a warning that it can't connect to docker, go back to
main.tfand specify the path to yourdocker.sockin theprovider "docker"section.
- NOTE: If this step fails with a warning that it can't connect to docker, go back to
- Run
terraform applyand enteryeswhen prompted so terraform will try to create your resources.- If this is successful, you should get an output block displaying the values you specified in
outputs.tf - If you want to specify values for the variables you define in
variables.tf, you'll need to do so on the command line. That entire command will look liketerraform apply -var="container_name=[some_name]" -var="image_name=[some_name]"
- If this is successful, you should get an output block displaying the values you specified in
- You can confirm that your code worked in a couple ways
- On the command line, run
docker ps -a. This will list all running containers. You should see your container running. - In your web browser, navigate to
localhost:[PORT]where you replace[PORT]with the port number you specified inmain.tf. You should see the output{"Hello":"World"}
- On the command line, run
- Tear everything down by running
terraform destroyand enteringyeswhen prompted.
To demonstrate that you successfully completed the tutorial, submit a few screenshots to Alejandro.
- In your web browser, navigate to
localhost:[PORT]/docs, again replacing[PORT]with your specified port number. Take a screenshot of the API docs that appear.- Hint: If you didn't modify
app/main.py, you should have two endpoints:Read RootandRead Item.
- Hint: If you didn't modify
- From the command line, take a screenshot of the
terraform applycommand you ran, including any variables you passed in. - Also screenshot the
Outputssection that is printed at the end of theapplyoutput.- Hint: this should look something like:
Outputs: container_id = [some_value] [another_key] = [another_value]
This tutorial leverages the FastAPI docs for creating a FastAPI Docker container, and the Terraform docs for building docker containers and building docker images.