Writing a Dockerfile
A Dockerfile is a recipe for an image. Hereβs a small Python web app:
FROM python:3.12-slim
WORKDIR /app
# Dependencies first: this layer is cached until requirements.txt changesCOPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt
# Code last: it changes most oftenCOPY . .
# Never run as root inside the containerRUN useradd --create-home appuserUSER appuser
EXPOSE 8000CMD ["python", "-m", "http.server", "8000"]Build and run it:
docker build -t myapp:1.0 .docker run --rm -p 8000:8000 myapp:1.0Keep the build context small
Section titled βKeep the build context smallβEverything in the directory is sent to the builder. Add a .dockerignore:
.gitnode_modules__pycache__.env