Lab Instructions - CassanovaFrankenstein00/Docker-Tutorial GitHub Wiki
Lab Instructions
Video WalkThrough
Watch this video to follow along with the lab or review the steps afterward. https://drive.google.com/file/d/1xpgF25Kf-YoFJRYwZOsrx43sj2MJa-vJ/view?usp=sharing
Create a folder in your home directory
Open that folder in vs code
Create a new file and name it: Dockerfile without using a file extension *Note: The file must be named 'Dockerfile' exactly
Inside the the Dockerfile paste the following code:
FROM python:3.12-slim
WORKDIR /app
COPY . .
CMD ["python", "python.py"]
- FROM python:3.12-slim — Starts with an image that already includes Python 3.12. The slim version includes fewer extra tools to keep it smaller.
- WORKDIR /app — Creates a folder called /app inside the image and makes it the working folder for the steps that follow.
- COPY . . — Copies the files from our project folder into /app. This includes our python.py file.
- CMD ["python", "python.py"] — Tells the container to run our python.py file using Python when it starts.
Create a python script and name it: python.py. For simplicity you can add:
print("Hello World")
Once you have both files created run in the vscode terminal:
docker build -t python .
*It looks like a typo, but don't forget the period at the end of the command. It tells docker to pull the files from the current directory.
This command builds our image using the Dockerfile. If the Python image we need isn’t already on our computer, Docker downloads it from Docker Hub. Downloading an image is called pulling. Docker then adds our program so we can run it in a container.
Now you can go to docker desktop, find the image you just built and press the play logo
Docker desktop will now ask you if you want to run the container, click yes. Docker will show the output of the container in the container logs.
Update the Program
Our original program finishes so quickly that we don’t have time to stop the container ourselves. We’re adding a 30-second pause so we can practice stopping and starting it. import time lets us use time.sleep(30), which tells the program to wait 30 seconds before printing our message. Replace the code in python.py with the updated code provided below, then save the file.
import time
time.sleep(30)
print("Hello! I updated my Docker program.")
Return to the same terminal you used to build the first image. Make sure you’re still in the folder containing your Dockerfile and python.py, then run the build command below again.
docker build -t python .
The program will wait for 30 seconds before printing the new message. We’ll use that time to practice stopping the container.
Once the build finishes, open Docker Desktop and go to Images. Find python with the latest tag and click Run. Click Run again in the window that opens to create and start a new container with your updated code.
Starting the old container won’t include your changes. Follow the steps below right away so you can stop the new container before the 30 seconds are up.
Practice Using the Container Controls
- Check that the new container’s status says Running.
- Click Stop before the 30 seconds are up.
- Check that the container has stopped.
- Click Start to run it again. This starts the program from the beginning, so the 30-second wait starts over too.
- Let the program finish, then open Logs to see “Hello! I updated my Docker program.”
- Remove this stopped practice container using the trash button. Keep the image.
What We Learned
We learned what Docker is, why we would use it, and how Dockerfiles, images, and containers work together. We built an image with our Python program and ran it in Docker Desktop. Then we checked the logs to see “Hello World.” Once the program was done, the container stopped. Our image is still saved, so we can run it again. We also changed our message, rebuilt the image, and created a new container to see the change. We practiced stopping, starting, and removing a container while keeping the image.
Docker Quiz
Complete the Docker Quiz to check what you learned: https://docs.google.com/forms/d/e/1FAIpQLSfIOuoyZwq1PG3W9s0ER-BoBPBAQ-ieQD2k0juM1919q1GJqw/viewform?usp=publish-editor
Training Feedback
Let us know what helped and what we could do better: https://docs.google.com/forms/d/e/1FAIpQLSdgyXOqXQmI2xx06mW-ASMWfDzuDj3S1ZLbIP2xHiakrFwg8g/viewform?usp=publish-editor