Repository navigation
Docker Lesson
Docker lets us package a program with the software it needs to run. That package is called an image. When we run it, Docker creates a container for the program to run in. For our example, we’re packaging a small Python program with Python itself. This means we can use the Python version included in the image without changing the version already installed on our computer.
A program might work on my computer but have problems on someone else’s because they have a different software version or are missing something it needs. Docker helps by packaging those things with the program. When we share the same image, we’re starting with the same software setup. This can save time setting things up and figuring out why a program works for one person but not another.
A Dockerfile is a text file with instructions Docker follows to build an image. It tells Docker what software to include, what files to add, and what command to use when the container starts. For our Python example, these instructions tell Docker how to prepare the image so it can run our program.
An image is the package Docker builds from the instructions in a Dockerfile. It contains our program and the software it needs to run. The image is saved so we can use it again or share it with someone else. Having the image on our computer doesn’t mean the program is running yet. We use that image to create a container and run the program.
A container is a separate space Docker creates from an image to run a program. It uses the code and software included in that image. In our example, the Python program runs inside the container and prints a message. The container stops once the program is done. The image is still saved, so we can use it again.
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.
-
Docker helps us package a program with the software it needs.
-
A Dockerfile gives Docker the instructions to build an image.
-
An image is the saved package.
-
A container is where we run the program using that image.
-
Our container stops when the program is done, but we can use the image again.