Skip to content

Commit ec55256

Browse files
authored
flask: dev envs support & misc improvements (docker#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 <[email protected]>
1 parent 457fe0e commit ec55256

File tree

5 files changed

+71
-24
lines changed

5 files changed

+71
-24
lines changed

flask/.docker/docker-compose.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
web:
3+
build:
4+
context: app
5+
target: dev-envs
6+
stop_signal: SIGINT
7+
ports:
8+
- '8000:8000'
9+
volumes:
10+
- /var/run/docker.sock:/var/run/docker.sock

flask/README.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,53 @@ Project structure:
1616
```
1717
services:
1818
web:
19-
build: app
19+
build:
20+
context: app
21+
target: builder
2022
ports:
21-
- '5000:5000'
23+
- '8000:8000'
2224
```
2325

2426
## Deploy with docker compose
2527

2628
```
2729
$ docker compose up -d
28-
Creating network "flask_default" with the default driver
29-
Building web
30-
Step 1/6 : FROM python:3.7-alpine
31-
...
32-
...
33-
Status: Downloaded newer image for python:3.7-alpine
34-
Creating flask_web_1 ... done
35-
30+
[+] Building 1.1s (16/16) FINISHED
31+
=> [internal] load build definition from Dockerfile 0.0s
32+
... 0.0s
33+
=> => naming to docker.io/library/flask_web 0.0s
34+
[+] Running 2/2
35+
⠿ Network flask_default Created 0.0s
36+
⠿ Container flask-web-1 Started
3637
```
3738

3839
## Expected result
3940

4041
Listing containers must show one container running and the port mapping as below:
4142
```
42-
$ docker ps
43-
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
44-
c126411df522 flask_web "python3 app.py" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp flask_web_1
43+
$ docker compose ps
44+
NAME COMMAND SERVICE STATUS PORTS
45+
flask-web-1 "python3 app.py" web running 0.0.0.0:8000->8000/tcp
4546
```
4647

47-
After the application starts, navigate to `http://localhost:5000` in your web browser or run:
48+
After the application starts, navigate to `http://localhost:8000` in your web browser or run:
4849
```
49-
$ curl localhost:5000
50+
$ curl localhost:8000
5051
Hello World!
5152
```
5253

5354
Stop and remove the containers
5455
```
5556
$ docker compose down
5657
```
58+
59+
## Use with Docker Development Environments
60+
61+
You can use this sample with the Dev Environments feature of Docker Desktop.
62+
63+
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
64+
65+
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
66+
```
67+
https://github.com/docker/awesome-compose/tree/master/flask
68+
```

flask/app/Dockerfile

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1-
FROM python:3.7-alpine
2-
WORKDIR /app
1+
# syntax=docker/dockerfile:1.4
2+
FROM --platform=$BUILDPLATFORM python:3.10-alpine AS builder
3+
4+
WORKDIR /app
5+
36
COPY requirements.txt /app
4-
RUN pip3 install -r requirements.txt --no-cache-dir
5-
COPY . /app
6-
ENTRYPOINT ["python3"]
7+
RUN --mount=type=cache,target=/root/.cache/pip \
8+
pip3 install -r requirements.txt
9+
10+
COPY . /app
11+
12+
ENTRYPOINT ["python3"]
713
CMD ["app.py"]
14+
15+
FROM builder as dev-envs
16+
17+
RUN <<EOF
18+
apk update
19+
apk add git
20+
EOF
21+
22+
RUN <<EOF
23+
addgroup -S docker
24+
adduser -S --shell /bin/bash --ingroup docker vscode
25+
EOF
26+
# install Docker tools (cli, buildx, compose)
27+
COPY --from=gloursdocker/docker / /

flask/app/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ def hello():
66
return "Hello World!"
77

88
if __name__ == '__main__':
9-
app.run(host='0.0.0.0')
9+
app.run(host='0.0.0.0', port=8000)

flask/compose.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
services:
22
web:
3-
build: app
4-
ports:
5-
- '5000:5000'
3+
build:
4+
context: app
5+
target: builder
6+
# flask requires SIGINT to stop gracefully
7+
# (default stop signal from Compose is SIGTERM)
8+
stop_signal: SIGINT
9+
ports:
10+
- '8000:8000'

0 commit comments

Comments
 (0)