From c4dbcf674ebe96cc570324626679628e3d259b91 Mon Sep 17 00:00:00 2001 From: Harshit Mohanta Date: Sun, 9 Nov 2025 16:53:06 +0530 Subject: [PATCH] feat: add working Flask app Docker example --- Dockerfile | 48 ++++++++++++++---------------------------------- Dockerfile.orig | 40 ++++++++++++++++++++++++++++++++++++++++ README.md | 47 +++++++++++++---------------------------------- app.py | 10 ++++++++++ requirements.txt | 1 + 5 files changed, 78 insertions(+), 68 deletions(-) create mode 100644 Dockerfile.orig create mode 100644 app.py diff --git a/Dockerfile b/Dockerfile index 5e4c7595d..fe123ba3f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,40 +1,20 @@ -# Install the base requirements for the app. -# This stage is to support development. -FROM --platform=$BUILDPLATFORM python:alpine AS base -WORKDIR /app -COPY requirements.txt . -RUN pip install -r requirements.txt +# Use lightweight Python base image +FROM python:3.11-alpine -FROM --platform=$BUILDPLATFORM node:18-alpine AS app-base +# Set working directory WORKDIR /app -COPY app/package.json app/yarn.lock ./ -COPY app/spec ./spec -COPY app/src ./src - -# Run tests to validate app -FROM app-base AS test -RUN yarn install -RUN yarn test -# Clear out the node_modules and create the zip -FROM app-base AS app-zip-creator -COPY --from=test /app/package.json /app/yarn.lock ./ -COPY app/spec ./spec -COPY app/src ./src -RUN apk add zip && \ - zip -r /app.zip /app - -# Dev-ready container - actual files will be mounted in -FROM --platform=$BUILDPLATFORM base AS dev -CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"] +# Copy requirements and install dependencies +COPY requirements.txt . +RUN apk add --no-cache build-base libffi-dev openssl-dev +RUN pip install --upgrade pip +RUN pip install -r requirements.txt -# Do the actual build of the mkdocs site -FROM --platform=$BUILDPLATFORM base AS build +# Copy all other project files COPY . . -RUN mkdocs build -# Extract the static content from the build -# and use a nginx image to serve the content -FROM --platform=$TARGETPLATFORM nginx:alpine -COPY --from=app-zip-creator /app.zip /usr/share/nginx/html/assets/app.zip -COPY --from=build /app/site /usr/share/nginx/html +# Expose app port +EXPOSE 3000 + +# Run the app +CMD ["python", "app.py"] diff --git a/Dockerfile.orig b/Dockerfile.orig new file mode 100644 index 000000000..b81edd5a2 --- /dev/null +++ b/Dockerfile.orig @@ -0,0 +1,40 @@ + + + +# Install the base requirements for the app +# This stage supports development +FROM python:3.11-alpine AS base + +# Install system dependencies required for building Python packages +RUN apk add --no-cache build-base libffi-dev openssl-dev + +WORKDIR /app +COPY requirements.txt . +RUN pip install --upgrade pip +RUN pip install -r requirements.txt +FROM app-base AS test +RUN yarn install +RUN yarn test + +# Clear out the node_modules and create the zip +FROM app-base AS app-zip-creator +COPY --from=test /app/package.json /app/yarn.lock ./ +COPY app/spec ./spec +COPY app/src ./src +RUN apk add zip && \ + zip -r /app.zip /app + +# Dev-ready container - actual files will be mounted in +FROM --platform=$BUILDPLATFORM base AS dev +CMD ["mkdocs", "serve", "-a", "0.0.0.0:8000"] + +# Do the actual build of the mkdocs site +FROM --platform=$BUILDPLATFORM base AS build +COPY . . +RUN mkdocs build + +# Extract the static content from the build +# and use a nginx image to serve the content +FROM --platform=$TARGETPLATFORM nginx:alpine +COPY --from=app-zip-creator /app.zip /usr/share/nginx/html/assets/app.zip +COPY --from=build /app/site /usr/share/nginx/html diff --git a/README.md b/README.md index 5ccd135dd..4f5f72457 100644 --- a/README.md +++ b/README.md @@ -1,44 +1,23 @@ -# Docker Getting Started Tutorial +# Docker Flask Setup -This tutorial was written with the intent of helping folks get up and running -with containers and is designed to work with Docker Desktop. While not going too much -into depth, it covers the following topics: +A minimal Flask app containerized with Docker. +This repository demonstrates how to build and run a small Flask app inside a Docker container — useful for quick local testing and learning Docker. -- Running your first container -- Building containers -- Learning what containers are -- Running and removing containers -- Using volumes to persist data -- Using bind mounts to support development -- Using container networking to support multi-container applications -- Using Docker Compose to simplify the definition and sharing of applications -- Using image layer caching to speed up builds and reduce push/pull size -- Using multi-stage builds to separate build-time and runtime dependencies +--- -## Getting Started +## 🧰 Contents +- `Dockerfile` — simple Dockerfile using `python:3.11-alpine` +- `app.py` — minimal Flask app +- `requirements.txt` — Python packages (Flask) -If you wish to run the tutorial, you can use the following command after installing Docker Desktop: +--- -```bash -docker run -d -p 80:80 docker/getting-started -``` - -Once it has started, you can open your browser to [http://localhost](http://localhost). - -## Development - -This project has a `docker-compose.yml` file, which will start the mkdocs application on your -local machine and help you see changes instantly. +## 🚀 Quickstart (build & run) +### 1️⃣ Build the image ```bash -docker compose up -``` +docker build -t getting-started:flask . -## Contributing -If you find typos or other issues with the tutorial, feel free to create a PR and suggest fixes! +docker run -d -p 3000:3000 --name getting-started getting-started:flask -If you have ideas on how to make the tutorial better or want to suggest adding new content, please open an -issue first before working on your idea. While we love input, we want to keep the tutorial scoped to new-comers. -As such, we may reject ideas for more advanced requests and don't want you to lose any work you might -have done. So, ask first and we'll gladly hear your thoughts! diff --git a/app.py b/app.py new file mode 100644 index 000000000..e7a60364c --- /dev/null +++ b/app.py @@ -0,0 +1,10 @@ +from flask import Flask +app = Flask(__name__) + +@app.route('/') +def home(): + return "✅ Docker Flask App is Running Successfully!" + +if __name__ == '__main__': + # Make sure it listens on all interfaces so Docker can expose it + app.run(host='0.0.0.0', port=3000, debug=True) diff --git a/requirements.txt b/requirements.txt index de34b2a8b..bf30c091f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ mkdocs-material==4.6.3 mkdocs-minify-plugin==0.2.3 pygments==2.7.4 pymdown-extensions==7.0 +flask