# 1. Base Image
FROM python:3.10-slim
# 2. Metadata (optional)
LABEL maintainer="you@example.com"
LABEL version="1.0"
# 3. Set working directory inside the container
WORKDIR /app
# 4. Copy files into the container
COPY requirements.txt .
# 5. Run a command (e.g., install dependencies)
RUN pip install -r requirements.txt
# 6. Copy the rest of the app
COPY . .
# 7. Environment variables (optional)
ENV ENVIRONMENT=production
# 8. Port to expose (optional, mainly for documentation)
EXPOSE 8000
# 9. Default command to run when the container starts
CMD ["python", "main.py"]
🔍 Line-by-Line Explanation
Command | Purpose |
---|---|
FROM | Specifies the base image. Every Docker image starts from a base. Could be ubuntu, node, python, etc. |
LABEL | Adds metadata. Not required but helps identify or manage images. |
WORKDIR | Sets the working directory inside the container. All subsequent commands will run from here. |
COPY | Copies files/folders from your local machine into the container. |
RUN | Executes commands at build time, e.g., install dependencies. Each RUN creates a new image layer. |
ENV | Sets environment variables. Useful for configuration. |
EXPOSE | Declares what port the app runs on. Optional but informative. |
CMD | The default command to run when the container starts. Only one CMD is allowed per Dockerfile (the last one used wins). |