Repository navigation
Lab Instructions
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 tells docker which image to use
WORKDIR creates a working directory for the app inside the container
COPY allows you to copy any files or folders from your host system
CMD has two parameters, the first is what the command is to be called and the second parameter is the file that should be executed when the command is called
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 .
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.
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.
- 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.
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.