diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1990805 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,41 @@ +# Build stage +ARG PYTHON_VERSION=3.12 +FROM python:${PYTHON_VERSION} AS base + +WORKDIR /app + +# Copy list of required packages to install +COPY requirements.txt ./ + +# Create venv +RUN python -m venv venv + +# Install requirements using venv and upgrade pip +RUN ./venv/bin/pip install --upgrade pip && \ + ./venv/bin/pip install -r requirements.txt + +# Copy the application code into the container +COPY accounts ./accounts/ +COPY api ./api/ +COPY lists ./lists/ +COPY todolist ./todolist/ +COPY manage.py ./ + +# Runtime stage +FROM python:${PYTHON_VERSION}-slim +WORKDIR /app + +# Set an environment variable for the runtime +ENV PYTHONUNBUFFERED=1 + +# Copy the built application and installed dependencies from the build stage +COPY --from=base /app . + +# Run migration +RUN ./venv/bin/python manage.py migrate + +# Expose port 8080 to the host +EXPOSE 8080 + +# Define the command to run the application +CMD ["./venv/bin/python", "manage.py", "runserver", "0.0.0.0:8080"] \ No newline at end of file diff --git a/INSTRUCTION.md b/INSTRUCTION.md new file mode 100644 index 0000000..86dc64f --- /dev/null +++ b/INSTRUCTION.md @@ -0,0 +1,86 @@ +# TodoApp — Docker Instructions + +## Docker Hub Repository + +The application image is available at: + +``` +https://hub.docker.com/repository/docker/nichelangeloo/todoapp/general +``` + +--- + +## Dockerfile Overview + +The `Dockerfile` uses a **two-stage build**: + +- **Build stage** — installs Python dependencies into an isolated layer. +- **Run stage** — copies only the installed packages and application code into a clean final image, keeping the image small. + +Key configuration: + +- `ARG PYTHON_VERSION` — parameterises the Python base image version (e.g. `3.14-slim`). +- `ENV PYTHONUNBUFFERED=1` — logs are written directly to stdout/stderr without buffering. +- Database migrations are applied at build time via a `RUN python manage.py migrate` instruction. +- The server is started with `python manage.py runserver 0.0.0.0:8080` so it is reachable from outside the container. + +--- + +## Building the Image Locally + +```bash +# Build with the default Python version +docker build -t todoapp:1.0.0 . + +# Or override the Python version explicitly +docker build --build-arg PYTHON_VERSION=3.11-slim -t todoapp:1.0.0 . + +# Tag the image for Docker Hub +docker tag todoapp:1.0.0 /todoapp:1.0.0 + +# Push the image to Docker Hub +docker push /todoapp:1.0.0 +``` + +--- + +## Running the Container + +```bash +docker run -d \ + --name todoapp \ + -p 8080:8080 \ + todoapp:1.0.0 +``` + +| Flag | Purpose | +| ---------------- | --------------------------------------------- | +| `-d` | Run in detached (background) mode | +| `--name todoapp` | Assign a human-readable name to the container | +| `-p 8080:8080` | Map host port 8080 to the container port 8080 | + +--- + +## Accessing the Application + +Once the container is running, open your browser and navigate to: + +``` +http://localhost:8080 +``` + +To verify the container is up: + +```bash +docker ps +docker logs todoapp +``` + +--- + +## Stopping and Removing the Container + +```bash +docker stop todoapp +docker rm todoapp +```