A container starts from an image.
An image is a read-only template that holds the application and everything it needs to run.
Figure 1 - From the Dockerfile to the Container
You describe an image in a text file named Dockerfile.
To build the image from this file, you rundocker build:docker build -t inventory:1.0 .
Figure 2 - The Parts of docker build
docker imageslists the images of your server.
In the lab, display the Dockerfile, build the imageinventory:1.0, and list the images to get the flag.Answer the question below
Enter the flag you obtained.
A Dockerfile starts with the instruction
FROM.
It gives the base image: the image that your image starts from.
Figure 3 - The Base Image and Its Tag
pythonis the name of the base image.3.12-slimis its tag: the version of the image.
This base image already holds Python 3.12, so you do not install Python yourself.
In the lab, you edit the Dockerfile withnano, a text editor that runs in the terminal:nano DockerfileMove the cursor with the arrow keys.
Type the line, then press Enter.Ctrl+O, then Enter: save the file.
Ctrl+X: exit nano.
The Dockerfile of the lab has no
FROMline, so the build fails.
AddFROM python:3.12-slimon the first line and build the image again to get the flag.Answer the question below
Enter the flag you obtained.
WORKDIRsets the working directory inside the image and creates it if it does not exist.COPYcopies files from your server into the image.WORKDIR /app COPY inventory.py .The dot is the destination: the working directory, here /app.
A file that you do not copy does not exist in the image.
Figure 4 - COPY Puts the Script in the Image
To start a container from your image, you run:
docker run inventory:1.0In the lab, the Dockerfile has no
COPYline, so Python cannot find the script.
AddCOPY inventory.py .after theWORKDIRline, then build and run the image again to get the flag.Answer the question below
Enter the flag you obtained.
RUNruns a command while Docker builds the image.
You use it to install what the application needs.
Figure 5 - RUN Installs a Python Package
The result is saved in the image, so every container that starts from it has the package.
In the lab, addRUN pip install pyyamlafter theWORKDIRline, then build and run the image again to get the flag.Answer the question below
Enter the flag you obtained.
CMDgives the command that runs when a container starts.
It does not run during the build.
Figure 6 - Build Instructions and the Start Command
You write the command as a list.
Each word is in double quotes, and a comma separates the words:CMD ["python", "inventory.py"]A Dockerfile uses one
CMD: when it has several, only the last one counts.
In the lab, addCMD ["python", "inventory.py"]on the last line, then build and run the image again to get the flag.Answer the question below
Enter the flag you obtained.
You can now read a complete Dockerfile:
FROM python:3.12-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY app.py . ENV API_PORT=8080 EXPOSE 8080 CMD ["python", "app.py"]This Dockerfile has two new instructions:
ENVsets an environment variable inside the container.EXPOSEdocuments the port that the application listens on.
EXPOSEdoes not open the port.
You publish a port when you start the container.
Figure 7 - The Instructions of a Dockerfile
Some Dockerfiles use
ENTRYPOINTin place ofCMD: its command is not replaced when you pass arguments todocker run.
In the next lesson, you will download an image and run it on your server.Answer the question below
Which instruction sets an environment variable?