Skip to content

Commit b53e454

Browse files
authored
flask-redis: dev envs support & misc improvements (docker#265)
(Most of this is almost identical to docker#263.) * Docker Desktop Development Environments config * Use cache volumes for pip * Downgrade from Python 3.11 _alpha_ -> Python 3.10 * Use port `8000` to avoid conflicts with Airplay on macOS for default Flask port `5000` * Use `SIGINT` to gracefully stop Flask * Formatting fixes in `compose.yaml` Signed-off-by: Milas Bowman <[email protected]>
1 parent ec55256 commit b53e454

File tree

5 files changed

+73
-20
lines changed

5 files changed

+73
-20
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
services:
2+
redis:
3+
image: redislabs/redismod
4+
ports:
5+
- '6379:6379'
6+
web:
7+
build:
8+
context: .
9+
target: dev-envs
10+
stop_signal: SIGINT
11+
ports:
12+
- '8000:8000'
13+
volumes:
14+
- /var/run/docker.sock:/var/run/docker.sock
15+
depends_on:
16+
- redis

flask-redis/Dockerfile

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1-
FROM python:3.11.0a6-alpine3.15
1+
# syntax=docker/dockerfile:1.4
2+
FROM --platform=$BUILDPLATFORM python:3.10-alpine AS builder
3+
24
WORKDIR /code
5+
36
COPY requirements.txt /code
4-
RUN pip install -r requirements.txt --no-cache-dir
7+
RUN --mount=type=cache,target=/root/.cache/pip \
8+
pip3 install -r requirements.txt
9+
510
COPY . /code
6-
CMD python app.py
11+
12+
ENTRYPOINT ["python3"]
13+
CMD ["app.py"]
14+
15+
FROM builder as dev-envs
16+
17+
RUN <<EOF
18+
apk update
19+
apk add git bash
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-redis/README.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ services:
2424
web:
2525
build: .
2626
ports:
27-
- "5000:5000"
27+
- "8000:8000"
2828
volumes:
2929
- .:/code
3030
depends_on:
@@ -55,12 +55,12 @@ Listing containers must show one container running and the port mapping as below
5555
$ docker compose ps
5656
NAME COMMAND SERVICE STATUS PORTS
5757
flask-redis-redis-1 "redis-server --load…" redis running 0.0.0.0:6379->6379/tcp
58-
flask-redis-web-1 "/bin/sh -c 'python …" web running 0.0.0.0:5000->5000/tcp
58+
flask-redis-web-1 "/bin/sh -c 'python …" web running 0.0.0.0:8000->8000/tcp
5959
```
6060

61-
After the application starts, navigate to `http://localhost:5000` in your web browser or run:
61+
After the application starts, navigate to `http://localhost:8000` in your web browser or run:
6262
```
63-
$ curl localhost:5000
63+
$ curl localhost:8000
6464
This webpage has been viewed 2 time(s)
6565
```
6666

@@ -80,3 +80,14 @@ Stop and remove the containers
8080
```
8181
$ docker compose down
8282
```
83+
84+
## Use with Docker Development Environments
85+
86+
You can use this sample with the Dev Environments feature of Docker Desktop.
87+
88+
![Screenshot of creating a Dev Environment in Docker Desktop](../dev-envs.png)
89+
90+
To develop directly on the services inside containers, use the HTTPS Git url of the sample:
91+
```
92+
https://github.com/docker/awesome-compose/tree/master/flask-redis
93+
```

flask-redis/app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ def hello():
1111
return "This webpage has been viewed "+counter+" time(s)"
1212

1313
if __name__ == "__main__":
14-
app.run(host="0.0.0.0", debug=True)
14+
app.run(host="0.0.0.0", port=8000, debug=True)

flask-redis/compose.yaml

+17-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
services:
2-
redis:
3-
image: redislabs/redismod
4-
ports:
5-
- '6379:6379'
6-
web:
7-
build: .
8-
ports:
9-
- "5000:5000"
10-
volumes:
11-
- .:/code
12-
depends_on:
13-
- redis
2+
redis:
3+
image: redislabs/redismod
4+
ports:
5+
- '6379:6379'
6+
web:
7+
build:
8+
context: .
9+
target: builder
10+
# flask requires SIGINT to stop gracefully
11+
# (default stop signal from Compose is SIGTERM)
12+
stop_signal: SIGINT
13+
ports:
14+
- '8000:8000'
15+
volumes:
16+
- .:/code
17+
depends_on:
18+
- redis

0 commit comments

Comments
 (0)