How to Deploy a Machine Learning Model with Docker
Deploying with Docker means packaging three things into one image: the trained model, the dependencies with pinned versions, and the server code that answers prediction requests. The container makes the same environment run on your machine and on the server, with no version surprises. The steps below cover folder layout, the Dockerfile, a local test, and the checks to run before you publish.
What has to go into the Docker image for a model?
A model image needs four things: the trained model file, the dependency list with fixed versions, the server code that exposes the prediction, and the build instructions. Leave one of them out and the container starts but fails on the first request.
The model is not trained inside the image. You train it in the notebook or script, save it to disk, and copy it into the image as an artifact. The container only serves: it loads the file and answers.
- Serialized model: the file produced at the end of training.
- requirements.txt with the exact versions of the libraries used in training.
- Inference server code that takes the input data and returns the prediction.
- Dockerfile with the build recipe.
- Entry command that starts the server on the chosen port.
How should you organize the project folders before the build?
Keep training and serving apart. Training lives in a notebooks directory that never enters the image. Serving lives in a small folder with only what production needs.
That split keeps raw data and notebooks out of the container, which would bloat the image and expose information that should stay in the analysis environment. It also makes clear which code has to be tested before each release.
- app/ — server and inference code.
- app/model.pkl — the trained model artifact.
- app/requirements.txt — dependencies with fixed versions.
- Dockerfile at the root of the serving directory.
- notebooks/ — analysis and training, outside the build context.
How do you write the Dockerfile and the inference server?
The Dockerfile starts from a Python base image, copies requirements.txt and installs dependencies before copying the code. That order uses the layer cache: changing the code does not reinstall the libraries on every build.
The server needs two endpoints. The health endpoint tells you whether the container is up; the prediction endpoint receives data in the same format used in training. Keeping the input format identical to the notebook is what prevents silent errors at inference time.
- FROM an official Python image.
- COPY requirements.txt and RUN pip install.
- COPY the server code and the model file.
- EXPOSE the chosen port.
- CMD to start the server when the container boots.
How do you test the container before publishing?
Build the image, run the container with the port mapped, and call the endpoint with a sample record. If the response matches the prediction made in the notebook, the packaging is correct.
Also test with the container offline and with the model file mounted read-only. That surfaces hidden dependencies and absolute paths that only worked on the machine where you trained.
- docker build -t model:1.0 .
- docker run -p 8000:8000 model:1.0
- A test call to the health endpoint.
- A test call with a known record.
- A comparison of the output against the notebook prediction.
What should you check before publishing the image?
Before the image goes anywhere, confirm that no credential, API key or connection string is baked into a layer. Environment variables are the right place for those, and they should be injected at run time.
Tag the image with a version and keep the model artifact that produced it. If a prediction starts to look wrong, you can roll back to the previous tag instead of rebuilding from memory. Check the image size too: a slim base image and only the libraries the server actually imports usually cut it down a lot.
- No secrets in the image; pass them as environment variables.
- A version tag on every build, plus the matching model artifact.
- Slim base image and only the libraries the server imports.
- Logging on the prediction endpoint, so you can see what was requested.
- Port and model path read from environment variables instead of hardcoded.
How does the course cover the model training that goes into the container?
Deployment starts before Docker: it starts with a model that was trained and validated properly. The Python for Data Science and Analytics course goes from zero to advanced in three main modules — Basic, Intermediate and Advanced — plus bonus modules.
The track covers IDE and Jupyter Notebook setup, basic functions, NumPy and Pandas, user-defined functions, loops and conditionals, data visualization, data cleaning and preparation, and outlier detection. Then it moves into machine learning: linear and logistic regression, K-Means, SVM, Naive Bayes, decision tree, Random Forest, Gradient Boosting, XGBoost, LightGBM, feature selection and cross-validation.
The course closes with a final project: end-to-end exploratory analysis and machine learning, with real market projects to build a portfolio. Lessons are recorded in high quality and watched at your own pace, with guided practice exercises. Enrollment and full details are on python.paanalytics.net/.
- Price: R$ 470, or 12 installments of R$ 20.37 on a credit card.
- Checkout on Hotmart, with credit card or boleto.
- Unlimited access: you keep the course, with no expiration date.
- Certificate of completion.
- Exclusive community for networking.
- No prior programming knowledge required to start.
| Criterion | Notebook run | Container serving |
|---|---|---|
| Environment | Depends on the machine it runs on | Fixed by the image definition |
| Dependency versions | Whatever is installed locally | Pinned in requirements.txt |
| Input format | Whatever the notebook cell holds | Enforced by the prediction endpoint |
| Repeatability | Hard to repeat on another machine | Same image runs on any host with Docker |
| Purpose | Analysis and training | Serving predictions |
Criteria come from the build and test steps described on this page; no external benchmark was used.
Learn the model before you package it: Python for Data Science and Analytics
Secure payment via Hotmart, up to 12 installments.
Enroll now Product siteFrequently asked questions
Do I need Docker experience to follow this tutorial?
No. The commands are limited to build, run and a test call. You do need to read Python and run commands in a terminal, because the server code and the Dockerfile are written by you. If the modeling part is still new, the course starts from zero and covers the training that produces the artifact.
Should the model be retrained inside the container?
No. Training inside the image makes every container start expensive and makes the build depend on data that should not ship. Train outside, save the artifact, copy it in. The container only loads the file and serves predictions.
How do I keep the image from getting too large?
Start from a slim Python base, install only the libraries the server imports, and keep notebooks and raw data out of the build context. A .dockerignore file listing notebooks, data and local environments prevents them from being copied by accident.
Can I reuse the same Dockerfile for a different model?
Yes, as long as the input format and the prediction endpoint stay the same. Replace the artifact, rebuild with a new tag, and keep the previous tag available for rollback. If the input columns change, update the server code and the test record together.
What if I still need to learn the modeling part before deploying?
The Python for Data Science and Analytics course goes from zero to advanced in three main modules plus bonus modules, covering NumPy and Pandas, data cleaning, and machine learning from regression to Gradient Boosting and XGBoost, ending with an end-to-end project. It costs R$ 470, or 12 installments of R$ 20.37, paid on the Hotmart checkout by card or boleto. Access is unlimited, there is a certificate of completion and an exclusive community; enrollment is at python.paanalytics.net/.