Dockerfile Basics

Your First Dockerfile

A Dockerfile lists the steps to build an image. Each instruction creates a layer.

Example

A small Python web app (app.py + requirements.txt):

Dockerfile

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

EXPOSE 8000
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

Build and Run It

Example

-t names and tags the image; the final . is the build context (the folder sent to the builder):

docker build -t myapi:1.0 .
docker run -d -p 8000:8000 --name api myapi:1.0

Example output

[+] Building 18.4s (10/10) FINISHED
 => [1/5] FROM docker.io/library/python:3.12-slim
 => [2/5] WORKDIR /app
 => [3/5] COPY requirements.txt .
 => [4/5] RUN pip install --no-cache-dir -r requirements.txt
 => [5/5] COPY . .
 => exporting to image
 => => naming to docker.io/library/myapi:1.0