Explorar o código

fastapi: add dev envs support

Signed-off-by: Milas Bowman <milas.bowman@docker.com>
Milas Bowman %!s(int64=3) %!d(string=hai) anos
pai
achega
035e3c5b43
Modificáronse 4 ficheiros con 44 adicións e 6 borrados
  1. 13 0
      fastapi/.docker/docker-compose.yaml
  2. 19 4
      fastapi/Dockerfile
  3. 9 0
      fastapi/README.md
  4. 3 2
      fastapi/compose.yaml

+ 13 - 0
fastapi/.docker/docker-compose.yaml

@@ -0,0 +1,13 @@
+services:
+  api:
+    build:
+      context: .
+      target: dev-envs
+    container_name: fastapi-application
+    environment:
+      PORT: 8000
+    ports:
+      - '8000:8000'
+    volumes:
+      - /var/run/docker.sock:/var/run/docker.sock
+    restart: "no"

+ 19 - 4
fastapi/Dockerfile

@@ -1,11 +1,26 @@
-FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim
+# syntax = docker/dockerfile:1.4
 
 
-WORKDIR /app
+FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim AS builder
 
 
-RUN apt update
+WORKDIR /app
 
 
 COPY requirements.txt ./
 COPY requirements.txt ./
-RUN pip install --no-cache-dir -r requirements.txt
+RUN --mount=type=cache,target=/root/.cache/pip \
+    pip install -r requirements.txt
 
 
 COPY ./app ./app
 COPY ./app ./app
 
 
+FROM builder as dev-envs
+
+RUN <<EOF
+apt-get update
+apt-get install -y --no-install-recommends git
+EOF
+
+RUN <<EOF
+useradd -s /bin/bash -m vscode
+groupadd docker
+usermod -aG docker vscode
+EOF
+# install Docker tools (cli, buildx, compose)
+COPY --from=gloursdocker/docker / /

+ 9 - 0
fastapi/README.md

@@ -52,4 +52,13 @@ Stop and remove the containers
 $ docker compose down
 $ 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/fastapi
+```

+ 3 - 2
fastapi/compose.yaml

@@ -1,10 +1,11 @@
 services:
 services:
   api:
   api:
-    build: .
+    build:
+      context: .
+      target: builder
     container_name: fastapi-application
     container_name: fastapi-application
     environment:
     environment:
       PORT: 8000
       PORT: 8000
     ports:
     ports:
       - '8000:8000'
       - '8000:8000'
     restart: "no"
     restart: "no"
-