From ec552567c7c3e4719e3b3664e78dc12f5d72486a Mon Sep 17 00:00:00 2001 From: Milas Bowman Date: Fri, 8 Jul 2022 09:22:59 -0400 Subject: [PATCH] flask: dev envs support & misc improvements (#263) * Docker Desktop Development Environments config * Use cache volumes for pip * Upgrade from Python 3.7 -> Python 3.10 * Use port `8000` to avoid conflicts with Airplay on macOS for default Flask port `5000` * Use `SIGINT` to gracefully stop Flask Signed-off-by: Milas Bowman --- flask/.docker/docker-compose.yaml | 10 ++++++++ flask/README.md | 42 ++++++++++++++++++++----------- flask/app/Dockerfile | 30 ++++++++++++++++++---- flask/app/app.py | 2 +- flask/compose.yaml | 11 +++++--- 5 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 flask/.docker/docker-compose.yaml diff --git a/flask/.docker/docker-compose.yaml b/flask/.docker/docker-compose.yaml new file mode 100644 index 0000000..8e9e1b2 --- /dev/null +++ b/flask/.docker/docker-compose.yaml @@ -0,0 +1,10 @@ +services: + web: + build: + context: app + target: dev-envs + stop_signal: SIGINT + ports: + - '8000:8000' + volumes: + - /var/run/docker.sock:/var/run/docker.sock diff --git a/flask/README.md b/flask/README.md index d97eb65..36e78d5 100644 --- a/flask/README.md +++ b/flask/README.md @@ -16,37 +16,38 @@ Project structure: ``` services: web: - build: app + build: + context: app + target: builder ports: - - '5000:5000' + - '8000:8000' ``` ## Deploy with docker compose ``` $ docker compose up -d -Creating network "flask_default" with the default driver -Building web -Step 1/6 : FROM python:3.7-alpine -... -... -Status: Downloaded newer image for python:3.7-alpine -Creating flask_web_1 ... done - +[+] Building 1.1s (16/16) FINISHED + => [internal] load build definition from Dockerfile 0.0s + ... 0.0s + => => naming to docker.io/library/flask_web 0.0s +[+] Running 2/2 + ⠿ Network flask_default Created 0.0s + ⠿ Container flask-web-1 Started ``` ## Expected result Listing containers must show one container running and the port mapping as below: ``` -$ docker ps -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -c126411df522 flask_web "python3 app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp flask_web_1 +$ docker compose ps +NAME COMMAND SERVICE STATUS PORTS +flask-web-1 "python3 app.py" web running 0.0.0.0:8000->8000/tcp ``` -After the application starts, navigate to `http://localhost:5000` in your web browser or run: +After the application starts, navigate to `http://localhost:8000` in your web browser or run: ``` -$ curl localhost:5000 +$ curl localhost:8000 Hello World! ``` @@ -54,3 +55,14 @@ Stop and remove the containers ``` $ docker compose down ``` + +## Use with Docker Development Environments + +You can use this sample with the Dev Environments feature of Docker Desktop. + +![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png) + +To develop directly on the services inside containers, use the HTTPS Git url of the sample: +``` +https://github.com/docker/awesome-compose/tree/master/flask +``` diff --git a/flask/app/Dockerfile b/flask/app/Dockerfile index cf861e9..d0be3b5 100644 --- a/flask/app/Dockerfile +++ b/flask/app/Dockerfile @@ -1,7 +1,27 @@ -FROM python:3.7-alpine -WORKDIR /app +# syntax=docker/dockerfile:1.4 +FROM --platform=$BUILDPLATFORM python:3.10-alpine AS builder + +WORKDIR /app + COPY requirements.txt /app -RUN pip3 install -r requirements.txt --no-cache-dir -COPY . /app -ENTRYPOINT ["python3"] +RUN --mount=type=cache,target=/root/.cache/pip \ + pip3 install -r requirements.txt + +COPY . /app + +ENTRYPOINT ["python3"] CMD ["app.py"] + +FROM builder as dev-envs + +RUN <