Skip to main content

Chapter 36: Docker Containers

MODULE 6 | MLOps​

Chapter 36: Docker Containers​

In the last chapter the API ran on your computer. But to run the same API on someone else's computer or on a server, you need the right Python version, the right libraries and the right files. Even a small difference breaks the code. Docker solves this problem by packing your app and its whole environment into one box (container) , and that box runs the same everywhere .

TOPIC CHECKLIST (tick them off as you go)​

  • ☐ What Docker and a container are

  • ☐ The difference between an image and a container

  • ☐ Installing and checking Docker

  • Writing a Dockerfile (what each line means)

  • ☐

  • ☐ Running an ML app in a container (port mapping)

  • ☐ Basic commands: build, run, ps, stop

  • ☐ .dockerignore and common problems

36.1 What are Docker and a Container?​

36.2 Image vs Container​

These two words confuse people the most, so let's get them clear once and for all:

36.3 Installing and Checking Docker​

On Windows and Mac, install Docker Desktop (on Windows it uses WSL2 ). After installing, open Docker Desktop and let it run, then check in the terminal:

TERMINAL​

docker --version EXAMPLE OUTPUT (your version number will differ) Docker version 27.x.x, build xxxxxxx

36.4 Writing a Dockerfile​

A Dockerfile is a text file (named exactly Dockerfile, with no extension) in which you write how the image will be built : which Python, which files, which libraries, and which command will run. First put only the libraries the API needs into requirements.txt (write the versions too, by looking at your own pip freeze / pip show):

requirements.txt (write your own versions)​

fastapi==0.159.1 uvicorn==0.54.0 scikit-learn==1.8.0 joblib==1.5.3 Dockerfile FROM python:3.12-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY iris_api.py iris_model.joblib ./
EXPOSE 8000
CMD ["uvicorn", "iris_api:app", "--host", "0.0.0.0", "--port", "8000"]
LineWhat it does
FROM python:3.12-slimThe base: a small Linux withPython 3.12already in it
WORKDIR /appThe working folder /app inside the container (it is created if it does not exist)
COPY requirements.txt .Copy only the requirements file inside (first, the reason is below)
RUN pip install ...Installthe libraries while building the image
COPY iris_api.py iris_model.joblib ./Put the API code and the model file inside
EXPOSE 8000Says that the app will listen onport 8000(like documentation)
CMD [...]Run this commandas soon as the container starts: the API through Uvicorn

36.5 Running the App in a Container and Basic Commands​

The folder should contain these files: Dockerfile, .dockerignore, requirements.txt, iris_api.py, iris_model.joblib. Now do this in the terminal (in the same folder):

TERMINAL​

docker build -t iris-api . docker run -d -p 8000:8000 --name iris-container iris-api docker ps EXAMPLE OUTPUT (the ID and time will differ; the COMMAND and CREATED columns are left out here) CONTAINER ID IMAGE STATUS PORTS NAMES a1b2c3d4e5f6 iris-api Up 9 seconds 0.0.0.0:8000->8000/tcp iris-container

Now the same API is running inside the container . Open http://localhost:8000/docs in the browser or run curl http://localhost:8000/health; you will get the same response as in Chapter 35.

CommandMeaning
docker build -t iris-api .Build the image. -t iris-api is the image'sname (tag); the final .
means: use the Dockerfile ofthis folder
docker run -d -p 8000:8000 --name iris-container iris-<br />apiRun a containerfrom the image. -d = in the background, -p<br />8000:8000 =port mapping(outside:inside), --name = the
container's name
docker psList ofrunningcontainers (docker ps -a shows stopped ones too)
docker logs iris-containerSee the container'soutput / log(the most useful for finding errors)
docker stop iris-containerStopthe container
docker rm iris-containerRemovea stopped container
docker imagesList of existingimages
docker rmi iris-apiDeletethe image (remove its containers first)

36.6 What to Do with Very Large Models?​

It is fine to put small models (like those of our Iris or Medical Assistant) in the image. But if the model is GBs in size (for example a Random Forest larger than 1 GB), then do not COPY it into the image , and it cannot be pushed to GitHub either (GitHub puts a 100 MB limit on every file). The usual solution: keep the model on the Hugging Face Hub (or some storage) and have the container download it when it starts . This keeps the image small.

36.7 Common Problems and Their Solutions​

ProblemCause and solution
Cannot connect to the Docker daemonDocker Desktop isclosed. Open it and let it reach "running"
port is already allocatedAnother program is using that port. Give adifferent portsuch as -p 9000:8000,
or stop the old container
Container stops as soon as it startsLook at docker logs &lt;name&gt;. Often the file name or CMD is wrong
Does not open in the browserRe-check --host 0.0.0.0 and the -p mapping
Code changed but the old response still
comes
Build the image again(docker build ...), then remove the old container and
run a new one
Build is very slow / image is largeCreate a .dockerignore; keep only the necessary items in requirements.txt

36.8 Common Mistakes and Hands-on​

Hands-on​

  • Make a Dockerfile, requirements.txt and .dockerignore for iris_api.py of Chapter 35.

  • Build the image, run the container, and make one prediction through /docs.

  • stop the container, then see the difference between docker ps and docker ps -a.

  • run it again, but this time with -p 9000:8000 , and open localhost:9000/docs.

  • Run docker logs iris-container and see which lines from your API appear.

KEY TAKEAWAYS​
  • A Docker container holds your app and its whole environment , which is why it runs the same everywhere .

  • Image = ready packet (mould), Container = the running copy of it; many containers from one image.

  • A Dockerfile says how the image will be built: FROM, WORKDIR, COPY, RUN, EXPOSE, CMD.

  • Commands: docker build -t name ., docker run -d -p 8000:8000 --name x name, docker ps, docker stop x.

  • Run the server in the container on --host 0.0.0.0 and expose the port outward with -p .