Skip to content

Commit 457fe0e

Browse files
authored
Merge pull request docker#262 from milas/dev-envs-fastapi
fastapi: add dev envs support
2 parents 9a20ca6 + 035e3c5 commit 457fe0e

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
api:
3+
build:
4+
context: .
5+
target: dev-envs
6+
container_name: fastapi-application
7+
environment:
8+
PORT: 8000
9+
ports:
10+
- '8000:8000'
11+
volumes:
12+
- /var/run/docker.sock:/var/run/docker.sock
13+
restart: "no"

fastapi/Dockerfile

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim
1+
# syntax = docker/dockerfile:1.4
22

3-
WORKDIR /app
3+
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9-slim AS builder
44

5-
RUN apt update
5+
WORKDIR /app
66

77
COPY requirements.txt ./
8-
RUN pip install --no-cache-dir -r requirements.txt
8+
RUN --mount=type=cache,target=/root/.cache/pip \
9+
pip install -r requirements.txt
910

1011
COPY ./app ./app
1112

13+
FROM builder as dev-envs
14+
15+
RUN <<EOF
16+
apt-get update
17+
apt-get install -y --no-install-recommends git
18+
EOF
19+
20+
RUN <<EOF
21+
useradd -s /bin/bash -m vscode
22+
groupadd docker
23+
usermod -aG docker vscode
24+
EOF
25+
# install Docker tools (cli, buildx, compose)
26+
COPY --from=gloursdocker/docker / /

fastapi/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@ Stop and remove the containers
5252
$ docker compose down
5353
```
5454

55+
## Use with Docker Development Environments
5556

57+
You can use this sample with the Dev Environments feature of Docker Desktop.
58+
59+
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
60+
61+
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
62+
```
63+
https://github.com/docker/awesome-compose/tree/master/fastapi
64+
```

fastapi/compose.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
services:
22
api:
3-
build: .
3+
build:
4+
context: .
5+
target: builder
46
container_name: fastapi-application
57
environment:
68
PORT: 8000
79
ports:
810
- '8000:8000'
911
restart: "no"
10-

0 commit comments

Comments
 (0)