Lesson · ⏱ 7 min read

πŸ“¦ Docker: packaging your app so it runs anywhere

EnglishHinglish

🏠 Real-life example

Moving house, you pack everything for the kitchen (utensils, spices, gas regulator) into one labelled box so it works in the new home. A Docker container packs your app with its exact Python version and libraries, so it runs the same on your laptop, a server or the cloud. No more 'it works on my machine'.

πŸ’‘ In simple words

A Dockerfile is a recipe: start from a base image (like python:3.12-slim), copy your code, install requirements, and set the command to run. 'docker build' turns the recipe into an image, a frozen package, and 'docker run' starts a container from it. Images can be pushed to a registry and pulled by any server. Containers are the standard unit of deployment for model APIs, and Kubernetes runs containers at scale. Keep images small and never bake secrets into them.

🎬 See how it works

app.pyPython 3.12pandas Β· xgboostπŸ“¦ imageeverything it needsπŸ’» Laptop βœ“πŸ–₯️ Server βœ“β˜οΈ Cloud βœ“

Your code, the exact Python version and the libraries are packed into one image, so the container runs the same on a laptop, a server or the cloud.

πŸͺœ Step by step

  1. Write a Dockerfile: start FROM a slim Python image, COPY requirements.txt, RUN pip install, COPY your code, set the CMD.
  2. Build the image: docker build -t my-model:1.0 .
  3. Run a container: docker run -p 8000:8000 my-model:1.0, then open localhost:8000.
  4. Pass configuration and secrets as environment variables, not baked into the image.
  5. Push the image to a registry so servers and Kubernetes can pull the exact same thing.

πŸ”‘ Key terms

Image
A packaged, read-only snapshot of an app and its environment.
Container
A running instance of an image.
Registry
Storage for images, like Docker Hub or a cloud registry.

πŸ‘€ See it in action

dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

# docker build -t price-api . && docker run -p 8000:8000 price-api

Six lines that make your model API portable.

⚠️ Common mistakes to avoid

  • Huge images with build tools and data inside. Use slim bases, .dockerignore and multi stage builds.
  • Using the 'latest' tag in production. Pin versions so you know exactly what's running.

βœ… Quick check

What problem does Docker mainly solve?

  1. Making models more accurate
  2. 'It works on my machine but not on the server'
  3. Writing SQL
Show answer

B. 'It works on my machine but not on the server': Containers package the exact environment, so the app behaves the same everywhere.

πŸ™‹ Questions learners ask

Is Docker the same as a virtual machine?

No. Containers share the host's operating system kernel, so they're much lighter and start in seconds.

Can containers use GPUs?

Yes, with the NVIDIA container toolkit and CUDA base images.

πŸ“Œ Remember

Dockerfile (recipe) β†’ image (package) β†’ container (running app).

πŸ“– Words from this lesson (AI dictionary)

Learn this as part of a full course

Track your progress, take the quick checks and follow the lessons in the right order. This lesson is part of:

ML Engineer Β· MLOps / AI Platform Engineer

Continue in the course