Now that you have a Dockerfile
defining the environment and setup instructions for your Flask prediction service, the next step is to use this file to actually build a Docker image. This image acts as a self-contained package, bundling your application code, dependencies (like Flask and scikit-learn), the saved model file, and the necessary system libraries.
Building an image involves telling the Docker daemon to read the Dockerfile
, execute its instructions, and create the resulting image file system layers. The primary command for this is docker build
.
docker build
CommandThe docker build
command takes the path to the build context (the directory containing your Dockerfile
and application files) as its main argument. It reads the Dockerfile
from this context and starts the build process.
A very common option used with docker build
is the -t
flag, which stands for "tag". Tagging allows you to give your image a human-readable name and optionally a version, making it easier to manage and identify. A good practice is to use a name reflecting the application and a version tag, like my-app:1.0
or prediction-service:0.1
. If you omit the version tag (e.g., just prediction-service
), Docker implicitly uses the tag latest
.
Let's build the image for the Flask application we prepared.
Prepare Your Build Context: Make sure you have the following files in a single directory. This directory will be your build context.
Dockerfile
: The file you created in the previous section.app.py
(or your Flask application script's name): Contains the Flask code to load the model and serve predictions.requirements.txt
: Lists the Python libraries needed (e.g., flask
, joblib
, scikit-learn
, numpy
).model.joblib
(or your saved model file's name): The serialized machine learning model.Navigate to the Directory: Open your terminal or command prompt and change to the directory containing these files.
cd /path/to/your/flask_app_directory
Replace /path/to/your/flask_app_directory
with the actual path on your system.
Run the Build Command: Execute the docker build
command. We will tag the image as prediction-service
with a version tag 0.1
. The .
at the end tells Docker to use the current directory as the build context.
docker build -t prediction-service:0.1 .
As Docker executes the command, you will see output in your terminal corresponding to each step defined in your Dockerfile
:
Sending build context to Docker daemon 5.8MB # Step 1: Context sent
Step 1/7 : FROM python:3.9-slim # Pulls the base image
---> a1b2c3d4e5f6
Step 2/7 : WORKDIR /app # Sets the working directory
---> Using cache
---> f6e5d4c3b2a1
Step 3/7 : COPY requirements.txt . # Copies requirements.txt
---> Using cache
---> 1a2b3c4d5e6f
Step 4/7 : RUN pip install --no-cache-dir -r requirements.txt # Installs dependencies
---> Running in abcdef123456
Collecting flask... # Output from pip install
... (installation output) ...
Removing intermediate container abcdef123456
---> 7f8e9d0c1b2a
Step 5/7 : COPY . . # Copies the rest of the context
---> 3a4b5c6d7e8f
Step 6/7 : EXPOSE 5000 # Exposes the port
---> Running in fedcba987654
Removing intermediate container fedcba987654
---> 9d8c7b6a5f4e
Step 7/7 : CMD ["flask", "run", "--host=0.0.0.0"] # Sets the run command
---> Running in 123456abcdef
Removing intermediate container 123456abcdef
---> e1f2a3b4c5d6
Successfully built e1f2a3b4c5d6
Successfully tagged prediction-service:0.1
Docker executes each instruction layer by layer. It often uses cached layers from previous builds if the instructions and context haven't changed, which speeds up subsequent builds significantly (indicated by ---> Using cache
). When an instruction like RUN
or COPY
is executed, Docker may create a temporary intermediate container, perform the action, and then commit the result as a new image layer before removing the intermediate container. Finally, it confirms the successful build and tagging.
Once the build completes successfully, you can verify that the image exists on your system using the docker images
or docker image ls
command:
docker images
You should see your new image listed, similar to this:
REPOSITORY TAG IMAGE ID CREATED SIZE
prediction-service 0.1 e1f2a3b4c5d6 2 minutes ago 150MB
python 3.9-slim a1b2c3d4e5f6 2 weeks ago 115MB
... (other images) ...
Notice the prediction-service
repository, the 0.1
tag, a unique IMAGE ID
, when it was created, and its size.
You have now successfully packaged your Flask prediction service and all its requirements into a Docker image. This image is a portable artifact that contains everything needed to run your application consistently across different environments. The next logical step is to take this image and run it as a container.
© 2025 ApX Machine Learning